Mediabibliotheek: gebruik-link via de router (blijft onder /admin) en opent de Hero op het tabblad Afbeeldingen
Continuous Integration / config-asf (pull_request) Successful in 7s
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 12s
Continuous Integration / compile-sites (pull_request) Successful in 1m53s
Continuous Integration / backend-build (pull_request) Successful in 1m54s
Continuous Integration / vulnerability-scan (pull_request) Successful in 2m22s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m46s
Continuous Integration / frontend-build (pull_request) Successful in 1m8s
Continuous Integration / backend-test (pull_request) Successful in 3m8s
Continuous Integration / frontend-test (pull_request) Successful in 2m48s
Continuous Integration / frontend-lint (pull_request) Successful in 3m26s
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>
This commit is contained in:
2026-11-17 16:38:28 +01:00
co-authored by Claude Sonnet 5.5
parent 4ae1789575
commit 8ad071da1c
4 changed files with 82 additions and 33 deletions
@@ -1,8 +1,8 @@
import { useRef } from 'react';
import { useRef, type ReactNode } from 'react';
import { useTranslation } from 'react-i18next';
import { Checkbox } from '@/components/ui/checkbox';
import { formatBytes } from '../lib/format';
import { usageHref, usageLabel } from '../lib/usageLabel';
import { usageLabel } from '../lib/usageLabel';
import { selectAll, selectRange, stateOf, type Selection } from '../lib/selection';
export interface PhotoUse {
@@ -33,13 +33,15 @@ interface PhotoSelectionListProps {
onSelectionChange: (selection: Selection) => void;
/** Photos that cannot be selected (for example in use during a clean-up). */
isDisabled?: (item: PhotoItem) => boolean;
/** How a use is shown (the page makes it a router link to the module). Default: the plain label. */
renderUse?: (use: PhotoUse, label: string) => ReactNode;
}
/**
* 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) {
export function PhotoSelectionList({ items, selection, onSelectionChange, isDisabled, renderUse }: PhotoSelectionListProps) {
const { t, i18n } = useTranslation();
const anchor = useRef<string | null>(null);
const selectable = items.filter((item) => !isDisabled?.(item));
@@ -115,17 +117,10 @@ export function PhotoSelectionList({ items, selection, onSelectionChange, isDisa
<span>
{item.uses.map((use, index) => {
const label = usageLabel(t, use);
const href = usageHref(use);
return (
<span key={`${use.module}-${use.kind}-${use.position ?? index}`}>
{index > 0 && '; '}
{href ? (
<a href={href} className="underline underline-offset-2 hover:text-primary" data-testid={`photo-use-link-${item.id}`}>
{label}
</a>
) : (
label
)}
{renderUse ? renderUse(use, label) : label}
</span>
);
})}
+17 -2
View File
@@ -14,7 +14,22 @@ export function usageLabel(t: TFunction, use: UsePlace): string {
return `${t(`media.module.${use.module}`, { defaultValue: use.module })}, ${where}${use.active ? '' : ` (${t('media.use.off')})`}`;
}
/** Where the admin screen of the module that uses the photo lives (null when there is none). */
export function usageHref(use: Pick<UsePlace, 'module'>): string | null {
/**
* The admin route of the module that uses the photo (null when there is none). It is a ROUTER path: the router adds the
* base path of the admin app (/admin) when it makes the link, so it never goes outside the admin.
*/
export function usageTarget(use: Pick<UsePlace, 'module'>): '/hero' | null {
return use.module === 'hero' ? '/hero' : null;
}
/**
* The hero screen opens on the tab it was left on; a use is a slide, so the link leaves it on the images tab.
* (Storage can be blocked: then the screen simply opens on its own default.)
*/
export function rememberHeroImagesTab(): void {
try {
window.sessionStorage.setItem('cms-hero-tab', 'images');
} catch {
// Not available: nothing to remember.
}
}
@@ -5,16 +5,30 @@ import userEvent from '@testing-library/user-event';
import i18n from '@/i18n/config';
import { server } from '@/mocks/server';
import { renderWithProviders } from '@/test/utils';
import { createMemoryHistory, createRootRoute, createRoute, createRouter, RouterProvider } from '@tanstack/react-router';
import { seedPhotos, seedStorage } from '../mocks/handlers';
import { MediaLibraryPage } from './MediaLibraryPage';
/** The page inside a router with the same base path as the real admin app (/admin), and a stub hero route to land on. */
function renderPage() {
const root = createRootRoute();
const media = createRoute({ getParentRoute: () => root, path: '/media', component: MediaLibraryPage });
const hero = createRoute({ getParentRoute: () => root, path: '/hero', component: () => <p data-testid="hero-stub">hero</p> });
const router = createRouter({
routeTree: root.addChildren([media, hero]),
basepath: '/admin',
history: createMemoryHistory({ initialEntries: ['/admin/media'] }),
});
return renderWithProviders(<RouterProvider router={router} />);
}
beforeEach(async () => {
await i18n.changeLanguage('nl');
});
describe('MediaLibraryPage', () => {
it('lists the photos newest first with where each is used, and the storage', async () => {
renderWithProviders(<MediaLibraryPage />);
renderPage();
const list = await screen.findByTestId('photo-selection-list');
const rows = within(list).getAllByRole('listitem');
@@ -28,17 +42,17 @@ describe('MediaLibraryPage', () => {
it('warns when the storage is nearly full and says so when it is full', async () => {
server.use(http.get('*/media-library/storage', () => HttpResponse.json({ ...seedStorage(), usedBytes: 1_800_000_000, level: 'warning' })));
const { unmount } = renderWithProviders(<MediaLibraryPage />);
const { unmount } = renderPage();
expect(await screen.findByTestId('storage-warning')).toHaveTextContent('80%');
unmount();
server.use(http.get('*/media-library/storage', () => HttpResponse.json({ ...seedStorage(), usedBytes: 2_147_483_648, level: 'full' })));
renderWithProviders(<MediaLibraryPage />);
renderPage();
expect(await screen.findByTestId('storage-warning')).toHaveTextContent('vol');
});
it('selects photos and shows the bulk bar with the delete action', async () => {
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByRole('checkbox', { name: 'zomer.jpg' }));
@@ -48,7 +62,7 @@ describe('MediaLibraryPage', () => {
});
it('opens the clean-up: older than 7 days, only unused, all selected, newest first, and nothing is deleted', async () => {
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('cleanup-open'));
@@ -63,7 +77,7 @@ describe('MediaLibraryPage', () => {
});
it('includes photos in use when "only unused" is switched off', async () => {
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('cleanup-open'));
const dialog = await screen.findByTestId('cleanup-dialog');
@@ -83,7 +97,7 @@ describe('MediaLibraryPage', () => {
return HttpResponse.json({ results: body.ids.map((id) => ({ id, status: 'trashed', uses: [] })) });
}),
);
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('photo-select-hero:22222222-2222-2222-2222-222222222222'));
@@ -103,7 +117,7 @@ describe('MediaLibraryPage', () => {
return HttpResponse.json({ results: body.ids.map((id) => ({ id, status: 'trashed', uses: [] })) });
}),
);
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('photo-select-hero:11111111-1111-1111-1111-111111111111'));
@@ -125,7 +139,7 @@ describe('MediaLibraryPage', () => {
return HttpResponse.json({ results: ids.map((id, i) => ({ id, status: i === 0 ? 'failed' : 'trashed', uses: [] })) });
}),
);
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('photo-select-hero:22222222-2222-2222-2222-222222222222'));
@@ -146,7 +160,7 @@ describe('MediaLibraryPage', () => {
return HttpResponse.json({ results: restored.map((id) => ({ id, status: 'restored', uses: [] })) });
}),
);
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('view-trash'));
@@ -165,7 +179,7 @@ describe('MediaLibraryPage', () => {
return HttpResponse.json({ results: body.ids.map((id) => ({ id, status: 'trashed', uses: [] })) });
}),
);
renderWithProviders(<MediaLibraryPage />);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('cleanup-open'));
const dialog = await screen.findByTestId('cleanup-dialog');
@@ -177,8 +191,8 @@ describe('MediaLibraryPage', () => {
await waitFor(() => expect(body).toEqual({ ids: ['hero:22222222-2222-2222-2222-222222222222'], confirmedInUse: false }));
});
it('filters the library on used and unused photos and links a use to its module', async () => {
renderWithProviders(<MediaLibraryPage />);
it('filters the library on used and unused photos', async () => {
renderPage();
const list = await screen.findByTestId('photo-selection-list');
expect(within(list).getAllByRole('listitem')).toHaveLength(3);
@@ -186,14 +200,25 @@ describe('MediaLibraryPage', () => {
expect(within(screen.getByTestId('photo-selection-list')).getAllByRole('listitem')).toHaveLength(1);
await userEvent.click(screen.getByTestId('filter-used'));
const used = within(screen.getByTestId('photo-selection-list')).getAllByRole('listitem');
expect(used).toHaveLength(2);
expect(within(used[0]!).getByRole('link', { name: 'Hero, slide 1' })).toHaveAttribute('href', '/hero');
expect(within(screen.getByTestId('photo-selection-list')).getAllByRole('listitem')).toHaveLength(2);
});
it('links a use to the hero under the admin base path (/admin/hero, not /hero) and opens its images tab', async () => {
window.sessionStorage.removeItem('cms-hero-tab');
renderPage();
const list = await screen.findByTestId('photo-selection-list');
const link = within(within(list).getAllByRole('listitem')[0]!).getByRole('link', { name: 'Hero, slide 1' });
expect(link).toHaveAttribute('href', '/admin/hero');
await userEvent.click(link);
expect(await screen.findByTestId('hero-stub')).toBeInTheDocument();
expect(window.sessionStorage.getItem('cms-hero-tab')).toBe('images');
});
it('shows an error when the library cannot be loaded', async () => {
server.use(http.get('*/media-library/photos', () => new HttpResponse(null, { status: 500 })));
renderWithProviders(<MediaLibraryPage />);
renderPage();
expect(await screen.findByTestId('media-error')).toBeInTheDocument();
});
@@ -1,16 +1,30 @@
import { useMemo, useState } from 'react';
import { useMemo, useState, type ReactNode } from 'react';
import { Link } from '@tanstack/react-router';
import { useTranslation } from 'react-i18next';
import { Eraser, RotateCcw, Trash2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { BulkActionBar } from '../components/BulkActionBar';
import { CleanupDialog } from '../components/CleanupDialog';
import { DeleteDialog } from '../components/DeleteDialog';
import { PhotoSelectionList } from '../components/PhotoSelectionList';
import { PhotoSelectionList, type PhotoUse } from '../components/PhotoSelectionList';
import { StorageMeter } from '../components/StorageMeter';
import { emptySelection, prune, type Selection } from '../lib/selection';
import { rememberHeroImagesTab, usageTarget } from '../lib/usageLabel';
import { toPhotoItem } from '../lib/toPhotoItem';
import { useMediaPhotos, useMediaStorage, useMediaTrash, useRestorePhotos } from '../services/hooks';
/** A use becomes a link to the screen of its module, made by the router (so it stays under the admin base path). */
function renderUse(use: PhotoUse, label: string): ReactNode {
const target = usageTarget(use);
return target ? (
<Link to={target} onClick={rememberHeroImagesTab} className="underline underline-offset-2 hover:text-primary" data-testid="photo-use-link">
{label}
</Link>
) : (
label
);
}
type View = 'library' | 'trash';
type UseFilter = 'all' | 'used' | 'unused';
@@ -106,7 +120,7 @@ export function MediaLibraryPage() {
{t('media.page.noModules')}
</p>
)}
{source.data && <PhotoSelectionList items={items} selection={selection} onSelectionChange={setSelection} />}
{source.data && <PhotoSelectionList items={items} selection={selection} onSelectionChange={setSelection} renderUse={renderUse} />}
<BulkActionBar count={selection.size} actions={actions} onClear={() => setSelection(emptySelection)} />
<CleanupDialog open={cleanupOpen} onOpenChange={setCleanupOpen} />