feat(app): Aller à selects the element in its tab, reload after submit

This commit is contained in:
bchanot committed 2026-11-04 14:06:09 +01:00
1 parent 2a54fa4c78
commit 4f223e04cd
24 files changed
+473 -66

No files matched your search

@@ -0,0 +1,20 @@
import type {
AnatomicalSheetDocument,
ElementKey,
} from '@faunosteo/core-wasm/types'
/** What « Aller à … » seeds in the Annotations tab: a view, and maybe an annotation. */
export function annotationFocus(
doc: AnatomicalSheetDocument,
key: ElementKey,
): { viewId: string; annotationId: string | null } | null {
if (key.kind === 'view') {
const view = doc.views.find((v) => v.id === key.id)
return view ? { viewId: view.id, annotationId: null } : null
}
if (key.kind !== 'annotation') return null
const annotation = doc.annotations.find((a) => a.id === key.id)
return annotation
? { viewId: annotation.view_id, annotationId: annotation.id }
: null
}
+20
View File
@@ -245,3 +245,23 @@ export function createdItemId(
const path = kind === 'category' ? [id] : [...parent.path, id]
return itemId(parent.zoneId, path)
}
/** The item a focus request names (a zone or a finding), with its ancestors' ids. */
export function itemForKey(
items: readonly FindingItem[],
key: ElementKey,
): { item: FindingItem; ancestors: string[] } | null {
const item = items.find((candidate) => sameKey(candidate.key, key))
if (!item) return null
const ancestors: string[] = []
let parent = item.parentId
while (parent !== null) {
ancestors.push(parent)
parent = items.find((i) => i.id === parent)?.parentId ?? null
}
return { item, ancestors }
}
function sameKey(a: ElementKey, b: ElementKey): boolean {
return JSON.stringify(a) === JSON.stringify(b)
}
+48
View File
@@ -0,0 +1,48 @@
import { useEffect, useRef } from 'react'
import type { ElementKey } from '@faunosteo/core-wasm/types'
/** « Aller à … »: the element to show and a serial so each request is consumed once. */
export interface FocusRequest {
key: ElementKey
serial: number
}
/** The attributes that make a row of a list reachable by a focus request. */
export function focusAttrs(key: ElementKey) {
return {
'data-focus-key': JSON.stringify(key),
tabIndex: -1,
} as const
}
/** The ring a programmatically focused row shows. */
export const FOCUS_CLASS =
'focus:outline focus:outline-2 focus:outline-[var(--color-accent)]'
/** Runs `apply` once per request serial, after the tab has rendered. */
export function useFocusRequest(
focus: FocusRequest | undefined,
apply: (key: ElementKey) => void,
): void {
const handled = useRef<number | null>(null)
useEffect(() => {
if (!focus || handled.current === focus.serial) return
handled.current = focus.serial
apply(focus.key)
})
}
/** Scrolls to and focuses the row tagged with `key`. */
export function focusRow(key: ElementKey): void {
const wanted = JSON.stringify(key)
const row = [
...document.querySelectorAll<HTMLElement>('[data-focus-key]'),
].find((candidate) => candidate.dataset.focusKey === wanted)
row?.scrollIntoView?.({ block: 'center' })
row?.focus()
}
/** Row-based tabs (Général, Vues, Références, Vertèbres): focus the named row. */
export function useRowFocus(focus: FocusRequest | undefined): void {
useFocusRequest(focus, focusRow)
}
+6
View File
@@ -1,3 +1,5 @@
import type { ElementKey } from '@faunosteo/core-wasm/types'
import type { FocusRequest } from './focusRequest'
import type { TabTier } from './useViewport'
export const EDITOR_TABS = [
@@ -32,6 +34,10 @@ export interface EditorTabProps {
readOnly: boolean
/** « Aller à … » and any link between tabs. */
onSelectTab: (tab: EditorTabId) => void
/** « Aller à … » with an element: the tab seeds its own selection from it once. */
focus?: FocusRequest
/** Switches tab and asks it to show the element. */
onGoTo?: (tab: EditorTabId, key: ElementKey) => void
/** Reads the sheet's overview again (after a submission: the round freezes the draft). */
reloadSheet: () => void
}
+57 -37
View File
@@ -73,56 +73,76 @@ const ROOT_PATH = /^(\w+)\[(\d+)\]/
export interface IssueTarget {
tab: EditorTabId
/** The named element (its label), when the path resolves in the document. */
/** The element the path names, when it resolves in the document. */
key: ElementKey | null
/** Its label. */
name: string | null
}
function keyOf(
doc: AnatomicalSheetDocument,
root: string,
index: number,
): ElementKey | null {
switch (root) {
case 'views':
return idKey('view', doc.views[index])
case 'annotations':
return idKey('annotation', doc.annotations[index])
case 'categories':
return idKey('category', doc.categories[index])
case 'finding_zones':
return idKey('zone', doc.finding_zones[index])
case 'references':
return idKey('reference', doc.references[index])
default:
return null
type Kind = 'view' | 'annotation' | 'category' | 'zone' | 'reference'
const ROOT_KIND: Record<string, Kind> = {
views: 'view',
annotations: 'annotation',
categories: 'category',
finding_zones: 'zone',
references: 'reference',
}
/** The id-bearing list a path root names. */
function listOf(doc: AnatomicalSheetDocument, root: string) {
return (doc as unknown as Record<string, { id: string }[] | undefined>)[root]
}
interface NodeLike {
id: string
categories?: NodeLike[]
children?: NodeLike[]
}
/** The finding the nested part of a `finding_zones[i]...` path names (zone when none). */
function findingKey(zone: NodeLike, path: string): ElementKey {
const ids: string[] = []
let node: NodeLike | undefined = zone
for (const step of path.matchAll(/\.(categories|children)\[(\d+)\]/g)) {
node = (step[1] === 'categories' ? node?.categories : node?.children)?.[
Number(step[2])
]
if (!node) break
ids.push(node.id)
}
return ids.length === 0
? { kind: 'zone', id: zone.id }
: { kind: 'finding', zone_id: zone.id, path: ids }
}
function idKey<
K extends 'view' | 'annotation' | 'category' | 'zone' | 'reference',
>(kind: K, item: { id: string } | undefined): ElementKey | null {
return item ? ({ kind, id: item.id } as ElementKey) : null
function keyOf(doc: AnatomicalSheetDocument, path: string): ElementKey | null {
const region = REGION_PATH.exec(path)?.[1]
if (region !== undefined) {
const code = doc.vertebral_config.regions[Number(region)]?.code
return code ? { kind: 'region', code } : null
}
const match = ROOT_PATH.exec(path)
const item = match ? listOf(doc, match[1])?.[Number(match[2])] : undefined
if (!match || !item) return null
if (match[1] === 'finding_zones') return findingKey(item, path)
return { kind: ROOT_KIND[match[1]], id: item.id } as ElementKey
}
/** The tab holding the element an issue path names (« Aller à … »); null when none. */
function tabOf(path: string): EditorTabId | null {
if (REGION_PATH.test(path)) return 'vertebrae'
const root = ROOT_PATH.exec(path)?.[1]
return root ? (ROOT_TAB[root] ?? null) : null
}
/** The tab and element an issue path names (« Aller à … »); null when it names none. */
export function issueTarget(
doc: AnatomicalSheetDocument,
path: string,
locale: Locale,
): IssueTarget | null {
const region = REGION_PATH.exec(path)?.[1]
if (region !== undefined) {
const code = doc.vertebral_config.regions[Number(region)]?.code
const name = code
? elementLabel(doc, { kind: 'region', code }, locale)
: null
return { tab: 'vertebrae', name }
}
const match = ROOT_PATH.exec(path)
const tab = match ? ROOT_TAB[match[1]] : undefined
if (!match || !tab) return null
const key = keyOf(doc, match[1], Number(match[2]))
return { tab, name: key ? elementLabel(doc, key, locale) : null }
const tab = tabOf(path)
if (!tab) return null
const key = keyOf(doc, path)
return { tab, key, name: key ? elementLabel(doc, key, locale) : null }
}
/** « 2 097 152 » with plain spaces, as the plan words the size. */