feat(editor): reference dialog, reading tiles and finding filter in the history

R2F-5 and R2F-6: the reference dialog is a view layer above the References tab or the citations sheet (local draft, one command per changed save, creation entry becomes the reference's own). Tiles only read. « Tout afficher » filters the library by the finding, « Revenir au constat » pops or pushes. Inline tile edit, draft tile and their guards removed.
This commit is contained in:
bchanot committed 2026-11-13 20:27:00 +01:00
1 parent e2d020bd3d
commit 8031e0078f
28 files changed
+1072 -1512

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
import { useCallback, useRef, useState, type RefObject } from 'react'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import type { LayerContext, MoveTrace } from './useEditorNavigation'
/** The reference a creation just made: the library shows a notice for it, once per serial. */
export interface AddedReference {
id: string
serial: number
}
type Opener = HTMLElement | null | undefined
/** What the reference dialog (R2F-5) and the References tab's filter (R2F-6) do to the view. */
export interface ReferenceMoves {
/** The dialog of reference `id`, over the place the view shows. */
open: (id: string, opener?: Opener) => void
/** The creation dialog, over the References tab. */
start: (opener?: Opener) => void
/** A user close; false when the form refused it (a dirty draft asks first). */
close: (forced?: boolean) => boolean
/**
* The dialog becomes another view of itself, by replace (no Back entry): `null` closes
* it. With `text`, a close the user did not ask for (the reference vanished): forced,
* said once the dialogs are closed.
*/
replace: (id: string | null, text?: string) => void
/** A creation succeeded: its entry becomes the reference's own, then the dialog closes. */
created: (id: string) => void
added: AddedReference | null
/** The element that opened the dialog, while it is still on the page. */
opener: () => HTMLElement | null
/** « × » of the filter chip, or a vanished finding (`text` says so): a replace. */
clearFilter: (text?: string) => void
/** « Revenir au constat »: a Back when the entry below shows it, else its dialog. */
backToFinding: (key: ElementKey) => void
}
/** The moves that add the dialog: an edit of a reference, a creation. */
function useOpening({ dispatch, mark }: LayerContext, held: RefObject<Opener>) {
const open = useCallback(
(id: string, opener?: Opener) => {
held.current = opener
mark()
dispatch({ type: 'open_reference', id })
},
[dispatch, mark, held],
)
const start = useCallback(
(opener?: Opener) => {
held.current = opener
mark()
dispatch({ type: 'create_reference' })
},
[dispatch, mark, held],
)
return { open, start }
}
/** The moves that take it away: a user close, and the replace that is no Back step. */
function useClosing({ dispatch, announce, guards, mark }: LayerContext) {
const close = useCallback(
(forced = false) => {
if (!guards.ask('reference', forced).allowed) return false
mark()
dispatch({ type: 'close_reference' })
return true
},
[guards, dispatch, mark],
)
const replace = useCallback(
(id: string | null, text?: string) => {
const note = text ? guards.ask('reference', true).note : undefined
if (text) announce(note ? `${note}. ${text}` : text)
mark(true)
dispatch(
id ? { type: 'open_reference', id } : { type: 'close_reference' },
)
},
[guards, announce, dispatch, mark],
)
return { close, replace }
}
/** A creation that succeeded: its entry becomes the reference's own, then it closes. */
function useCreated(
{ dispatch, mark }: LayerContext,
replace: ReferenceMoves['replace'],
) {
const [added, setAdded] = useState<AddedReference | null>(null)
const created = useCallback(
(id: string) => {
replace(id)
setAdded((last) => ({ id, serial: (last?.serial ?? 0) + 1 }))
// After the commit that wrote the replace: the entry is the reference's own before
// the close goes back over it, so a Forward never reopens a creation.
queueMicrotask(() => {
mark()
dispatch({ type: 'close_reference' })
})
},
[replace, mark, dispatch],
)
return { created, added }
}
/** The filter's two moves: remove it by replace, return to the finding it names. */
function useFilter(
{ dispatch, announce, mark }: LayerContext,
trace: MoveTrace,
) {
const clearFilter = useCallback(
(text?: string) => {
if (text) announce(text)
mark(true)
dispatch({ type: 'clear_reference_filter' })
},
[announce, dispatch, mark],
)
const backToFinding = useCallback(
(key: ElementKey) => {
mark()
trace.backTo = key
dispatch({ type: 'open_finding', key })
},
[dispatch, mark, trace],
)
return { clearFilter, backToFinding }
}
export function useReferenceMoves(
ctx: LayerContext,
trace: MoveTrace,
): ReferenceMoves {
const held = useRef<Opener>(null)
const opener = useCallback((): HTMLElement | null => {
const element = held.current
return element?.isConnected && !element.closest('[hidden]') ? element : null
}, [held])
const closing = useClosing(ctx)
return {
...useOpening(ctx, held),
...closing,
...useCreated(ctx, closing.replace),
...useFilter(ctx, trace),
opener,
}
}