From 0100e2e63d8244f5b0d638ef2360780f423fe498 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 23 Nov 2026 18:08:53 +0100 Subject: [PATCH] fix(app): bilan detail stack guards, unrelated reformat reverted --- apps/app/src/app/routes/EditorRoute.tsx | 43 +++++--- apps/app/src/components/ui/Icon.tsx | 18 +--- .../tests/unit/routes.EditorRoute.test.tsx | 99 +++++++++++++++---- 3 files changed, 118 insertions(+), 42 deletions(-) diff --git a/apps/app/src/app/routes/EditorRoute.tsx b/apps/app/src/app/routes/EditorRoute.tsx index 7a95bd5a..f6d87993 100644 --- a/apps/app/src/app/routes/EditorRoute.tsx +++ b/apps/app/src/app/routes/EditorRoute.tsx @@ -14,9 +14,7 @@ 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' @@ -36,7 +34,10 @@ 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' @@ -45,6 +46,21 @@ import { useCoreStopped } from '@/core/runtime' const DISCLAIMER_FLAG = 'osteo:disclaimer-shown' /** What the PDF lacks, named for the practitioner: logo slots and view ids. */ +/** + * The detail window's views after an `open_view`: pushed when the click came from the view + * on top, a new stack from the page; the view already on top is never pushed again. + */ +function nextDetailStack( + stack: string[], + viewId: string | undefined, + fromViewId: string, +): string[] { + if (!viewId) return [] + const top = stack[stack.length - 1] + if (top !== fromViewId) return [viewId] + return viewId === top ? stack : [...stack, viewId] +} + function missingItemsOf(warnings: readonly ExportWarning[]): string[] { return warnings.map((w) => (w.kind === 'logo_failed' ? w.slot : w.viewId)) } @@ -83,7 +99,9 @@ export default function EditorRoute() { 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. @@ -109,6 +127,12 @@ export default function EditorRoute() { return () => exportAbort.current?.abort() }, []) + // A view id of another sheet means nothing: the window closes when the sheet changes. + const loadedIndex = sheet.status === 'ready' ? sheet.index : null + useEffect(() => { + setDetailStack((stack) => (stack.length ? [] : stack)) + }, [loadedIndex]) + // No report in memory: restore the per-tab draft once the sheet is known. // A draft rebased on another sheet is announced with its unresolved count, // an unreadable one is set aside. While the core cannot run, nothing is @@ -174,17 +198,12 @@ 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': // 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], + nextDetailStack(stack, click.viewId, fromViewId), ) case 'activate_fallback': return store.setActiveObservation(report.observations.length - 1) diff --git a/apps/app/src/components/ui/Icon.tsx b/apps/app/src/components/ui/Icon.tsx index 71a09189..00d84036 100644 --- a/apps/app/src/components/ui/Icon.tsx +++ b/apps/app/src/components/ui/Icon.tsx @@ -30,8 +30,7 @@ const PATHS: Record = { 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', @@ -41,16 +40,14 @@ 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', + 'arrow-left': 'M19 12H5M12 5l-7 7 7 7', '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: @@ -64,12 +61,7 @@ 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() @@ -288,6 +286,25 @@ async function withNestedView() { name: null, opens_view_id: 'incisors', }) + // A trigger of the head that opens the head itself: the view already on top. + document.annotations.push({ + id: 'head-self-trigger', + kind: 'detail_trigger', + view_id: 'head', + category_id: 'articulations', + laterality: 'unpaired', + geometry: { + type: 'polygon', + points: [ + [100, 300], + [150, 300], + [150, 340], + [100, 340], + ], + }, + name: null, + opens_view_id: 'head', + }) // 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 } @@ -385,6 +402,54 @@ describe('EditorRoute detail window stack (D15)', () => { ).toBe('60 100 500 380') }) + it('closes the window on a backdrop click', async () => { + const user = userEvent.setup() + await openNested() + await user.click(screen.getByTestId('dialog-backdrop')) + 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('never pushes the view already on top of the stack', async () => { + const user = userEvent.setup() + await mount() + clickZone('head-trigger') + const head = await screen.findByRole('dialog', { name: HEAD }) + await waitFor(() => + expect(head.querySelector('[data-anno-id="head-self-trigger"]')).not.toBeNull(), + ) + clickZone('head-self-trigger', head) + expect(within(head).queryByRole('button', { name: /Revenir/ })).toBeNull() + clickZone('incisors-trigger', head) + const nested = await screen.findByRole('dialog', { name: 'Incisives' }) + await user.click(within(nested).getByRole('button', { name: BACK })) + const back = await screen.findByRole('dialog', { name: HEAD }) + expect(within(back).queryByRole('button', { name: /Revenir/ })).toBeNull() + }) + + it('clears the stack when the sheet changes, so the window never comes back', async () => { + await openNested() + // The fixture provider answers any species: another species loads another index. + const animal = useReportStore.getState().report!.animal + act(() => + useReportStore.setState((s) => ({ + report: { ...s.report!, animal: { ...animal, speciesId: 'canine' } }, + })), + ) + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + act(() => + useReportStore.setState((s) => ({ + report: { ...s.report!, animal }, + })), + ) + await waitFor(() => + expect(document.querySelector('[data-anno-id="head-trigger"]')).not.toBeNull(), + ) + expect(screen.queryByRole('dialog')).toBeNull() + }) + it('centres the drawing vertically in its area (flex, items-center)', async () => { const dialog = await openNested() const area = dialog.querySelector(