diff --git a/frontend/src/features/media/components/PhotoSelectionList.tsx b/frontend/src/features/media/components/PhotoSelectionList.tsx index 3454204..32c2613 100644 --- a/frontend/src/features/media/components/PhotoSelectionList.tsx +++ b/frontend/src/features/media/components/PhotoSelectionList.tsx @@ -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(null); const selectable = items.filter((item) => !isDisabled?.(item)); @@ -115,17 +117,10 @@ export function PhotoSelectionList({ items, selection, onSelectionChange, isDisa {item.uses.map((use, index) => { const label = usageLabel(t, use); - const href = usageHref(use); return ( {index > 0 && '; '} - {href ? ( - - {label} - - ) : ( - label - )} + {renderUse ? renderUse(use, label) : label} ); })} diff --git a/frontend/src/features/media/lib/usageLabel.ts b/frontend/src/features/media/lib/usageLabel.ts index d2e32c2..68a6982 100644 --- a/frontend/src/features/media/lib/usageLabel.ts +++ b/frontend/src/features/media/lib/usageLabel.ts @@ -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): 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): '/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. + } +} diff --git a/frontend/src/features/media/pages/MediaLibraryPage.test.tsx b/frontend/src/features/media/pages/MediaLibraryPage.test.tsx index 8951820..48cf39a 100644 --- a/frontend/src/features/media/pages/MediaLibraryPage.test.tsx +++ b/frontend/src/features/media/pages/MediaLibraryPage.test.tsx @@ -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: () =>

hero

}); + const router = createRouter({ + routeTree: root.addChildren([media, hero]), + basepath: '/admin', + history: createMemoryHistory({ initialEntries: ['/admin/media'] }), + }); + return renderWithProviders(); +} + beforeEach(async () => { await i18n.changeLanguage('nl'); }); describe('MediaLibraryPage', () => { it('lists the photos newest first with where each is used, and the storage', async () => { - renderWithProviders(); + 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(); + 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(); + renderPage(); expect(await screen.findByTestId('storage-warning')).toHaveTextContent('vol'); }); it('selects photos and shows the bulk bar with the delete action', async () => { - renderWithProviders(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + renderPage(); expect(await screen.findByTestId('media-error')).toBeInTheDocument(); }); diff --git a/frontend/src/features/media/pages/MediaLibraryPage.tsx b/frontend/src/features/media/pages/MediaLibraryPage.tsx index 7ff88e7..a8ce2d8 100644 --- a/frontend/src/features/media/pages/MediaLibraryPage.tsx +++ b/frontend/src/features/media/pages/MediaLibraryPage.tsx @@ -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 ? ( + + {label} + + ) : ( + label + ); +} + type View = 'library' | 'trash'; type UseFilter = 'all' | 'used' | 'unused'; @@ -106,7 +120,7 @@ export function MediaLibraryPage() { {t('media.page.noModules')}

)} - {source.data && } + {source.data && } setSelection(emptySelection)} />