fix(editor): forget the citing form on Back, refuse sparse paths
This commit is contained in:
1 parent
7a5684aace
commit
6c0f913104
5 files changed
+30
-3
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<Held>({ 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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user