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) { 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(ctx: LayerContext) { const { dispatch, announce, guards, mark } = ctx 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(ctx: LayerContext, replace: ReferenceMoves['replace']) { const { dispatch, mark } = ctx const [added, setAdded] = useState(null) const created = useCallback( (id: string) => { replace(id) setAdded((last) => ({ id, serial: (last?.serial ?? 0) + 1 })) // Precondition: the caller is a discrete event (the form's submit). React then // renders the replace and runs the history effect before this microtask, so the // entry is the reference's own before the close goes back over it and a Forward // never reopens a creation. Called from anywhere else, the replace and the close // would merge into one view change (a Back, and the entry keeps its creation). // The microtask also keeps `mark()` from overwriting the replace hint early. 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(ctx: LayerContext, trace: MoveTrace) { const { dispatch, announce, mark } = ctx 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(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, } }