From a5c6ec4bcdc02dd012c73f152369ff8572ccf10d Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 23 Nov 2026 18:05:30 +0100 Subject: [PATCH] feat(app): back to the parent view in the bilan detail window --- apps/app/src/app/routes/EditorRoute.tsx | 41 ++-- .../app/src/components/canvas/DetailModal.tsx | 50 ++++- apps/app/src/components/ui/Dialog.tsx | 4 + apps/app/src/components/ui/Icon.tsx | 18 +- apps/app/src/i18n/en.ts | 1 + apps/app/src/i18n/fr.ts | 1 + .../tests/unit/routes.EditorRoute.test.tsx | 177 ++++++++++++++++-- apps/app/tests/unit/ui.Dialog.test.tsx | 24 +++ 8 files changed, 281 insertions(+), 35 deletions(-) diff --git a/apps/app/src/app/routes/EditorRoute.tsx b/apps/app/src/app/routes/EditorRoute.tsx index c21b07e5..7a95bd5a 100644 --- a/apps/app/src/app/routes/EditorRoute.tsx +++ b/apps/app/src/app/routes/EditorRoute.tsx @@ -14,7 +14,9 @@ import EditInfosDialog, { type Identity, } from '@/components/report/EditInfosDialog' import DisclaimerDialog from '@/components/report/DisclaimerDialog' -import CoreStoppedNotice, { CopyReportButton } from '@/components/report/CoreStoppedNotice' +import CoreStoppedNotice, { + CopyReportButton, +} from '@/components/report/CoreStoppedNotice' import CoreUnavailable from '@/components/report/CoreUnavailable' import HeldSessionPanel from '@/components/report/HeldSessionPanel' import Notice from '@/components/ui/Notice' @@ -34,10 +36,7 @@ import { pickLabel } from '@/i18n/pickLocaleLabel' import type { ViewStates } from '@/anatomy/drawing' import { exportReportToPdf } from '@/pdf/export' import { downloadBlob } from '@/lib/download' -import type { - ExportWarning, - RebaseOutcome, -} from '@faunosteo/core-wasm/types' +import type { ExportWarning, RebaseOutcome } from '@faunosteo/core-wasm/types' import { useT } from '@/i18n/useT' import { DEFAULT_SPECIES_ID } from './NewReportRoute' import { coreFailureMessage } from '@/core/failure' @@ -79,13 +78,12 @@ export default function EditorRoute() { const sheet = useSheet(report?.animal.speciesId ?? DEFAULT_SPECIES_ID) const stopped = useCoreStopped() - const [detailViewId, setDetailViewId] = useState(null) + // The views of the detail window, the one shown last; empty while it is closed. + const [detailStack, setDetailStack] = useState([]) const [vertebraTarget, setVertebraTarget] = useState( null, ) - const [lateralTarget, setLateralTarget] = useState( - null, - ) + const [lateralTarget, setLateralTarget] = useState(null) // The core failed while reading the draft: stay here, keep the draft, offer a retry. const [restoreFailed, setRestoreFailed] = useState(false) // Texts that could not be set aside: the resolution panel replaces the editor. @@ -166,7 +164,7 @@ export default function EditorRoute() { }, [activeObs]) const onZoneClick = useCallback( - (annotationId: string) => { + (annotationId: string, fromViewId: string) => { // Fresh state: two clicks may land before React re-renders the handler. const { report, activeObservationIndex: activeIndex } = useReportStore.getState() @@ -176,10 +174,18 @@ export default function EditorRoute() { const outcome = sheet.index.ops.zoneClick(activeObs ?? null, annotationId) if (!outcome.ok) return window.alert(coreFailureMessage(t)) const click = outcome.value - if (click.observation) store.updateObservation(activeIndex, click.observation) + if (click.observation) + store.updateObservation(activeIndex, click.observation) switch (click.kind) { case 'open_view': - return setDetailViewId(click.viewId ?? null) + // From inside the window the view is pushed; from the page it starts a stack. + return setDetailStack((stack) => + !click.viewId + ? [] + : stack[stack.length - 1] === fromViewId + ? [...stack, click.viewId] + : [click.viewId], + ) case 'activate_fallback': return store.setActiveObservation(report.observations.length - 1) case 'open_vertebra_picker': @@ -302,7 +308,10 @@ export default function EditorRoute() { } if (sheet.status === 'none') return null const index = sheet.index - const detailView = detailViewId ? index.view(detailViewId) : undefined + const detailId = detailStack[detailStack.length - 1] + const detailView = detailId ? index.view(detailId) : undefined + const parentId = detailStack[detailStack.length - 2] + const parentView = parentId ? index.view(parentId) : undefined return (
@@ -385,7 +394,11 @@ export default function EditorRoute() { {detailView && ( setDetailViewId(null)} + onClose={() => setDetailStack([])} + backTo={ + parentView ? { name: pickLabel(parentView.name, locale) } : null + } + onBack={() => setDetailStack((stack) => stack.slice(0, -1))} index={index} viewId={detailView.id} title={pickLabel(detailView.name, locale)} diff --git a/apps/app/src/components/canvas/DetailModal.tsx b/apps/app/src/components/canvas/DetailModal.tsx index 3bbe1dae..0aea5837 100644 --- a/apps/app/src/components/canvas/DetailModal.tsx +++ b/apps/app/src/components/canvas/DetailModal.tsx @@ -1,7 +1,10 @@ +import { useEffect, useRef } from 'react' import Dialog from '@/components/ui/Dialog' import AnatomyCanvas from './AnatomyCanvas' import type { ViewStates } from '@/anatomy/drawing' import type { SheetIndex } from '@/services/sheetIndex' +import Button from '@/components/ui/Button' +import Icon from '@/components/ui/Icon' import { useT } from '@/i18n/useT' export interface DetailModalProps { @@ -12,8 +15,18 @@ export interface DetailModalProps { title: string states: ViewStates onZoneClick: (annotationId: string, viewId: string) => void + /** The parent view when the window holds a view opened from another one. */ + backTo: { name: string } | null + onBack: () => void } +const TITLE_ID = 'detail-modal-title' + +/** The drawing sits in the middle of its area, not at the top (the host is a flex row). */ +const AREA = + 'aspect-[4/3] w-full [&_.svg-canvas>div:first-child]:flex ' + + '[&_.svg-canvas>div:first-child]:items-center' + /** A detail view of the sheet (a crop or its own drawing) in a dialog. */ export default function DetailModal({ open, @@ -23,12 +36,45 @@ export default function DetailModal({ title, states, onZoneClick, + backTo, + onBack, }: DetailModalProps) { const t = useT() + const focusTitle = useRef(false) + // After a back the button is gone: the focus goes to the window's title. + useEffect(() => { + if (!focusTitle.current) return + focusTitle.current = false + document.getElementById(TITLE_ID)?.focus() + }, [viewId]) + const back = () => { + focusTitle.current = true + onBack() + } return ( - -
+ + + {backTo.name} + + ) + } + > +
+ {headerStart}

= { close: 'M6 6l12 12M18 6L6 18', dashboard: 'M4 4h7v7H4zM13 4h7v4h-7zM13 11h7v9h-7zM4 14h7v6H4z', user: 'M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM4 21a8 8 0 0 1 16 0', - users: 'M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM2 21a7 7 0 0 1 14 0M16 3.5a4 4 0 0 1 0 7M22 21a7 7 0 0 0-4.5-6.5', + users: + 'M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM2 21a7 7 0 0 1 14 0M16 3.5a4 4 0 0 1 0 7M22 21a7 7 0 0 0-4.5-6.5', paw: 'M3.7 10.5a1.8 1.8 0 1 0 3.6 0 1.8 1.8 0 1 0-3.6 0M7.1 6.5a1.9 1.9 0 1 0 3.8 0 1.9 1.9 0 1 0-3.8 0M13.1 6.5a1.9 1.9 0 1 0 3.8 0 1.9 1.9 0 1 0-3.8 0M16.7 10.5a1.8 1.8 0 1 0 3.6 0 1.8 1.8 0 1 0-3.6 0M12 12.5c-2.5 0-5 3.2-5 5.3 0 1.6 1.3 2.4 2.8 2.4 1 0 1.5-.5 2.2-.5s1.2.5 2.2.5c1.5 0 2.8-.8 2.8-2.4 0-2.1-2.5-5.3-5-5.3z', book: 'M4 5a2 2 0 0 1 2-2h13v16H6a2 2 0 0 0-2 2zM4 21V5M8 7h7M8 11h5', shield: 'M12 3l8 3v6c0 4.5-3.4 8-8 9-4.6-1-8-4.5-8-9V6zM9 12l2 2 4-4', @@ -39,13 +41,16 @@ const PATHS: Record = { 'eye-off': 'M3 3l18 18M10.6 10.6A3 3 0 0 0 13.4 13.4M9.9 5.2A10.5 10.5 0 0 1 12 5c6 0 10 7 10 7a17 17 0 0 1-3.2 3.9M6.1 6.1A17 17 0 0 0 2 12s4 7 10 7a10 10 0 0 0 4-.8', check: 'M5 12l5 5L20 7', + 'arrow-left': 'M19 12H5M11 6l-6 6 6 6', 'chevron-right': 'M9 6l6 6-6 6', 'chevron-down': 'M6 9l6 6 6-6', - external: 'M14 4h6v6M20 4l-9 9M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5', + external: + 'M14 4h6v6M20 4l-9 9M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5', lock: 'M6 11h12v9H6zM8 11V8a4 4 0 0 1 8 0v3', 'wifi-off': 'M3 3l18 18M8.5 16.1a6 6 0 0 1 7 0M5 12.5a11 11 0 0 1 5.2-2.4M16.7 11a11 11 0 0 1 2.3 1.5M1.5 9a16 16 0 0 1 4.6-2.9M10.7 5a16 16 0 0 1 11.8 4M12 20h.01', - alert: 'M12 9v4M12 17h.01M10.3 3.9L2.8 17a2 2 0 0 0 1.7 3h15a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z', + alert: + 'M12 9v4M12 17h.01M10.3 3.9L2.8 17a2 2 0 0 0 1.7 3h15a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z', google: 'M21 12.2c0-.7-.1-1.3-.2-1.9H12v3.6h5.1a4.4 4.4 0 0 1-1.9 2.9v2.4h3.1c1.8-1.7 2.7-4.1 2.7-7zM12 21c2.4 0 4.5-.8 6-2.2l-3.1-2.4c-.8.6-1.8.9-2.9.9-2.3 0-4.2-1.5-4.9-3.6H4v2.5A9 9 0 0 0 12 21zM7.1 13.7a5.4 5.4 0 0 1 0-3.4V7.8H4a9 9 0 0 0 0 8.4zM12 6.6c1.3 0 2.5.5 3.4 1.3l2.6-2.6A9 9 0 0 0 4 7.8l3.1 2.5c.7-2.1 2.6-3.7 4.9-3.7z', apple: @@ -59,7 +64,12 @@ export interface IconProps extends SVGProps { size?: number } -export default function Icon({ name, size = 20, className, ...rest }: IconProps) { +export default function Icon({ + name, + size = 20, + className, + ...rest +}: IconProps) { const filled = FILLED.has(name) return ( { window.scrollTo = vi.fn() as unknown as typeof window.scrollTo vi.stubGlobal('alert', vi.fn()) useReportStore.setState({ report: null, activeObservationIndex: 0 }) - useReportStore.getState().hydrateImport({ - report: minimalReport(sheet, { - observations: [ - { - id: 'o1', - zones: [], - findings: [], - freeTextFindings: [], - comment: '', - }, - ], - }), - archive: null, - }, sheet) + useReportStore.getState().hydrateImport( + { + report: minimalReport(sheet, { + observations: [ + { + id: 'o1', + zones: [], + findings: [], + freeTextFindings: [], + comment: '', + }, + ], + }), + archive: null, + }, + sheet, + ) }) afterEach(() => { cleanup() @@ -249,3 +254,145 @@ describe('EditorRoute on the canonical sheet', () => { ) }) }) + +/** The reference sheet plus a view « Incisives » opened from the head (the board's panel 16). */ +async function withNestedView() { + const bundle = await bundledSheet() + const document = structuredClone(bundle.document) as { + views: unknown[] + annotations: unknown[] + } + document.views.push({ + id: 'incisors', + kind: 'detail', + parent_view_id: 'head', + name: { fr: 'Incisives', en: 'Incisors' }, + svg_asset_id: null, + view_box: { min_x: 300, min_y: 180, width: 200, height: 150 }, + }) + document.annotations.push({ + id: 'incisors-trigger', + kind: 'detail_trigger', + view_id: 'head', + category_id: 'articulations', + laterality: 'unpaired', + geometry: { + type: 'polygon', + points: [ + [330, 300], + [380, 300], + [380, 340], + [330, 340], + ], + }, + name: null, + opens_view_id: 'incisors', + }) + // Without a `fingerprint` block the reference is a historical one: read unverified, so an + // edited document loads (the frozen seed is never touched). + const ref = { ...bundle.ref, fingerprint: undefined } + const edited = { ...bundle, ref, document } + setSheetProvider({ getReferenceSheet: async () => edited }) +} + +describe('EditorRoute detail window stack (D15)', () => { + const HEAD = 'Tête (vue latérale gauche)' + const BACK = fr['detail.back'].replace('{name}', HEAD) + + beforeEach(withNestedView) + afterEach(() => setSheetProvider(bundledSheetProvider)) + + const bands = (dialog: HTMLElement) => + dialog.querySelectorAll('[class*="frame-band"]') + + async function openNested() { + await mount() + return pushNested() + } + + async function pushNested() { + clickZone('head-trigger') + const head = await screen.findByRole('dialog', { name: HEAD }) + await waitFor(() => + expect( + head.querySelector('[data-anno-id="incisors-trigger"]'), + ).not.toBeNull(), + ) + clickZone('incisors-trigger', head) + return screen.findByRole('dialog', { name: 'Incisives' }) + } + + it('pushes a nested view, shows the way back, and pops it with the focus on the title', async () => { + const user = userEvent.setup() + const dialog = await openNested() + const back = within(dialog).getByRole('button', { name: BACK }) + expect(back).toHaveTextContent('Tête (vue latérale gauche)') + await user.click(back) + const head = await screen.findByRole('dialog', { name: HEAD }) + expect(within(head).queryByRole('button', { name: BACK })).toBeNull() + expect(within(head).getByRole('heading', { name: HEAD })).toHaveFocus() + }) + + it('opening from the page starts a new stack, so a first view has no back button', async () => { + const user = userEvent.setup() + await openNested() + await user.keyboard('{Escape}') + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + clickZone('head-trigger') + const head = await screen.findByRole('dialog', { name: HEAD }) + expect(within(head).queryByRole('button', { name: /Revenir/ })).toBeNull() + }) + + it('Escape and Fermer close the whole window, never one view', async () => { + const user = userEvent.setup() + const dialog = await openNested() + await user.keyboard('{Escape}') + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + const again = await pushNested() + await user.click( + within(again).getByRole('button', { name: fr['common.close'] }), + ) + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + expect(dialog).not.toBeInTheDocument() + }) + + it('each view pushed or popped shows its own drawing and one band, none left over', async () => { + const user = userEvent.setup() + const canvasOf = (d: HTMLElement, viewId: string) => { + const canvases = d.querySelectorAll('[data-testid="anatomy-canvas"]') + expect(canvases).toHaveLength(1) + expect(canvases[0]).toHaveAttribute('data-view-id', viewId) + return canvases[0]! + } + const dialog = await openNested() + await waitFor(() => + expect(canvasOf(dialog, 'incisors').querySelector('svg')).not.toBeNull(), + ) + expect(bands(dialog)).toHaveLength(1) + expect( + canvasOf(dialog, 'incisors') + .querySelector('svg')! + .getAttribute('viewBox'), + ).toBe('300 180 200 150') + await user.click(within(dialog).getByRole('button', { name: BACK })) + const head = await screen.findByRole('dialog', { name: HEAD }) + await waitFor(() => + expect(canvasOf(head, 'head').querySelector('svg')).not.toBeNull(), + ) + expect(bands(head)).toHaveLength(1) + expect( + canvasOf(head, 'head').querySelector('svg')!.getAttribute('viewBox'), + ).toBe('60 100 500 380') + }) + + it('centres the drawing vertically in its area (flex, items-center)', async () => { + const dialog = await openNested() + const area = dialog.querySelector( + '[data-testid="anatomy-canvas"]', + )!.parentElement! + expect(area.className).toContain('[&_.svg-canvas>div:first-child]:flex') + expect(area.className).toContain( + '[&_.svg-canvas>div:first-child]:items-center', + ) + }) +}) diff --git a/apps/app/tests/unit/ui.Dialog.test.tsx b/apps/app/tests/unit/ui.Dialog.test.tsx index e85fc275..b7ddfe5a 100644 --- a/apps/app/tests/unit/ui.Dialog.test.tsx +++ b/apps/app/tests/unit/ui.Dialog.test.tsx @@ -81,6 +81,30 @@ describe('Dialog', () => { ) }) + it('renders headerStart before the title and leaves the ✕ unchanged', () => { + useLocaleStore.setState({ locale: 'fr' }) + render( + {}} + title="X" + headerStart={} + > + body + , + ) + const back = screen.getByRole('button', { name: 'Back' }) + const title = screen.getByRole('heading', { name: 'X' }) + expect( + back.compareDocumentPosition(title) & Node.DOCUMENT_POSITION_FOLLOWING, + ).toBeTruthy() + expect(screen.getAllByRole('button', { name: 'Fermer' })).toHaveLength(1) + expect(screen.getByRole('button', { name: 'Fermer' })).toHaveClass( + 'h-11', + 'w-11', + ) + }) + it('has no description when none is given', () => { render( {}} title="X">