diff --git a/apps/app/src/editor/history/entry.ts b/apps/app/src/editor/history/entry.ts index 9e95339b..f415d715 100644 --- a/apps/app/src/editor/history/entry.ts +++ b/apps/app/src/editor/history/entry.ts @@ -44,7 +44,9 @@ const isId = (value: unknown): value is string => /** A finding path no deeper than the tree allows, each id within the cap. */ const isPath = (path: unknown): path is string[] => - Array.isArray(path) && path.length <= MAX_FINDING_PATH && path.every(isId) + Array.isArray(path) && + path.length <= MAX_FINDING_PATH && + Array.from(path).every(isId) /** A finding or zone key as the tree builds it, or null when `raw` is no such key. */ function sanitizeKey(raw: unknown): ElementKey | null { diff --git a/apps/app/src/editor/useEditorNavigation.ts b/apps/app/src/editor/useEditorNavigation.ts index d17f23fc..6a62f78c 100644 --- a/apps/app/src/editor/useEditorNavigation.ts +++ b/apps/app/src/editor/useEditorNavigation.ts @@ -623,7 +623,7 @@ export function useEditorNavigation( ...useFindingLayer(ctx), ...useCitationsLayer(ctx, openers), ...useLeavingMoves(base.go, useAskLeaving(ctx, base.state)), - references: useReferenceMoves(ctx, base.trace), + references: useReferenceMoves(ctx, base.trace, base.state.reference), citationsTrigger: useCallback(() => triggerOf(openers), [openers]), guards, trace: base.trace, diff --git a/apps/app/src/editor/useReferenceMoves.ts b/apps/app/src/editor/useReferenceMoves.ts index 376f83e5..de98fc23 100644 --- a/apps/app/src/editor/useReferenceMoves.ts +++ b/apps/app/src/editor/useReferenceMoves.ts @@ -1,5 +1,6 @@ -import { useCallback, useRef, useState } from 'react' +import { useCallback, useEffect, useRef, useState } from 'react' import type { ElementKey } from '@faunosteo/core-wasm/types' +import type { NavigationState } from './navigation' import type { LayerContext, MoveTrace } from './useEditorNavigation' /** The reference a creation just made: the library shows a notice for it, once per serial. */ @@ -169,8 +170,14 @@ function useFilter(ctx: LayerContext, trace: MoveTrace) { export function useReferenceMoves( ctx: LayerContext, trace: MoveTrace, + view: NavigationState['reference'], ): ReferenceMoves { const held = useRef({ opener: null, cite: null }).current + const creating = view !== null && 'create' in view + // A Back that removes the creation dialog leaves no citing form to answer. + useEffect(() => { + if (!creating) held.cite = null + }, [creating, held]) const opener = useCallback((): HTMLElement | null => { const element = held.opener return element?.isConnected && !element.closest('[hidden]') ? element : null diff --git a/apps/app/tests/unit/editor-citations-guards.test.tsx b/apps/app/tests/unit/editor-citations-guards.test.tsx index 7ff552d9..28331798 100644 --- a/apps/app/tests/unit/editor-citations-guards.test.tsx +++ b/apps/app/tests/unit/editor-citations-guards.test.tsx @@ -181,6 +181,17 @@ describe('a creation started over the citations dialog', () => { expect(cite).not.toHaveBeenCalled() }) + it('a Back that removes the creation dialog forgets its citing form', async () => { + cited() + const cite = vi.fn() + act(() => nav.references.start(null, cite)) + act(() => navigate(-1)) + expect(nav.state.reference).toBeNull() + await act(async () => nav.references.created('ref-z')) + expect(cite).not.toHaveBeenCalled() + expect(nav.references.added).toMatchObject({ id: 'ref-z' }) + }) + it('a later creation from the library sets the notice, cites nothing', async () => { cited() const cite = vi.fn() diff --git a/apps/app/tests/unit/editor-history-entry.test.ts b/apps/app/tests/unit/editor-history-entry.test.ts index d373ab64..5e77a18e 100644 --- a/apps/app/tests/unit/editor-history-entry.test.ts +++ b/apps/app/tests/unit/editor-history-entry.test.ts @@ -412,4 +412,11 @@ describe('final review: read-back values are capped', () => { const sheet = { ...edit, finding: { key: node, citations: true } } expect(sanitizeView(sheet)?.reference).toBeNull() }) + + it('drops a key whose path is a sparse array', () => { + const path: string[] = [] + path[1] = 'n' + const sparse: ElementKey = { kind: 'finding', zone_id: 'z', path } + expect(sanitizeView(onFindings(sparse))?.finding).toBeNull() + }) })