Continuous Integration / config-asf (pull_request) Successful in 6s
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 11s
Continuous Integration / compile-sites (pull_request) Successful in 1m44s
Continuous Integration / backend-build (pull_request) Successful in 1m49s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m51s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m2s
Continuous Integration / frontend-build (pull_request) Successful in 42s
Continuous Integration / backend-test (pull_request) Failing after 3m56s
Continuous Integration / frontend-lint (pull_request) Successful in 39s
Continuous Integration / frontend-test (pull_request) Successful in 2m33s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
123 lines
5.8 KiB
TypeScript
123 lines
5.8 KiB
TypeScript
import { useRef } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { Checkbox } from '@/components/ui/checkbox';
|
|
import { formatBytes } from '../lib/format';
|
|
import { selectAll, selectRange, stateOf, type Selection } from '../lib/selection';
|
|
|
|
export interface PhotoUse {
|
|
/** The module key, for example `hero`; the screen translates it. */
|
|
module: string;
|
|
/** The kind of place, for example `slide`. */
|
|
kind: string;
|
|
position?: number | null;
|
|
active: boolean;
|
|
}
|
|
|
|
export interface PhotoItem {
|
|
id: string;
|
|
thumbnailUrl?: string | null;
|
|
name: string;
|
|
bytes: number;
|
|
createdAt: string;
|
|
uses: readonly PhotoUse[];
|
|
}
|
|
|
|
interface PhotoSelectionListProps {
|
|
items: readonly PhotoItem[];
|
|
selection: Selection;
|
|
onSelectionChange: (selection: Selection) => void;
|
|
/** Photos that cannot be selected (for example in use during a clean-up). */
|
|
isDisabled?: (item: PhotoItem) => boolean;
|
|
}
|
|
|
|
/**
|
|
* The photos with a checkbox each: click or Space to toggle, shift-click for a range, a "select all" box in the header
|
|
* (with the in-between state), and a live announcement of the count. One component for the library and the clean-up.
|
|
*/
|
|
export function PhotoSelectionList({ items, selection, onSelectionChange, isDisabled }: PhotoSelectionListProps) {
|
|
const { t, i18n } = useTranslation();
|
|
const anchor = useRef<string | null>(null);
|
|
const selectable = items.filter((item) => !isDisabled?.(item));
|
|
const selectableIds = selectable.map((item) => item.id);
|
|
const state = stateOf(selection, selectableIds);
|
|
const dateFormat = new Intl.DateTimeFormat(i18n.language, { dateStyle: 'medium', timeStyle: 'short' });
|
|
|
|
const onItemClick = (id: string, checked: boolean, shift: boolean) => {
|
|
onSelectionChange(selectRange(selection, selectableIds, shift ? anchor.current : null, id, checked));
|
|
anchor.current = id;
|
|
};
|
|
|
|
const usageLabel = (use: PhotoUse) => {
|
|
const place = t(`media.use.${use.kind}`, { defaultValue: use.kind });
|
|
const where = use.position ? `${place} ${use.position}` : place;
|
|
return `${t(`media.module.${use.module}`, { defaultValue: use.module })}, ${where}${use.active ? '' : ` (${t('media.use.off')})`}`;
|
|
};
|
|
|
|
if (items.length === 0) {
|
|
return (
|
|
<p className="rounded-md border border-dashed p-6 text-center text-sm text-muted-foreground" data-testid="photo-list-empty">
|
|
{t('media.list.empty')}
|
|
</p>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div data-testid="photo-selection-list">
|
|
<div className="flex items-center gap-3 border-b px-2 py-2">
|
|
<Checkbox
|
|
aria-label={t('media.list.selectAll')}
|
|
data-testid="photo-select-all"
|
|
checked={state === 'all'}
|
|
indeterminate={state === 'some'}
|
|
disabled={selectableIds.length === 0}
|
|
onChange={() => onSelectionChange(state === 'all' ? new Set() : selectAll(selectableIds))}
|
|
/>
|
|
<span className="text-sm text-muted-foreground">{t('media.list.selectAll')}</span>
|
|
<span className="sr-only" role="status" aria-live="polite" data-testid="photo-selection-status">
|
|
{t('media.bulk.selected', { count: selection.size })}
|
|
</span>
|
|
</div>
|
|
<ul className="divide-y" aria-label={t('media.list.label')}>
|
|
{items.map((item) => {
|
|
const disabled = isDisabled?.(item) ?? false;
|
|
const labelId = `photo-${item.id}-label`;
|
|
return (
|
|
<li key={item.id} className="flex items-center gap-3 px-2 py-2" data-testid={`photo-row-${item.id}`}>
|
|
<Checkbox
|
|
aria-labelledby={labelId}
|
|
data-testid={`photo-select-${item.id}`}
|
|
checked={selection.has(item.id)}
|
|
disabled={disabled}
|
|
onChange={() => undefined}
|
|
onClick={(event) => onItemClick(item.id, event.currentTarget.checked, event.shiftKey)}
|
|
/>
|
|
{item.thumbnailUrl ? (
|
|
<img src={item.thumbnailUrl} alt="" loading="lazy" className="size-14 shrink-0 rounded object-cover" />
|
|
) : (
|
|
<span className="flex size-14 shrink-0 items-center justify-center rounded bg-muted text-xs text-muted-foreground">
|
|
{t('media.list.noThumbnail')}
|
|
</span>
|
|
)}
|
|
<div className="min-w-0 flex-1">
|
|
<p id={labelId} className="truncate text-sm font-medium">
|
|
{item.name}
|
|
</p>
|
|
<p className="text-xs text-muted-foreground">
|
|
{dateFormat.format(new Date(item.createdAt))} · {formatBytes(item.bytes, i18n.language)}
|
|
</p>
|
|
<p className="text-xs" data-testid={`photo-usage-${item.id}`}>
|
|
{item.uses.length === 0 ? (
|
|
<span className="text-muted-foreground">{t('media.use.none')}</span>
|
|
) : (
|
|
<span>{item.uses.map(usageLabel).join('; ')}</span>
|
|
)}
|
|
</p>
|
|
</div>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</div>
|
|
);
|
|
}
|