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(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 (

{t('media.list.empty')}

); } return (
onSelectionChange(state === 'all' ? new Set() : selectAll(selectableIds))} /> {t('media.list.selectAll')} {t('media.bulk.selected', { count: selection.size })}
); }