Mediascherm: paneel Controle bibliotheek (read-check in gewone taal, stand van de schakelaars)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 23:36:05 +01:00
co-authored by Claude Sonnet 5.5
parent 30599c60ee
commit e1f2abf004
16 changed files with 237 additions and 6 deletions
@@ -0,0 +1,70 @@
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { CheckCircle2, ListChecks, TriangleAlert } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { useReadCheck } from '../services/hooks';
/**
* "Library check": a small panel for administrators that compares the library's own tables with the modules (the hero) in
* plain words and shows the state of the migration switches (copy, write-through, reading). It only asks the server when the
* button is clicked; it changes nothing.
*/
export function LibraryCheck() {
const { t } = useTranslation();
const [asked, setAsked] = useState(false);
const check = useReadCheck(asked);
return (
<section className="space-y-2 rounded-md border p-3" aria-labelledby="library-check-title" data-testid="library-check">
<div className="flex flex-wrap items-center justify-between gap-2">
<h2 id="library-check-title" className="text-sm font-medium">
{t('media.check.title')}
</h2>
<Button type="button" variant="outline" size="sm" disabled={check.isFetching} onClick={() => (asked ? void check.refetch() : setAsked(true))} data-testid="library-check-run">
<ListChecks className="mr-1.5 size-4" aria-hidden="true" />
{check.isFetching ? t('media.check.running') : t('media.check.run')}
</Button>
</div>
<p className="text-xs text-muted-foreground">{t('media.check.hint')}</p>
{check.isError && (
<p role="alert" className="text-sm text-destructive" data-testid="library-check-error">
{t('media.check.error')}
</p>
)}
{check.data && (
<div className="space-y-2" role="status" data-testid="library-check-result">
<p className="text-sm" data-testid="library-check-switches">
{t('media.check.switches', {
copy: check.data.copyOnStartup ? t('media.check.on') : t('media.check.off'),
write: check.data.dualWrite ? t('media.check.on') : t('media.check.off'),
read: t(`media.check.read.${check.data.mode}`),
})}
</p>
{check.data.modules.length === 0 && <p className="text-sm text-muted-foreground">{t('media.check.noModules')}</p>}
{check.data.modules.map((module) => (
<div key={module.module} data-testid={`library-check-${module.module}`}>
{module.matches ? (
<p className="flex items-center gap-2 text-sm text-green-700 dark:text-green-400">
<CheckCircle2 className="size-4 shrink-0" aria-hidden="true" />
{t('media.check.matches', { module: t(`media.module.${module.module}`, { defaultValue: module.module }), photos: module.photos, uses: module.uses })}
</p>
) : (
<div className="space-y-1">
<p className="flex items-center gap-2 text-sm font-medium text-destructive">
<TriangleAlert className="size-4 shrink-0" aria-hidden="true" />
{t('media.check.differs', { module: t(`media.module.${module.module}`, { defaultValue: module.module }), count: module.differences.length })}
</p>
<ul className="list-disc space-y-0.5 pl-6 text-xs" data-testid={`library-check-${module.module}-differences`}>
{module.differences.map((difference) => (
<li key={difference}>{difference}</li>
))}
</ul>
</div>
)}
</div>
))}
</div>
)}
</section>
);
}
@@ -1,5 +1,5 @@
import { http, HttpResponse } from 'msw';
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types';
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types';
const daysAgo = (days: number) => new Date(Date.now() - days * 86_400_000).toISOString();
@@ -81,6 +81,9 @@ export const mediaHandlers = [
...mediaTrashHandlers,
http.get('*/media-library/photos', () => HttpResponse.json({ photos: seedPhotos(), modules: ['hero'] } satisfies MediaLibraryDto)),
http.get('*/media-library/storage', () => HttpResponse.json(seedStorage())),
http.get('*/media-library/read-check', () =>
HttpResponse.json({ mode: 'off', modules: [{ module: 'hero', mode: 'off', photos: 3, uses: 2, matches: true, differences: [] }], copyOnStartup: false, dualWrite: false } satisfies MediaReadCheckResponse),
),
http.get('*/media-library/cleanup-preview', ({ request }) => {
const url = new URL(request.url);
const days = Number(url.searchParams.get('days') ?? 7);
@@ -339,6 +339,52 @@ describe('MediaLibraryPage', () => {
expect(within(dialog).getByTestId('cleanup-confirm')).toBeInTheDocument();
});
it('checks the library against the hero in plain words, with the state of the switches, only when asked', async () => {
let calls = 0;
server.use(
http.get('*/media-library/read-check', () => {
calls += 1;
return HttpResponse.json({ mode: 'compare', modules: [{ module: 'hero', mode: 'compare', photos: 11, uses: 8, matches: true, differences: [] }], copyOnStartup: true, dualWrite: true });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
expect(calls).toBe(0);
expect(screen.queryByTestId('library-check-result')).not.toBeInTheDocument();
await userEvent.click(screen.getByTestId('library-check-run'));
expect(await screen.findByTestId('library-check-hero')).toHaveTextContent('De bibliotheek komt overeen met Hero (11 foto');
expect(screen.getByTestId('library-check-switches')).toHaveTextContent('kopie bij het opstarten: aan; schrijven naar de bibliotheek: aan; lezen uit de bibliotheek: vergelijken (alleen loggen)');
expect(calls).toBe(1);
});
it('lists the differences when the library does not match', async () => {
server.use(
http.get('*/media-library/read-check', () =>
HttpResponse.json({ mode: 'off', modules: [{ module: 'hero', mode: 'off', photos: 3, uses: 2, matches: false, differences: ['photo 1 is missing in the library', 'use 2 differs'] }], copyOnStartup: false, dualWrite: false }),
),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('library-check-run'));
expect(await screen.findByTestId('library-check-hero')).toHaveTextContent('De bibliotheek wijkt af van Hero: 2 verschillen.');
expect(within(screen.getByTestId('library-check-hero-differences')).getAllByRole('listitem')).toHaveLength(2);
expect(screen.getByTestId('library-check-switches')).toHaveTextContent('kopie bij het opstarten: uit; schrijven naar de bibliotheek: uit; lezen uit de bibliotheek: uit');
});
it('says so when the check cannot be run', async () => {
server.use(http.get('*/media-library/read-check', () => new HttpResponse(null, { status: 500 })));
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('library-check-run'));
expect(await screen.findByTestId('library-check-error')).toHaveTextContent('De controle kon niet worden uitgevoerd.');
});
it('shows an error when the library cannot be loaded', async () => {
server.use(http.get('*/media-library/photos', () => new HttpResponse(null, { status: 500 })));
renderPage();
@@ -7,6 +7,7 @@ import { CleanupDialog } from '../components/CleanupDialog';
import { DeleteDialog } from '../components/DeleteDialog';
import { PurgeDialog } from '../components/PurgeDialog';
import { PhotoSelectionList, type PhotoUse } from '../components/PhotoSelectionList';
import { LibraryCheck } from '../components/LibraryCheck';
import { StorageMeter } from '../components/StorageMeter';
import { useLayoutPreference } from '../lib/layoutPreference';
import { emptySelection, prune, type Selection } from '../lib/selection';
@@ -90,6 +91,7 @@ export function MediaLibraryPage() {
</div>
{storage.data && <StorageMeter storage={storage.data} />}
<LibraryCheck />
<div className="flex gap-2" role="group" aria-label={t('media.view.label')}>
{(['library', 'trash'] as const).map((value) => (
+12 -1
View File
@@ -1,6 +1,6 @@
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { api } from '@/lib/api-client';
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types';
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types';
export const MEDIA_KEY = ['media-library'];
@@ -22,6 +22,17 @@ export function useMediaStorage() {
});
}
/** Compares the library tables with the modules and tells the state of the migration switches. Only asked for when the person clicks. */
export function useReadCheck(enabled: boolean) {
return useQuery<MediaReadCheckResponse, Error>({
queryKey: [...MEDIA_KEY, 'read-check'],
queryFn: () => api.get<MediaReadCheckResponse>('/api/v1/media-library/read-check'),
enabled,
staleTime: 0,
gcTime: 0,
});
}
/** What a clean-up with these settings would select. A preview: nothing is deleted. */
export function useCleanupPreview(days: number, onlyUnused: boolean, enabled: boolean) {
return useQuery<MediaCleanupPreviewDto, Error>({
@@ -29,6 +29,23 @@ export interface MediaLibraryDto {
export type MediaStorageLevel = 'ok' | 'warning' | 'full';
export interface MediaReadCheckModuleDto {
module: string;
mode: string;
photos: number;
uses: number;
matches: boolean;
differences: string[];
}
/** The check of the library tables against the modules, plus the state of the migration switches. */
export interface MediaReadCheckResponse {
mode: 'off' | 'compare' | 'on';
modules: MediaReadCheckModuleDto[];
copyOnStartup: boolean;
dualWrite: boolean;
}
export interface MediaStorageDto {
usedBytes: number;
/** 0 means no limit. */