fix(app): annotations tab split, editor-wide undo keys, keyboard point fits, typed radius message

This commit is contained in:
bchanot committed 2026-11-04 14:00:57 +01:00
1 parent 447d0a4272
commit 3f15762d95
25 files changed
+1398 -653

No files matched your search

@@ -1,38 +1,24 @@
import { useState, type Dispatch } from 'react'
import type {
AnatomicalSheetDocument,
Laterality,
} from '@faunosteo/core-wasm/types'
import DraftField from '@/components/editor/fields/DraftField'
import FormField from '@/components/ui/FormField'
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import Select from '@/components/ui/Select'
import type { CommitContext } from '@/editor/annotations/annotationCommit'
import {
insertAnnotation,
type CommitContext,
type CommitResult,
} from '@/editor/annotations/annotationCommit'
import {
attemptPoint,
confirmCorrection,
type Correction,
type PointAttempt,
} from '@/editor/annotations/correction'
import {
LATERALITIES,
annotationOf,
directChildren,
emptyForm,
formProblem,
withGeometry,
type CreationForm,
type PendingElement,
} from '@/editor/annotations/creation'
import {
confirmAttempt,
createAttempt,
type Attempt,
type CreationInput,
} from '@/editor/annotations/creationAttempt'
import type { Frame } from '@/editor/annotations/frame'
import type { EditableGeometry } from '@/editor/annotations/geometryEdit'
import type { UiAction } from '@/editor/annotations/uiState'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import CorrectionNotice from './CorrectionNotice'
import CreationFields from './CreationFields'
import GeometryGroup from './GeometryGroup'
export interface CreationPanelProps {
@@ -45,47 +31,44 @@ export interface CreationPanelProps {
context: CommitContext
}
type Attempt = PointAttempt | { status: 'invalid'; messages: string[] }
/** The insert of this panel's element under a given geometry, through `accept`. */
function inserterOf(props: CreationPanelProps, form: CreationForm) {
return (geometry: EditableGeometry): CommitResult =>
insertAnnotation(
(id) =>
annotationOf(id, props.viewId, { ...props.pending, geometry }, form),
props.context,
)
/** What an attempt leaves on screen: the core's words, or the form's own. */
function messagesOf(attempt: Attempt, words: (problem: string) => string) {
if (attempt.status === 'refused') return attempt.messages
return attempt.status === 'invalid' ? [words(attempt.problem)] : []
}
/** « Créer »: a point lets the core's verdict offer C; nothing else is ever invented. */
function createAttempt(
props: CreationPanelProps,
form: CreationForm,
words: (problem: string) => string,
): Attempt {
const { pending, frame } = props
const problem = formProblem(pending.kind, form)
if (problem) return { status: 'invalid', messages: [words(problem)] }
const insert = inserterOf(props, form)
const { geometry } = pending
if (pending.kind === 'point' && geometry.type === 'circle' && frame) {
return attemptPoint(frame, geometry, (c) =>
insert({ type: 'circle', ...c }),
/**
* The typed fields and the outcome of « Créer » / « Utiliser cette position ». The form
* survives a change of the pending geometry (a second click): the panel is not remounted.
*/
function useCreation(props: CreationPanelProps) {
const t = useT()
const [form, setForm] = useState<CreationForm>(() => emptyForm(props.doc))
const [messages, setMessages] = useState<string[]>([])
const settle = (attempt: Attempt) => {
const { dispatch, pending } = props
setMessages(
messagesOf(attempt, (p) => t(`editor.annotations.required.${p}`)),
)
if (attempt.status === 'created') {
dispatch({ type: 'select', id: attempt.id ?? null })
}
if (attempt.status === 'correction') {
const next = { ...pending, correction: attempt.correction }
dispatch({ type: 'pending_update', pending: next })
}
}
const input: CreationInput = props
return {
form,
messages,
patch: (change: Partial<CreationForm>) => setForm({ ...form, ...change }),
create: () => settle(createAttempt(input, form)),
confirm: () => {
const { correction } = props.pending
if (correction) settle(confirmAttempt(input, form, correction))
},
}
const result = insert(geometry)
return result.ok
? { status: 'created', id: result.id }
: { status: 'refused', messages: result.messages }
}
function confirmAttempt(
props: CreationPanelProps,
form: CreationForm,
correction: Correction,
): Attempt {
const insert = inserterOf(props, form)
return confirmCorrection(correction, (c) => insert({ type: 'circle', ...c }))
}
/**
@@ -95,80 +78,74 @@ function confirmAttempt(
*/
export default function CreationPanel(props: CreationPanelProps) {
const t = useT()
const { pending, doc, dispatch } = props
const [form, setForm] = useState<CreationForm>(() => emptyForm(doc))
const [messages, setMessages] = useState<string[]>([])
const update = (next: PendingElement) =>
dispatch({ type: 'pending_update', pending: next })
const settle = (attempt: Attempt) => {
setMessages(
attempt.status === 'refused' || attempt.status === 'invalid'
? attempt.messages
: [],
)
if (attempt.status === 'created') {
dispatch({ type: 'select', id: attempt.id ?? null })
}
if (attempt.status === 'correction') {
update({ ...pending, correction: attempt.correction })
}
}
const words = (problem: string) => t(`editor.annotations.required.${problem}`)
const setGeometry = (geometry: EditableGeometry) => {
update({ ...pending, geometry, correction: null })
return { ok: true as const }
}
const creation = useCreation(props)
const { pending, dispatch } = props
const title = t(`editor.annotations.new.${pending.kind}`)
return (
<section
aria-label={t(`editor.annotations.new.${pending.kind}`)}
aria-label={title}
data-testid="annotation-creation"
className="space-y-3 rounded-xl border border-[var(--color-border)] p-4"
>
<h3 className="text-base font-semibold">
{t(`editor.annotations.new.${pending.kind}`)}
</h3>
<h3 className="text-base font-semibold">{title}</h3>
<CreationFields
kind={pending.kind}
viewId={props.viewId}
form={form}
doc={doc}
onChange={(patch) => setForm({ ...form, ...patch })}
form={creation.form}
doc={props.doc}
onChange={creation.patch}
/>
<GeometryGroup
idPrefix="creation"
geometry={pending.geometry}
onApply={setGeometry}
locked={false}
onApply={(geometry) => {
const next = withGeometry(pending, geometry)
dispatch({ type: 'pending_update', pending: next })
return { ok: true }
}}
/>
{pending.correction && (
<CorrectionNotice
correction={pending.correction}
onConfirm={() =>
settle(confirmAttempt(props, form, pending.correction!))
onConfirm={creation.confirm}
onCancel={() =>
dispatch({
type: 'pending_update',
pending: { ...pending, correction: null },
})
}
onCancel={() => update({ ...pending, correction: null })}
/>
)}
<Messages messages={messages} />
<div className="flex gap-2">
<Button
type="button"
onClick={() => settle(createAttempt(props, form, words))}
>
{t('editor.annotations.create')}
</Button>
<Button
type="button"
variant="secondary"
onClick={() => dispatch({ type: 'pending_drop' })}
>
{t('editor.annotations.create_cancel')}
</Button>
</div>
<Messages messages={creation.messages} />
<CreationActions
onCreate={creation.create}
onCancel={() => dispatch({ type: 'pending_drop' })}
/>
</section>
)
}
function CreationActions({
onCreate,
onCancel,
}: {
onCreate: () => void
onCancel: () => void
}) {
const t = useT()
return (
<div className="flex gap-2">
<Button type="button" onClick={onCreate}>
{t('editor.annotations.create')}
</Button>
<Button type="button" variant="secondary" onClick={onCancel}>
{t('editor.annotations.create_cancel')}
</Button>
</div>
)
}
function Messages({ messages }: { messages: string[] }) {
if (messages.length === 0) return null
return (
@@ -182,161 +159,3 @@ function Messages({ messages }: { messages: string[] }) {
</div>
)
}
interface FieldsProps {
doc: AnatomicalSheetDocument
form: CreationForm
onChange: (patch: Partial<CreationForm>) => void
}
interface Option {
value: string
label: string
}
function OptionSelect({
id,
label,
value,
options,
blank,
onChange,
}: {
id: string
label: string
value: string
options: Option[]
/** A « Choisir… » entry: the choice is required and has no default. */
blank: boolean
onChange: (value: string) => void
}) {
const t = useT()
return (
<FormField id={id} label={label}>
<Select id={id} value={value} onChange={(e) => onChange(e.target.value)}>
{blank && <option value="">{t('editor.field.choose')}</option>}
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>
</FormField>
)
}
/** The fields the core requires, collected before the insert. */
function CreationFields({
kind,
viewId,
...fields
}: FieldsProps & { kind: PendingElement['kind']; viewId: string }) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const { doc, form, onChange } = fields
const categories = doc.categories.map((c) => ({
value: c.id,
label: c.name[locale],
}))
const sides = LATERALITIES.map((l: Laterality) => ({
value: l,
label: t(`editor.annotations.laterality.${l}`),
}))
return (
<div className="grid gap-3 sm:grid-cols-2">
<DraftField
id="creation-fr"
label={t('editor.annotations.name_fr')}
value={form.fr}
onChange={(fr) => onChange({ fr })}
/>
<DraftField
id="creation-en"
label={t('editor.annotations.name_en')}
value={form.en}
onChange={(en) => onChange({ en })}
/>
<OptionSelect
id="creation-category"
label={t('editor.annotations.category')}
value={form.categoryId}
options={categories}
blank={false}
onChange={(categoryId) => onChange({ categoryId })}
/>
<OptionSelect
id="creation-laterality"
label={t('editor.annotations.laterality')}
value={form.laterality}
options={sides}
blank
onChange={(laterality) =>
onChange({ laterality: laterality as CreationForm['laterality'] })
}
/>
{kind === 'detail_trigger' ? (
<OpensSelect {...fields} viewId={viewId} />
) : (
<LinkSelects {...fields} />
)}
</div>
)
}
function OpensSelect({
doc,
form,
onChange,
viewId,
}: FieldsProps & { viewId: string }) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const options = directChildren(doc.views, viewId).map((v) => ({
value: v.id,
label: v.name[locale],
}))
return (
<OptionSelect
id="creation-opens"
label={t('editor.annotations.opens')}
value={form.opensViewId}
options={options}
blank
onChange={(opensViewId) => onChange({ opensViewId })}
/>
)
}
/** Findings zone and vertebral region: optional links of a point or a zone. */
function LinkSelects({ doc, form, onChange }: FieldsProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const zones = doc.finding_zones.map((z) => ({
value: z.id,
label: z.name[locale],
}))
const regions = doc.vertebral_config.regions.map((r) => ({
value: r.code,
label: r.name[locale],
}))
return (
<>
<OptionSelect
id="creation-zone"
label={t('editor.annotations.zone')}
value={form.zoneId}
options={zones}
blank
onChange={(zoneId) => onChange({ zoneId })}
/>
<OptionSelect
id="creation-region"
label={t('editor.annotations.region')}
value={form.regionCode}
options={regions}
blank
onChange={(regionCode) => onChange({ regionCode })}
/>
</>
)
}