feat(app): editor Annotations tab, core-decided placement, repair rules
This commit is contained in:
1 parent
92c595caa1
commit
26097852ff
37 files changed
+5001
-6
No files matched your search
@@ -0,0 +1,342 @@
|
||||
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 Button from '@/components/ui/Button'
|
||||
import Select from '@/components/ui/Select'
|
||||
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,
|
||||
type CreationForm,
|
||||
type PendingElement,
|
||||
} from '@/editor/annotations/creation'
|
||||
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 GeometryGroup from './GeometryGroup'
|
||||
|
||||
export interface CreationPanelProps {
|
||||
pending: PendingElement
|
||||
viewId: string
|
||||
doc: AnatomicalSheetDocument
|
||||
/** F of the current view; null while it is not measured. */
|
||||
frame: Frame | null
|
||||
dispatch: Dispatch<UiAction>
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
/** « 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 }),
|
||||
)
|
||||
}
|
||||
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 }))
|
||||
}
|
||||
|
||||
/**
|
||||
* « Nouveau point / Nouvelle zone / Nouvel accès »: the fields the core requires are
|
||||
* collected first, then « Créer » inserts once through `accept` (no default labels).
|
||||
* The geometry stays here, outside the document, until then.
|
||||
*/
|
||||
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 }
|
||||
}
|
||||
return (
|
||||
<section
|
||||
aria-label={t(`editor.annotations.new.${pending.kind}`)}
|
||||
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>
|
||||
<CreationFields
|
||||
kind={pending.kind}
|
||||
viewId={props.viewId}
|
||||
form={form}
|
||||
doc={doc}
|
||||
onChange={(patch) => setForm({ ...form, ...patch })}
|
||||
/>
|
||||
<GeometryGroup
|
||||
idPrefix="creation"
|
||||
geometry={pending.geometry}
|
||||
onApply={setGeometry}
|
||||
locked={false}
|
||||
/>
|
||||
{pending.correction && (
|
||||
<CorrectionNotice
|
||||
correction={pending.correction}
|
||||
onConfirm={() =>
|
||||
settle(confirmAttempt(props, form, pending.correction!))
|
||||
}
|
||||
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>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function Messages({ messages }: { messages: string[] }) {
|
||||
if (messages.length === 0) return null
|
||||
return (
|
||||
<div
|
||||
role="alert"
|
||||
className="space-y-1 text-sm text-[var(--color-danger-strong)]"
|
||||
>
|
||||
{messages.map((message) => (
|
||||
<p key={message}>{message}</p>
|
||||
))}
|
||||
</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 })}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user