From 3d589e15566bdf6de1a2462f8644682029429cef Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 14:09:30 +0100 Subject: [PATCH] fix(app): a focus request is consumed once, not replayed on tab return --- .../routes/app/admin/AdminSheetDraftRoute.tsx | 22 ++++---- .../annotations/EditableAnnotations.tsx | 2 +- .../editor/general/CategoriesSection.tsx | 6 ++- .../editor/references/ReferenceRow.tsx | 7 ++- .../components/editor/tabs/AnnotationsTab.tsx | 2 +- .../src/components/editor/tabs/GeneralTab.tsx | 2 +- .../components/editor/tabs/ReferencesTab.tsx | 2 +- .../components/editor/tabs/VertebraeTab.tsx | 2 +- .../src/components/editor/tabs/ViewsTab.tsx | 2 +- .../components/editor/vertebrae/RegionRow.tsx | 6 ++- .../components/editor/views/DetailViewRow.tsx | 6 ++- .../components/editor/views/MainViewCard.tsx | 6 ++- apps/app/src/editor/focusRequest.ts | 13 +++-- .../tests/unit/editor-focus-request.test.tsx | 51 +++++++++++++++++++ 14 files changed, 99 insertions(+), 30 deletions(-) diff --git a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx index e5cbeef6..cee555be 100644 --- a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx +++ b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx @@ -30,7 +30,7 @@ import { storeCanEdit, } from '@/editor/mode' import { leaveGuardArmed, useEditorStore } from '@/editor/state/editorStore' -import type { FocusRequest } from '@/editor/focusRequest' +import { FocusConsumedContext, type FocusRequest } from '@/editor/focusRequest' import type { EditorTabId, EditorTabProps } from '@/editor/tabs' import { useUndoShortcuts } from '@/editor/useUndoShortcuts' import { useTabTier } from '@/editor/useViewport' @@ -80,6 +80,8 @@ function DraftEditor({ const tier = useTabTier() const [tab, setTab] = useState(FIRST_TAB) const [focus, setFocus] = useState() + const clearFocus = (serial: number) => + setFocus((prev) => (prev?.serial === serial ? undefined : prev)) const goTo: EditorTabProps['onGoTo'] = (next, key) => { setTab(next) setFocus((prev) => ({ key, serial: (prev?.serial ?? 0) + 1 })) @@ -129,14 +131,16 @@ function DraftEditor({
- + + +
diff --git a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx index 73c5190e..1de106c4 100644 --- a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx +++ b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx @@ -5,7 +5,6 @@ import type { } from '@faunosteo/core-wasm/types' import type { CommitContext } from '@/editor/annotations/annotationCommit' import { annotationFocus } from '@/editor/annotations/focusTarget' -import { useFocusRequest } from '@/editor/focusRequest' import { keyboardPending, type NewKind } from '@/editor/annotations/creation' import { parseFrame, type Frame } from '@/editor/annotations/frame' import { isEditable } from '@/editor/annotations/geometryEdit' @@ -23,6 +22,7 @@ import { useViewModel, type ViewModel, } from '@/editor/annotations/useViewModel' +import { useFocusRequest } from '@/editor/focusRequest' import type { EditorTabProps } from '@/editor/tabs' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' diff --git a/apps/app/src/components/editor/general/CategoriesSection.tsx b/apps/app/src/components/editor/general/CategoriesSection.tsx index cf3fb3ca..b31bd594 100644 --- a/apps/app/src/components/editor/general/CategoriesSection.tsx +++ b/apps/app/src/components/editor/general/CategoriesSection.tsx @@ -1,4 +1,3 @@ -import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest' import { useState } from 'react' import type { AnatomicalSheetDocument, @@ -10,12 +9,15 @@ import TextField from '@/components/editor/fields/TextField' import DeleteDialog, { type DeleteTarget, } from '@/components/editor/DeleteDialog' +import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest' import { isBlank, nullable } from '@/editor/sheetDocument' import { useEditCommand } from '@/editor/useEditCommand' import type { Locale } from '@/i18n/types' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' +const CARD = 'rounded-lg border border-[var(--color-border)] p-3' + /** Annotation categories: list, rename, colour, add, delete with dependents. */ export default function CategoriesSection({ doc, @@ -78,7 +80,7 @@ function CategoryRow({ return (
  • {(['fr', 'en'] as const).map((locale) => ( diff --git a/apps/app/src/components/editor/references/ReferenceRow.tsx b/apps/app/src/components/editor/references/ReferenceRow.tsx index 7922aebd..fd56d6ce 100644 --- a/apps/app/src/components/editor/references/ReferenceRow.tsx +++ b/apps/app/src/components/editor/references/ReferenceRow.tsx @@ -9,6 +9,8 @@ import { useEditCommand } from '@/editor/useEditCommand' import { useT } from '@/i18n/useT' import { REFERENCE_TYPES } from './referenceTypes' +const CARD = 'space-y-3 rounded-lg border border-[var(--color-border)] p-3' + /** One reference: fields commit one by one, ↑ / ↓ reorder, delete asks the core. */ export default function ReferenceRow({ reference, @@ -38,10 +40,7 @@ export default function ReferenceRow({ })) const prefix = `reference-${reference.id}` return ( -
  • +
  • value === undefined ? '' : String(value) @@ -52,7 +54,7 @@ export default function RegionRow({ return (
  • diff --git a/apps/app/src/components/editor/views/MainViewCard.tsx b/apps/app/src/components/editor/views/MainViewCard.tsx index c651e371..c5552f74 100644 --- a/apps/app/src/components/editor/views/MainViewCard.tsx +++ b/apps/app/src/components/editor/views/MainViewCard.tsx @@ -1,10 +1,12 @@ -import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest' import type { MainView } from '@faunosteo/core-wasm/types' +import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest' import { useViewUpdate } from '@/editor/useViewUpdate' import { useT } from '@/i18n/useT' import ViewDrawing from './ViewDrawing' import ViewNames from './ViewNames' +const CARD = 'space-y-3 rounded-lg border border-[var(--color-border)] p-3' + /** The main view: its names and its drawing (state, detail, « Choisir une illustration »). */ export default function MainViewCard({ view, @@ -19,7 +21,7 @@ export default function MainViewCard({

    {t('editor.views.main_title')} diff --git a/apps/app/src/editor/focusRequest.ts b/apps/app/src/editor/focusRequest.ts index a40c93bd..17ad3fad 100644 --- a/apps/app/src/editor/focusRequest.ts +++ b/apps/app/src/editor/focusRequest.ts @@ -1,4 +1,4 @@ -import { useEffect, useRef } from 'react' +import { createContext, useContext, useEffect, useRef } from 'react' import type { ElementKey } from '@faunosteo/core-wasm/types' /** « Aller à … »: the element to show and a serial so each request is consumed once. */ @@ -19,17 +19,24 @@ export function focusAttrs(key: ElementKey) { export const FOCUS_CLASS = 'focus:outline focus:outline-2 focus:outline-[var(--color-accent)]' -/** Runs `apply` once per request serial, after the tab has rendered. */ +/** The route's answer to a consumed request: it clears it, so a remounted tab never replays. */ +export const FocusConsumedContext = createContext<(serial: number) => void>( + () => undefined, +) + +/** Runs `apply` once per request, after the tab has rendered, then reports it consumed. */ export function useFocusRequest( focus: FocusRequest | undefined, apply: (key: ElementKey) => void, ): void { const handled = useRef(null) + const consumed = useContext(FocusConsumedContext) useEffect(() => { if (!focus || handled.current === focus.serial) return handled.current = focus.serial apply(focus.key) - }) + consumed(focus.serial) + }, [focus, apply, consumed]) } /** Scrolls to and focuses the row tagged with `key`. */ diff --git a/apps/app/tests/unit/editor-focus-request.test.tsx b/apps/app/tests/unit/editor-focus-request.test.tsx index e2d75514..1084f272 100644 --- a/apps/app/tests/unit/editor-focus-request.test.tsx +++ b/apps/app/tests/unit/editor-focus-request.test.tsx @@ -1,6 +1,8 @@ /** Lot J fix: « Aller à … » seeds each tab's own selection once. */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { useState } from 'react' import { cleanup, render, screen } from '@testing-library/react' +import userEvent from '@testing-library/user-event' import type { AnatomicalSheetDocument, ElementKey, @@ -9,6 +11,7 @@ import FindingsTab from '@/components/editor/tabs/FindingsTab' import ReferencesTab from '@/components/editor/tabs/ReferencesTab' import VertebraeTab from '@/components/editor/tabs/VertebraeTab' import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab' +import { FocusConsumedContext, type FocusRequest } from '@/editor/focusRequest' import { issueTarget } from '@/editor/verification' import { useLocaleStore } from '@/state/localeStore' import { edit, insert, point } from '../helpers/editorFixtures' @@ -192,3 +195,51 @@ describe('issueTarget', () => { expect(issueTarget(d, 'nothing', 'fr')).toBeNull() }) }) + +/** The route's contract: one tab mounted at a time, the request cleared once consumed. */ +function Harness() { + const [tab, setTab] = useState<'references' | 'vertebrae'>('references') + const [focus, setFocus] = useState({ + key: { kind: 'reference', id: 'ref-a' }, + serial: 1, + }) + const clear = (serial: number) => + setFocus((prev) => (prev?.serial === serial ? undefined : prev)) + const Body = tab === 'references' ? ReferencesTab : VertebraeTab + return ( + + + + + ) +} + +describe('a consumed request', () => { + it('is not replayed when the tab is left and reopened', async () => { + const text = insert(baseText(), { + kind: 'reference', + value: { + id: 'ref-a', + type: 'book', + citation: 'Alpha', + url: null, + doi: null, + }, + }) + await openReady({ text }) + const user = userEvent.setup() + render() + expect(document.activeElement?.getAttribute('data-focus-key')).toContain( + 'ref-a', + ) + await user.click(screen.getByText('switch')) + await user.click(screen.getByText('switch')) + expect(document.activeElement?.getAttribute('data-focus-key')).toBeNull() + }) +})