import { useEffect, useMemo, useRef, type RefObject } from 'react' import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types' import { visibleElement } from '../focusRequest' import { MIN_EDIT_WIDTH } from '../mode' import type { AnnotationLayer } from '../tabs' import { CANVAS_BOX } from '../useEditorNavigation' import { guardDrawing } from './drawingGuard' /** What opened the properties dialog: the strip's « Propriétés », or the drawing or the list. */ export type OpenedBy = 'button' | 'drawing' /** Opens the properties dialog of `id` (A1). */ export type OpenDialog = (id: string, by: OpenedBy) => void /** * The column the dialog sits over (A2), resolved when measured: the right column of the * body shown (`data-annotation-aside`: `edit`, or `read` while read-only) at 1 024 px and * wider, none below (stacked layout: the dialog is centred, R-14). */ function columnAnchor(readOnly: boolean): RefObject { const column = `[data-annotation-aside="${readOnly ? 'read' : 'edit'}"]` return { get current() { if (window.innerWidth < MIN_EDIT_WIDTH) return null return visibleElement(column) }, } } /** The visible row of the annotation `id` in the overview. */ function rowOf(id: string): HTMLElement | null { const wanted = JSON.stringify({ kind: 'annotation', id }) return visibleElement('[data-focus-key]', (row) => { return row.dataset.focusKey === wanted }) } /** * The dialog's way in, out and place (board 32): an opening first commits a key series and * asks started drawing work (R-12); once closed, the focus goes back to « Propriétés » * when it opened it, else to the annotation's row, or the drawing when a filter hides it. * The anchor is measured again when the editor turns read-only or back. */ export function useDialogOpening( layer: AnnotationLayer | undefined, readOnly: boolean, ) { const opened = useRef<{ id: string; by: OpenedBy } | null>(null) const latest = useRef(layer) latest.current = layer // A layer opened by a reload or a Forward has no opener: its row is the way back. const shown = layer?.view?.id if (shown && opened.current?.id !== shown) { opened.current = { id: shown, by: 'drawing' } } const open: OpenDialog = (id, by) => { const go = () => { opened.current = { id, by } latest.current?.open(id) } if (guardDrawing(go)) go() } const returnFocus = () => { const last = opened.current if (!last) return null const button = last.by === 'button' && visibleElement('[data-properties-button]') return button || rowOf(last.id) || visibleElement(CANVAS_BOX) } // A new object per flip: the dialog measures its place again (no resize fires). const anchor = useMemo(() => columnAnchor(readOnly), [readOnly]) return { open, returnFocus, anchor } } /** What a body lets the layer do with its selection. */ export interface LayerSelection { viewId: string selectedId: string | null show: (viewId: string, annotationId: string) => void clear: () => void } /** * The selection follows the layer (a Back, a Forward, a reload): its annotation is shown * on its view and selected; a layer whose annotation is gone leaves nothing selected. */ export function useLayerFollow( layerId: string | null, doc: AnatomicalSheetDocument, body: LayerSelection, ): void { const latest = useRef(body) latest.current = body const { viewId, selectedId } = body useEffect(() => { if (layerId === null) return const found = doc.annotations.find((a) => a.id === layerId) if (!found) { if (selectedId !== null) latest.current.clear() return } if (found.id === selectedId && found.view_id === viewId) return latest.current.show(found.view_id, found.id) }, [layerId, doc, viewId, selectedId]) }