From 1e19479853b85f300617cf0f412ca036eca68ae9 Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 13 Nov 2026 11:25:50 +0100 Subject: [PATCH] fix(editor): C1 review round 1, note endings tested, tile hook, pin guard Tests: note ends on another tile's Modifier, on Ajouter une reference, and is replaced (announced again) by a newer drop; long titles shortened. Read-only toggle deletes the key from openKeys explicitly. Drop slot and tile wrappers moved to useTileActions. pinEdited pins nothing for an absent key. --- .../editor/references/ReferenceTile.tsx | 29 +++++++--- .../editor/references/useReferenceView.ts | 4 +- .../editor/references/useTileState.ts | 5 ++ .../unit/editor-references-flip.test.tsx | 55 ++++++++++++++++++- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/apps/app/src/components/editor/references/ReferenceTile.tsx b/apps/app/src/components/editor/references/ReferenceTile.tsx index 0f41cd96..f105875c 100644 --- a/apps/app/src/components/editor/references/ReferenceTile.tsx +++ b/apps/app/src/components/editor/references/ReferenceTile.tsx @@ -243,6 +243,26 @@ function useDroppedDrafts(editing: boolean, readOnly: boolean) { return [dropped, setDropped] as const } +/** The tile's own actions: « Terminer » refocuses the header, a toggle ends the note. */ +function useTileActions( + props: TileProps, + headerRef: RefObject, +) { + const { on, editing, readOnly } = props + const [dropped, setDropped] = useDroppedDrafts(editing, readOnly) + return { + dropped, + setDropped, + done: () => { + if (on.done()) headerRef.current?.focus() + }, + toggle: () => { + setDropped(null) + on.toggle() + }, + } +} + /** « Saisie non appliquée : … »: plain text, the live region says it once elsewhere. */ function DroppedNote({ fields }: { fields: readonly Field[] }) { const droppedText = useDroppedText() @@ -264,14 +284,7 @@ export default function ReferenceTile(props: TileProps) { const t = useT() const base = tileBase(host, position, reference) const headerRef = useRef(null) - const [dropped, setDropped] = useDroppedDrafts(editing, props.readOnly) - const done = () => { - if (on.done()) headerRef.current?.focus() - } - const toggle = () => { - setDropped(null) - on.toggle() - } + const { dropped, setDropped, done, toggle } = useTileActions(props, headerRef) return (
  • (null) // The reset view is the sheet order: the tile in edit sits at its own position. const pinEdited = (): Pinned | null => - editingKey === null + editingKey === null || !keys.includes(editingKey) ? null - : { key: editingKey, index: Math.max(0, keys.indexOf(editingKey)) } + : { key: editingKey, index: keys.indexOf(editingKey) } if (seen !== resetSignal) { setSeen(resetSignal) setView(INITIAL) diff --git a/apps/app/src/components/editor/references/useTileState.ts b/apps/app/src/components/editor/references/useTileState.ts index 04d5f6f2..765ac34e 100644 --- a/apps/app/src/components/editor/references/useTileState.ts +++ b/apps/app/src/components/editor/references/useTileState.ts @@ -48,6 +48,11 @@ export function useTileState(keys: readonly string[] | null) { if (editingKey === key) { if (!readOnly) return void finish() setEditingKey(null) + return setOpenKeys((prev) => { + const next = new Set(prev) + next.delete(key) + return next + }) } setOpenKeys((prev) => { const next = new Set(prev) diff --git a/apps/app/tests/unit/editor-references-flip.test.tsx b/apps/app/tests/unit/editor-references-flip.test.tsx index 77a95734..f2d6a83f 100644 --- a/apps/app/tests/unit/editor-references-flip.test.tsx +++ b/apps/app/tests/unit/editor-references-flip.test.tsx @@ -145,7 +145,7 @@ async function typeBadUrl(user: User, id: string) { } describe('a read-only flip with a refused draft (tab tile)', () => { - it('shows the note in the tile, says it once, writes nothing, keeps the focus on the tile', async () => { + it('shows the note, says it once, writes nothing, focuses the tile', async () => { const user = await mount() await editTile(user, 'ref-b', /^Denoix/) await typeBadUrl(user, 'ref-b') @@ -167,7 +167,7 @@ describe('a read-only flip with a refused draft (tab tile)', () => { watch.stop() }) - it('keeps the note across the flip back, URL field on the document value; Terminer removes it', async () => { + it('keeps the note across the flip back; Terminer removes it', async () => { const user = await mount() await editTile(user, 'ref-b', /^Denoix/) await typeBadUrl(user, 'ref-b') @@ -225,8 +225,57 @@ describe('a read-only flip with a refused draft (tab tile)', () => { }) }) +describe('the note ends on the next action', () => { + /** ref-b flipped with a refused URL and back: in edit again, note shown. */ + async function flippedBack() { + const user = await mount() + await editTile(user, 'ref-b', /^Denoix/) + await typeBadUrl(user, 'ref-b') + cross(800) + cross(1280) + expect( + within(tile('ref-b')).getByText(/Saisie non appliquée/), + ).toBeVisible() + return user + } + const noted = () => within(tabPanel()).queryAllByText(/Saisie non appliquée/) + + it("ends on another tile's « Modifier » (this tile leaves edit)", async () => { + const user = await flippedBack() + await user.click( + within(tabPanel()).getByRole('button', { name: /^Barone/ }), + ) + await user.click( + within(tile('ref-a')).getByRole('button', { name: 'Modifier' }), + ) + expect(noted()).toHaveLength(0) + }) + + it('ends on « Ajouter une référence »', async () => { + const user = await flippedBack() + await user.click( + within(tabPanel()).getByRole('button', { name: 'Ajouter une référence' }), + ) + expect(noted()).toHaveLength(0) + }) + + it('is replaced by a newer drop of the same tile, announced again', async () => { + const user = await flippedBack() + const watch = watchNotes() + await user.clear(field('ref-b', 'citation')) + await typeBadUrl(user, 'ref-b') + cross(800) + await flush() + const items = noted() + expect(items).toHaveLength(1) + expect(items[0]).toHaveTextContent(`${NOTE} : Citation, Adresse web (URL)`) + expect(watch.seen).toHaveLength(1) + watch.stop() + }) +}) + describe('a read-only flip without a refused draft', () => { - it('adds exactly the typed change when a valid draft has the focus, nothing on the way back', async () => { + it('adds only the typed change when a valid draft has the focus', async () => { const user = await mount() await editTile(user, 'ref-a', /^Barone/) const citation = field('ref-a', 'citation')