Merge pull request 'Mediabibliotheek fase A: gebruikscontract, opruimselectie en selectiecomponent' (#21) from feature/media-library-selection into master
Continuous Integration / config-asf (push) Successful in 7s
Continuous Integration / config-sls (push) Successful in 7s
Continuous Integration / changes (push) Successful in 11s
Continuous Integration / compile-sites (push) Successful in 1m51s
Continuous Integration / vulnerability-scan (push) Successful in 1m54s
Continuous Integration / backend-build (push) Successful in 1m51s
Continuous Integration / frontend-prepare (push) Successful in 1m54s
Continuous Integration / frontend-build (push) Successful in 37s
Continuous Integration / backend-test (push) Successful in 2m29s
Continuous Integration / frontend-test (push) Successful in 2m29s
Continuous Integration / frontend-lint (push) Successful in 2m29s
Continuous Integration / publish-sls-production (push) Skipped
Continuous Integration / deploy-sls-production (push) Skipped
Continuous Integration / publish-asf-test (push) Successful in 1m31s
Continuous Integration / publish-sls-test (push) Successful in 1m47s
Continuous Integration / deploy-asf-test (push) Successful in 39s
Continuous Integration / smoke-asf-test (push) Successful in 11s
Continuous Integration / deploy-sls-test (push) Successful in 42s
Continuous Integration / smoke-sls-test (push) Successful in 9s
Deploy (SCP) / deploy (push) Successful in 41s
Continuous Integration / config-asf (push) Successful in 7s
Continuous Integration / config-sls (push) Successful in 7s
Continuous Integration / changes (push) Successful in 11s
Continuous Integration / compile-sites (push) Successful in 1m51s
Continuous Integration / vulnerability-scan (push) Successful in 1m54s
Continuous Integration / backend-build (push) Successful in 1m51s
Continuous Integration / frontend-prepare (push) Successful in 1m54s
Continuous Integration / frontend-build (push) Successful in 37s
Continuous Integration / backend-test (push) Successful in 2m29s
Continuous Integration / frontend-test (push) Successful in 2m29s
Continuous Integration / frontend-lint (push) Successful in 2m29s
Continuous Integration / publish-sls-production (push) Skipped
Continuous Integration / deploy-sls-production (push) Skipped
Continuous Integration / publish-asf-test (push) Successful in 1m31s
Continuous Integration / publish-sls-test (push) Successful in 1m47s
Continuous Integration / deploy-asf-test (push) Successful in 39s
Continuous Integration / smoke-asf-test (push) Successful in 11s
Continuous Integration / deploy-sls-test (push) Successful in 42s
Continuous Integration / smoke-sls-test (push) Successful in 9s
Deploy (SCP) / deploy (push) Successful in 41s
This commit was merged in pull request #21.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
export interface BulkAction {
|
||||
id: string;
|
||||
label: string;
|
||||
/** A destructive action (delete) is drawn in the warning colour. */
|
||||
destructive?: boolean;
|
||||
disabled?: boolean;
|
||||
icon?: ReactNode;
|
||||
run: () => void;
|
||||
}
|
||||
|
||||
interface BulkActionBarProps {
|
||||
count: number;
|
||||
actions: readonly BulkAction[];
|
||||
onClear: () => void;
|
||||
/** Extra text on the bar, for example the bytes the selection takes. */
|
||||
summary?: ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* The actions on the selected photos. The actions are data, so more of them (tags, move) can be added without touching
|
||||
* the list. It shows nothing while nothing is selected.
|
||||
*/
|
||||
export function BulkActionBar({ count, actions, onClear, summary }: BulkActionBarProps) {
|
||||
const { t } = useTranslation();
|
||||
if (count === 0) return null;
|
||||
return (
|
||||
<div
|
||||
role="toolbar"
|
||||
aria-label={t('media.bulk.label')}
|
||||
data-testid="bulk-action-bar"
|
||||
className="sticky bottom-0 z-10 flex flex-wrap items-center gap-3 rounded-md border bg-background px-4 py-2 shadow-md"
|
||||
>
|
||||
<span className="text-sm font-medium" data-testid="bulk-count">
|
||||
{t('media.bulk.selected', { count })}
|
||||
</span>
|
||||
{summary && <span className="text-sm text-muted-foreground">{summary}</span>}
|
||||
<div className="ml-auto flex flex-wrap items-center gap-2">
|
||||
<Button type="button" variant="ghost" size="sm" onClick={onClear} data-testid="bulk-clear">
|
||||
{t('media.bulk.clear')}
|
||||
</Button>
|
||||
{actions.map((action) => (
|
||||
<Button
|
||||
key={action.id}
|
||||
type="button"
|
||||
size="sm"
|
||||
variant={action.destructive ? 'destructive' : 'outline'}
|
||||
disabled={action.disabled}
|
||||
onClick={action.run}
|
||||
data-testid={`bulk-action-${action.id}`}
|
||||
>
|
||||
{action.icon}
|
||||
{action.label}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { useState } from 'react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import i18n from '@/i18n/config';
|
||||
import { emptySelection, type Selection } from '../lib/selection';
|
||||
import { BulkActionBar } from './BulkActionBar';
|
||||
import { PhotoSelectionList, type PhotoItem } from './PhotoSelectionList';
|
||||
|
||||
const items: PhotoItem[] = [
|
||||
{ id: 'p1', name: 'Foto 1', bytes: 2048, createdAt: '2026-11-17T10:00:00Z', uses: [{ module: 'hero', kind: 'slide', position: 3, active: true }] },
|
||||
{ id: 'p2', name: 'Foto 2', bytes: 500, createdAt: '2026-11-16T10:00:00Z', uses: [] },
|
||||
{ id: 'p3', name: 'Foto 3', bytes: 1048576, createdAt: '2026-11-15T10:00:00Z', uses: [{ module: 'hero', kind: 'slide', position: 1, active: false }] },
|
||||
{ id: 'p4', name: 'Foto 4', bytes: 10, createdAt: '2026-11-14T10:00:00Z', uses: [] },
|
||||
];
|
||||
|
||||
function Harness({ disabled }: { disabled?: (item: PhotoItem) => boolean }) {
|
||||
const [selection, setSelection] = useState<Selection>(emptySelection);
|
||||
return (
|
||||
<>
|
||||
<PhotoSelectionList items={items} selection={selection} onSelectionChange={setSelection} isDisabled={disabled} />
|
||||
<BulkActionBar count={selection.size} actions={[{ id: 'delete', label: 'Verwijderen', destructive: true, run: vi.fn() }]} onClear={() => setSelection(emptySelection)} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await i18n.changeLanguage('nl');
|
||||
});
|
||||
|
||||
describe('PhotoSelectionList', () => {
|
||||
it('shows each photo with where it is used, or that it is not used', () => {
|
||||
render(<Harness />);
|
||||
|
||||
expect(screen.getByTestId('photo-usage-p1')).toHaveTextContent('Hero, slide 3');
|
||||
expect(screen.getByTestId('photo-usage-p2')).toHaveTextContent('Niet gebruikt');
|
||||
expect(screen.getByTestId('photo-usage-p3')).toHaveTextContent('Hero, slide 1 (uit)');
|
||||
});
|
||||
|
||||
it('selects a photo with a click and announces the count', async () => {
|
||||
render(<Harness />);
|
||||
|
||||
await userEvent.click(screen.getByRole('checkbox', { name: 'Foto 2' }));
|
||||
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 2' })).toBeChecked();
|
||||
expect(screen.getByTestId('photo-selection-status')).toHaveTextContent('1 foto geselecteerd');
|
||||
expect(screen.getByTestId('bulk-count')).toHaveTextContent('1 foto geselecteerd');
|
||||
});
|
||||
|
||||
it('selects a range with shift-click', async () => {
|
||||
render(<Harness />);
|
||||
|
||||
const user = userEvent.setup();
|
||||
|
||||
await user.click(screen.getByRole('checkbox', { name: 'Foto 1' }));
|
||||
await user.keyboard('{Shift>}');
|
||||
await user.click(screen.getByRole('checkbox', { name: 'Foto 3' }));
|
||||
await user.keyboard('{/Shift}');
|
||||
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 2' })).toBeChecked();
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 3' })).toBeChecked();
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 4' })).not.toBeChecked();
|
||||
});
|
||||
|
||||
it('works with the keyboard: Space toggles the focused photo', async () => {
|
||||
render(<Harness />);
|
||||
|
||||
screen.getByRole('checkbox', { name: 'Foto 4' }).focus();
|
||||
await userEvent.keyboard(' ');
|
||||
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 4' })).toBeChecked();
|
||||
});
|
||||
|
||||
it('select all shows the in-between state for part of the list and clears everything on a second click', async () => {
|
||||
render(<Harness />);
|
||||
const all = screen.getByRole('checkbox', { name: 'Alles selecteren' }) as HTMLInputElement;
|
||||
|
||||
await userEvent.click(screen.getByRole('checkbox', { name: 'Foto 1' }));
|
||||
expect(all.indeterminate).toBe(true);
|
||||
|
||||
await userEvent.click(all);
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 4' })).toBeChecked();
|
||||
expect(all).toBeChecked();
|
||||
|
||||
await userEvent.click(all);
|
||||
expect(screen.queryByTestId('bulk-action-bar')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('leaves out photos that cannot be selected, from select all and from ranges', async () => {
|
||||
render(<Harness disabled={(item) => item.uses.length > 0} />);
|
||||
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 1' })).toBeDisabled();
|
||||
await userEvent.click(screen.getByRole('checkbox', { name: 'Alles selecteren' }));
|
||||
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 2' })).toBeChecked();
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 4' })).toBeChecked();
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 1' })).not.toBeChecked();
|
||||
expect(screen.getByTestId('bulk-count')).toHaveTextContent('2 foto');
|
||||
});
|
||||
|
||||
it('shows a message when there are no photos', () => {
|
||||
render(<PhotoSelectionList items={[]} selection={emptySelection} onSelectionChange={vi.fn()} />);
|
||||
|
||||
expect(screen.getByTestId('photo-list-empty')).toHaveTextContent('Er zijn geen foto');
|
||||
});
|
||||
});
|
||||
|
||||
describe('BulkActionBar', () => {
|
||||
it('shows nothing without a selection, then the count and the actions, and clears the selection', async () => {
|
||||
render(<Harness />);
|
||||
expect(screen.queryByTestId('bulk-action-bar')).not.toBeInTheDocument();
|
||||
|
||||
await userEvent.click(screen.getByRole('checkbox', { name: 'Foto 1' }));
|
||||
expect(screen.getByRole('toolbar', { name: 'Acties op de selectie' })).toBeInTheDocument();
|
||||
expect(screen.getByTestId('bulk-action-delete')).toHaveTextContent('Verwijderen');
|
||||
|
||||
await userEvent.click(screen.getByTestId('bulk-clear'));
|
||||
expect(screen.queryByTestId('bulk-action-bar')).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('checkbox', { name: 'Foto 1' })).not.toBeChecked();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
import { useRef } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
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;
|
||||
}
|
||||
|
||||
function formatBytes(bytes: number, language: string) {
|
||||
const units = ['B', 'KB', 'MB', 'GB'];
|
||||
let value = bytes;
|
||||
let unit = 0;
|
||||
while (value >= 1024 && unit < units.length - 1) {
|
||||
value /= 1024;
|
||||
unit++;
|
||||
}
|
||||
return `${new Intl.NumberFormat(language, { maximumFractionDigits: unit === 0 ? 0 : 1 }).format(value)} ${units[unit]}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { emptySelection, prune, selectAll, selectRange, stateOf, toggle } from './selection';
|
||||
|
||||
const ids = ['a', 'b', 'c', 'd', 'e'];
|
||||
|
||||
describe('selection rules', () => {
|
||||
it('toggles one photo on and off without changing the original set', () => {
|
||||
const one = toggle(emptySelection, 'b');
|
||||
expect([...one]).toEqual(['b']);
|
||||
expect([...toggle(one, 'b')]).toEqual([]);
|
||||
expect(emptySelection.size).toBe(0);
|
||||
});
|
||||
|
||||
it('selects a range from the anchor to the target, in either direction', () => {
|
||||
expect([...selectRange(emptySelection, ids, 'b', 'd', true)].sort()).toEqual(['b', 'c', 'd']);
|
||||
expect([...selectRange(emptySelection, ids, 'd', 'b', true)].sort()).toEqual(['b', 'c', 'd']);
|
||||
});
|
||||
|
||||
it('clears a range when the target is switched off', () => {
|
||||
const all = selectAll(ids);
|
||||
expect([...selectRange(all, ids, 'a', 'c', false)].sort()).toEqual(['d', 'e']);
|
||||
});
|
||||
|
||||
it('only changes the target without an anchor or with an anchor that is gone', () => {
|
||||
expect([...selectRange(emptySelection, ids, null, 'c', true)]).toEqual(['c']);
|
||||
expect([...selectRange(emptySelection, ids, 'zzz', 'c', true)]).toEqual(['c']);
|
||||
});
|
||||
|
||||
it('ignores a target that is not in the list', () => {
|
||||
const selection = new Set(['a']);
|
||||
expect(selectRange(selection, ids, 'a', 'nope', true)).toBe(selection);
|
||||
});
|
||||
|
||||
it('reports none, some and all', () => {
|
||||
expect(stateOf(emptySelection, ids)).toBe('none');
|
||||
expect(stateOf(new Set(['a']), ids)).toBe('some');
|
||||
expect(stateOf(selectAll(ids), ids)).toBe('all');
|
||||
expect(stateOf(selectAll(ids), [])).toBe('none');
|
||||
});
|
||||
|
||||
it('drops ids that left the list and keeps the same object when nothing changes', () => {
|
||||
const selection = new Set(['a', 'x']);
|
||||
expect([...prune(selection, ids)]).toEqual(['a']);
|
||||
const clean = new Set(['a']);
|
||||
expect(prune(clean, ids)).toBe(clean);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* The selection of photos in the library, as pure functions over a set of ids. The list component keeps the state; these
|
||||
* functions are the rules (toggle, range with shift-click, all, none), so they are easy to test.
|
||||
*/
|
||||
|
||||
export type Selection = ReadonlySet<string>;
|
||||
|
||||
export const emptySelection: Selection = new Set();
|
||||
|
||||
export function selectAll(ids: readonly string[]): Selection {
|
||||
return new Set(ids);
|
||||
}
|
||||
|
||||
export function toggle(selection: Selection, id: string): Selection {
|
||||
const next = new Set(selection);
|
||||
if (!next.delete(id)) next.add(id);
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shift-click: every photo between the anchor (the last one clicked) and the target gets the state of the target's new
|
||||
* value. `ids` is the displayed order. Without a known anchor, or an anchor that is no longer in the list, only the
|
||||
* target changes.
|
||||
*/
|
||||
export function selectRange(selection: Selection, ids: readonly string[], anchor: string | null, target: string, value: boolean): Selection {
|
||||
const to = ids.indexOf(target);
|
||||
if (to < 0) return selection;
|
||||
const from = anchor === null ? -1 : ids.indexOf(anchor);
|
||||
const [start, end] = from < 0 ? [to, to] : [Math.min(from, to), Math.max(from, to)];
|
||||
const next = new Set(selection);
|
||||
for (const id of ids.slice(start, end + 1)) {
|
||||
if (value) next.add(id);
|
||||
else next.delete(id);
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
/** Drops ids that are no longer in the list (after a delete or a filter change). */
|
||||
export function prune(selection: Selection, ids: readonly string[]): Selection {
|
||||
const visible = new Set(ids);
|
||||
const next = new Set([...selection].filter((id) => visible.has(id)));
|
||||
return next.size === selection.size ? selection : next;
|
||||
}
|
||||
|
||||
export type SelectionState = 'none' | 'some' | 'all';
|
||||
|
||||
export function stateOf(selection: Selection, ids: readonly string[]): SelectionState {
|
||||
if (ids.length === 0) return 'none';
|
||||
const count = ids.filter((id) => selection.has(id)).length;
|
||||
return count === 0 ? 'none' : count === ids.length ? 'all' : 'some';
|
||||
}
|
||||
Reference in New Issue
Block a user