feat(app): editor Général, Références and Vertèbres tabs, delete with dependents

This commit is contained in:
bchanot committed 2026-11-04 12:46:55 +01:00
1 parent 6aba9711f0
commit d163b2ce9a
24 files changed
+2239 -12

No files matched your search

@@ -0,0 +1,167 @@
import { useMemo, useState } from 'react'
import type {
AnatomicalSheetDocument,
Dependent,
ElementKey,
} from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import { dependentsOf } from '@/editor/core/editOps'
import { refusalMessage } from '@/editor/refusal'
import { elementLabel, useSheetDocument } from '@/editor/sheetDocument'
import { useEditorStore } from '@/editor/state/editorStore'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
export interface DeleteTarget {
key: ElementKey
/** The element's name as the list shows it. */
label: string
}
export interface DeleteDialogProps {
/** null = closed. The dialog asks the core for the dependents each time it opens. */
target: DeleteTarget | null
onClose: () => void
}
/**
* Delete with dependents (shared by the Général, Références, Vertèbres, Vues and Constats
* panels): the core lists what still names the element; if anything does, the dialog
* shows the list and offers no deletion (the core would refuse it); descendants go with
* the element and are listed in the confirmation.
*/
export default function DeleteDialog({ target, onClose }: DeleteDialogProps) {
const t = useT()
const doc = useSheetDocument()
const heading = t('editor.delete.title').replace(
'{name}',
target?.label ?? '',
)
return (
<Dialog
open={target !== null && doc !== null}
onClose={onClose}
title={heading}
>
{target && doc && (
<DeleteBody target={target} doc={doc} onClose={onClose} />
)}
</Dialog>
)
}
function DeleteBody({
target,
doc,
onClose,
}: {
target: DeleteTarget
doc: AnatomicalSheetDocument
onClose: () => void
}) {
const t = useT()
const [failure, setFailure] = useState<string | null>(null)
const found = useMemo(
() => dependentsOf(useEditorStore.getState().documentText, target.key),
[target.key],
)
if (!found.ok) {
return (
<Closing message={t('editor.delete.lookup_failed')} onClose={onClose} />
)
}
const blocking = found.value.filter((d) => d.relation !== 'descendant')
const descendants = found.value.filter((d) => d.relation === 'descendant')
if (blocking.length > 0) {
return (
<>
<p className="text-sm">
{t('editor.delete.blocked').replace('{name}', target.label)}
</p>
<DependentList doc={doc} dependents={blocking} withRelation />
<Closing onClose={onClose} />
</>
)
}
const confirm = () => {
const outcome = useEditorStore
.getState()
.commit({ op: 'remove', key: target.key })
if (outcome.ok) onClose()
else setFailure(refusalMessage(t, outcome))
}
return (
<>
<p className="text-sm">
{t('editor.delete.ask').replace('{name}', target.label)}
</p>
{descendants.length > 0 && (
<>
<p className="mt-2 text-sm">{t('editor.delete.descendants')}</p>
<DependentList doc={doc} dependents={descendants} />
</>
)}
{failure && (
<p
role="alert"
className="mt-2 text-sm text-[var(--color-danger-strong)]"
>
{failure}
</p>
)}
<div className="mt-4 flex flex-wrap justify-end gap-2">
<Button type="button" variant="ghost" onClick={onClose}>
{t('editor.delete.cancel')}
</Button>
<Button type="button" variant="danger" onClick={confirm}>
{t('editor.delete.confirm')}
</Button>
</div>
</>
)
}
function Closing({
message,
onClose,
}: {
message?: string
onClose: () => void
}) {
const t = useT()
return (
<>
{message && <p className="text-sm">{message}</p>}
<div className="mt-4 flex justify-end">
<Button type="button" variant="secondary" onClick={onClose}>
{t('editor.delete.close')}
</Button>
</div>
</>
)
}
function DependentList({
doc,
dependents,
withRelation = false,
}: {
doc: AnatomicalSheetDocument
dependents: readonly Dependent[]
withRelation?: boolean
}) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
return (
<ul className="mt-2 list-disc space-y-1 pl-5 text-sm">
{dependents.map((d) => (
<li key={JSON.stringify(d.element)}>
{t(`editor.element.${d.element.kind}`)} «{' '}
{elementLabel(doc, d.element, locale)} »
{withRelation && <> : {t(`editor.relation.${d.relation}`)}</>}
</li>
))}
</ul>
)
}
@@ -0,0 +1,38 @@
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import Input from '@/components/ui/Input'
import Textarea from '@/components/ui/Textarea'
/**
* A controlled field of a creation form: it never commits, the form's own button does
* (BDR-050). The committed fields of existing elements are `TextField`.
*/
export default function DraftField({
id,
label,
value,
onChange,
multiline,
hint,
error,
}: {
id: string
label: string
value: string
onChange: (value: string) => void
multiline?: boolean
hint?: string
error?: string
}) {
const shared = {
id,
value,
onChange: (e: { target: { value: string } }) => onChange(e.target.value),
'aria-invalid': Boolean(error),
'aria-describedby': fieldDescribedBy(id, hint, error),
}
return (
<FormField id={id} label={label} hint={hint} error={error}>
{multiline ? <Textarea {...shared} /> : <Input {...shared} />}
</FormField>
)
}
@@ -0,0 +1,41 @@
import { useT } from '@/i18n/useT'
import ActionButton from './ActionButton'
/** ↑ / ↓ for a list row: one `move` per press; absent while read-only. */
export default function MoveButtons({
index,
count,
readOnly,
onMove,
}: {
index: number
count: number
readOnly: boolean
onMove: (position: number) => void
}) {
const t = useT()
return (
<>
<ActionButton
readOnly={readOnly}
variant="ghost"
size="sm"
aria-label={t('editor.action.move_up')}
disabled={index === 0}
onClick={() => onMove(index - 1)}
>
↑
</ActionButton>
<ActionButton
readOnly={readOnly}
variant="ghost"
size="sm"
aria-label={t('editor.action.move_down')}
disabled={index >= count - 1}
onClick={() => onMove(index + 1)}
>
↓
</ActionButton>
</>
)
}
@@ -0,0 +1,129 @@
import { useState } from 'react'
import type {
AnatomicalSheetDocument,
Attribution,
} from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import TextField from '@/components/editor/fields/TextField'
import { isBlank, nullable, textsOf } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
/** Authors (add / remove), licence and notes. */
export default function AttributionSection({
doc,
readOnly,
}: {
doc: AnatomicalSheetDocument
readOnly: boolean
}) {
const t = useT()
const edit = useEditCommand()
const attribution = doc.attribution
const send = (field: string, patch: Partial<Attribution>) =>
edit.run(field, {
op: 'update',
key: { kind: 'sheet' },
element: {
kind: 'sheet',
value: { ...textsOf(doc), attribution: { ...attribution, ...patch } },
},
})
return (
<section aria-labelledby="general-attribution" className="space-y-3">
<h3 id="general-attribution" className="text-base font-semibold">
{t('editor.general.attribution')}
</h3>
<Authors
authors={attribution.authors}
readOnly={readOnly}
error={edit.fieldError('authors')}
onChange={(authors) => send('authors', { authors })}
/>
<TextField
id="general-license"
label={t('editor.general.license')}
value={attribution.license ?? ''}
onCommit={(value) => send('license', { license: nullable(value) })}
readOnly={readOnly}
error={edit.fieldError('license')}
/>
<TextField
id="general-notes"
label={t('editor.general.notes')}
value={attribution.notes ?? ''}
onCommit={(value) => send('notes', { notes: nullable(value) })}
readOnly={readOnly}
multiline
error={edit.fieldError('notes')}
/>
</section>
)
}
function Authors({
authors,
readOnly,
error,
onChange,
}: {
authors: readonly string[]
readOnly: boolean
error?: string
onChange: (authors: string[]) => boolean
}) {
const t = useT()
const [draft, setDraft] = useState('')
const add = () => {
if (isBlank(draft)) return
if (onChange([...authors, draft])) setDraft('')
}
return (
<div className="space-y-2">
<p className="text-sm font-medium">{t('editor.general.authors')}</p>
{authors.length === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">—</p>
)}
<ul className="space-y-1">
{authors.map((author, index) => (
<li
key={`${index}-${author}`}
className="flex items-center gap-2 text-sm"
>
<span className="min-w-0 flex-1 break-words">{author}</span>
<ActionButton
readOnly={readOnly}
variant="ghost"
size="sm"
aria-label={t('editor.general.author_remove').replace(
'{name}',
author,
)}
onClick={() => onChange(authors.filter((_, i) => i !== index))}
>
{t('editor.action.remove')}
</ActionButton>
</li>
))}
</ul>
{!readOnly && (
<div className="flex items-end gap-2">
<div className="min-w-0 flex-1">
<DraftField
id="general-author-new"
label={t('editor.general.author_new')}
value={draft}
onChange={setDraft}
error={error}
/>
</div>
<ActionButton readOnly={readOnly} variant="secondary" onClick={add}>
{t('editor.general.author_add')}
</ActionButton>
</div>
)}
</div>
)
}
@@ -0,0 +1,173 @@
import { useState } from 'react'
import type {
AnatomicalSheetDocument,
AnnotationCategory,
} from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import TextField from '@/components/editor/fields/TextField'
import DeleteDialog, {
type DeleteTarget,
} from '@/components/editor/DeleteDialog'
import { isBlank, nullable } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import type { Locale } from '@/i18n/types'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
/** Annotation categories: list, rename, colour, add, delete with dependents. */
export default function CategoriesSection({
doc,
readOnly,
}: {
doc: AnatomicalSheetDocument
readOnly: boolean
}) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
return (
<section aria-labelledby="general-categories" className="space-y-3">
<h3 id="general-categories" className="text-base font-semibold">
{t('editor.general.categories')}
</h3>
<ul className="space-y-3">
{doc.categories.map((category) => (
<CategoryRow
key={category.id}
category={category}
readOnly={readOnly}
onDelete={() =>
setDeleting({
key: { kind: 'category', id: category.id },
label: category.name[locale],
})
}
/>
))}
</ul>
{!readOnly && <CategoryForm />}
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
</section>
)
}
function CategoryRow({
category,
readOnly,
onDelete,
}: {
category: AnnotationCategory
readOnly: boolean
onDelete: () => void
}) {
const t = useT()
const edit = useEditCommand()
const send = (field: string, patch: Partial<AnnotationCategory>) =>
edit.run(field, {
op: 'update',
key: { kind: 'category', id: category.id },
element: { kind: 'category', value: { ...category, ...patch } },
})
const rename = (locale: Locale) => (value: string) =>
isBlank(value)
? edit.reject(locale, 'editor.field.required')
: send(locale, { name: { ...category.name, [locale]: value } })
const prefix = `category-${category.id}`
return (
<li className="rounded-lg border border-[var(--color-border)] p-3">
<div className="grid gap-3 md:grid-cols-3">
{(['fr', 'en'] as const).map((locale) => (
<TextField
key={locale}
id={`${prefix}-name-${locale}`}
label={t(`editor.general.category_name_${locale}`)}
value={category.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
error={edit.fieldError(locale)}
/>
))}
<TextField
id={`${prefix}-color`}
label={t('editor.general.category_color')}
value={category.color ?? ''}
onCommit={(value) => send('color', { color: nullable(value) })}
readOnly={readOnly}
hint={t('editor.general.category_color_hint')}
error={edit.fieldError('color')}
/>
</div>
<div className="mt-2 flex justify-end">
<ActionButton
readOnly={readOnly}
variant="danger"
size="sm"
onClick={onDelete}
>
{t('editor.action.delete')}
</ActionButton>
</div>
</li>
)
}
function CategoryForm() {
const t = useT()
const edit = useEditCommand()
const [fr, setFr] = useState('')
const [en, setEn] = useState('')
const [color, setColor] = useState('')
const create = () => {
if (isBlank(fr) || isBlank(en)) {
edit.reject('form', 'editor.field.required_names')
return
}
const added = edit.runNew('form', 'cat', (id) => ({
op: 'insert',
element: {
kind: 'category',
value: { id, name: { fr, en }, color: nullable(color) },
},
}))
if (added) {
setFr('')
setEn('')
setColor('')
}
}
return (
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
<p className="text-sm font-medium">{t('editor.general.category_new')}</p>
<div className="grid gap-3 md:grid-cols-3">
<DraftField
id="category-new-fr"
label={t('editor.general.category_name_fr')}
value={fr}
onChange={setFr}
/>
<DraftField
id="category-new-en"
label={t('editor.general.category_name_en')}
value={en}
onChange={setEn}
/>
<DraftField
id="category-new-color"
label={t('editor.general.category_color')}
value={color}
onChange={setColor}
hint={t('editor.general.category_color_hint')}
/>
</div>
{edit.fieldError('form') && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('form')}
</p>
)}
<ActionButton readOnly={false} variant="secondary" onClick={create}>
{t('editor.general.category_add')}
</ActionButton>
</div>
)
}
@@ -0,0 +1,70 @@
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
import TextField from '@/components/editor/fields/TextField'
import { isBlank, optionalText, textsOf } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import type { Locale } from '@/i18n/types'
import { useT } from '@/i18n/useT'
const LOCALES: readonly Locale[] = ['fr', 'en']
/** Names, description and scope disclaimer of the sheet, FR and EN. */
export default function TextsSection({
doc,
readOnly,
}: {
doc: AnatomicalSheetDocument
readOnly: boolean
}) {
const t = useT()
const edit = useEditCommand()
const send = (field: string, patch: Partial<ReturnType<typeof textsOf>>) =>
edit.run(field, {
op: 'update',
key: { kind: 'sheet' },
element: { kind: 'sheet', value: { ...textsOf(doc), ...patch } },
})
const rename = (locale: Locale) => (value: string) => {
const field = `name-${locale}`
if (isBlank(value)) return edit.reject(field, 'editor.field.required')
return send(field, { name: { ...doc.name, [locale]: value } })
}
const describe =
(key: 'description' | 'scope_disclaimer', locale: Locale) =>
(value: string) =>
send(`${key}-${locale}`, { [key]: optionalText(doc[key], locale, value) })
return (
<section aria-labelledby="general-texts" className="space-y-3">
<h3 id="general-texts" className="text-base font-semibold">
{t('editor.general.texts')}
</h3>
<div className="grid gap-3 md:grid-cols-2">
{LOCALES.map((locale) => (
<TextField
key={`name-${locale}`}
id={`general-name-${locale}`}
label={t(`editor.general.name_${locale}`)}
value={doc.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
error={edit.fieldError(`name-${locale}`)}
/>
))}
{(['description', 'scope_disclaimer'] as const).flatMap((key) =>
LOCALES.map((locale) => (
<TextField
key={`${key}-${locale}`}
id={`general-${key}-${locale}`}
label={t(`editor.general.${key}_${locale}`)}
value={doc[key][locale] ?? ''}
onCommit={describe(key, locale)}
readOnly={readOnly}
multiline
error={edit.fieldError(`${key}-${locale}`)}
/>
)),
)}
</div>
</section>
)
}
@@ -0,0 +1,87 @@
import { useState } from 'react'
import type { ReferenceType } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import SelectField from '@/components/editor/fields/SelectField'
import { isBlank, nullable } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
import { REFERENCE_TYPES } from './referenceTypes'
const EMPTY = { type: '', citation: '', url: '', doi: '' }
/** Adds a reference: the type and the citation are required, URL and DOI optional. */
export default function ReferenceForm() {
const t = useT()
const edit = useEditCommand()
const [form, setForm] = useState(EMPTY)
const set = (patch: Partial<typeof EMPTY>) => setForm({ ...form, ...patch })
const options = [
{ value: '', label: t('editor.field.choose') },
...REFERENCE_TYPES.map((type) => ({
value: type,
label: t(`editor.reference.type.${type}`),
})),
]
const create = () => {
if (form.type === '' || isBlank(form.citation)) {
edit.reject('form', 'editor.reference.required')
return
}
const added = edit.runNew('form', 'ref', (id) => ({
op: 'insert',
element: {
kind: 'reference',
value: {
id,
type: form.type as ReferenceType,
citation: form.citation,
url: nullable(form.url),
doi: nullable(form.doi),
},
},
}))
if (added) setForm(EMPTY)
}
return (
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
<p className="text-sm font-medium">{t('editor.reference.new')}</p>
<div className="grid gap-3 md:grid-cols-2">
<SelectField
id="reference-new-type"
label={t('editor.reference.type')}
value={form.type}
options={options}
onCommit={(type) => set({ type })}
/>
<DraftField
id="reference-new-citation"
label={t('editor.reference.citation')}
value={form.citation}
onChange={(citation) => set({ citation })}
multiline
/>
<DraftField
id="reference-new-url"
label={t('editor.reference.url')}
value={form.url}
onChange={(url) => set({ url })}
/>
<DraftField
id="reference-new-doi"
label={t('editor.reference.doi')}
value={form.doi}
onChange={(doi) => set({ doi })}
/>
</div>
{edit.fieldError('form') && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('form')}
</p>
)}
<ActionButton readOnly={false} variant="secondary" onClick={create}>
{t('editor.reference.add')}
</ActionButton>
</div>
)
}
@@ -0,0 +1,104 @@
import type { Reference, ReferenceType } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import MoveButtons from '@/components/editor/fields/MoveButtons'
import SelectField from '@/components/editor/fields/SelectField'
import TextField from '@/components/editor/fields/TextField'
import { isBlank, nullable } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
import { REFERENCE_TYPES } from './referenceTypes'
/** One reference: fields commit one by one, ↑ / ↓ reorder, delete asks the core. */
export default function ReferenceRow({
reference,
index,
count,
readOnly,
onDelete,
}: {
reference: Reference
index: number
count: number
readOnly: boolean
onDelete: () => void
}) {
const t = useT()
const edit = useEditCommand()
const key = { kind: 'reference', id: reference.id } as const
const send = (field: string, patch: Partial<Reference>) =>
edit.run(field, {
op: 'update',
key,
element: { kind: 'reference', value: { ...reference, ...patch } },
})
const options = REFERENCE_TYPES.map((type) => ({
value: type,
label: t(`editor.reference.type.${type}`),
}))
const prefix = `reference-${reference.id}`
return (
<li className="space-y-3 rounded-lg border border-[var(--color-border)] p-3">
<div className="grid gap-3 md:grid-cols-2">
<SelectField
id={`${prefix}-type`}
label={t('editor.reference.type')}
value={reference.type}
options={options}
onCommit={(type) => send('type', { type: type as ReferenceType })}
readOnly={readOnly}
error={edit.fieldError('type')}
/>
<TextField
id={`${prefix}-citation`}
label={t('editor.reference.citation')}
value={reference.citation}
onCommit={(value) =>
isBlank(value)
? edit.reject('citation', 'editor.field.required')
: send('citation', { citation: value })
}
readOnly={readOnly}
multiline
error={edit.fieldError('citation')}
/>
<TextField
id={`${prefix}-url`}
label={t('editor.reference.url')}
value={reference.url ?? ''}
onCommit={(value) => send('url', { url: nullable(value) })}
readOnly={readOnly}
error={edit.fieldError('url')}
/>
<TextField
id={`${prefix}-doi`}
label={t('editor.reference.doi')}
value={reference.doi ?? ''}
onCommit={(value) => send('doi', { doi: nullable(value) })}
readOnly={readOnly}
error={edit.fieldError('doi')}
/>
</div>
<div className="flex flex-wrap items-center justify-end gap-1">
<MoveButtons
index={index}
count={count}
readOnly={readOnly}
onMove={(position) => edit.run('move', { op: 'move', key, position })}
/>
<ActionButton
readOnly={readOnly}
variant="danger"
size="sm"
onClick={onDelete}
>
{t('editor.action.delete')}
</ActionButton>
</div>
{edit.fieldError('move') && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('move')}
</p>
)}
</li>
)
}
@@ -0,0 +1,10 @@
import type { ReferenceType } from '@faunosteo/core-wasm/types'
export const REFERENCE_TYPES: readonly ReferenceType[] = [
'book',
'journal',
'standard',
'legal',
'web',
'manual',
]
@@ -1,14 +1,27 @@
import AttributionSection from '@/components/editor/general/AttributionSection'
import CategoriesSection from '@/components/editor/general/CategoriesSection'
import TextsSection from '@/components/editor/general/TextsSection'
import { useSheetDocument } from '@/editor/sheetDocument'
import type { EditorTabProps } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
/** Placeholder: Lot F replaces the body and keeps the props contract (`EditorTabProps`). */
export default function GeneralTab(_props: EditorTabProps) {
/** Général: the sheet's texts, attribution and annotation categories. */
export default function GeneralTab({ readOnly }: EditorTabProps) {
const t = useT()
const doc = useSheetDocument()
return (
<section
data-testid="editor-tab-general"
aria-label={t('editor.tab.general')}
className="min-h-24"
/>
className="min-h-24 space-y-8"
>
{doc && (
<>
<TextsSection doc={doc} readOnly={readOnly} />
<AttributionSection doc={doc} readOnly={readOnly} />
<CategoriesSection doc={doc} readOnly={readOnly} />
</>
)}
</section>
)
}
@@ -1,14 +1,49 @@
import { useState } from 'react'
import DeleteDialog, {
type DeleteTarget,
} from '@/components/editor/DeleteDialog'
import ReferenceForm from '@/components/editor/references/ReferenceForm'
import ReferenceRow from '@/components/editor/references/ReferenceRow'
import { useSheetDocument } from '@/editor/sheetDocument'
import type { EditorTabProps } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
/** Placeholder: Lot F replaces the body and keeps the props contract (`EditorTabProps`). */
export default function ReferencesTab(_props: EditorTabProps) {
/** Références: the sheet's bibliography, cited by id from the findings. */
export default function ReferencesTab({ readOnly }: EditorTabProps) {
const t = useT()
const doc = useSheetDocument()
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
const references = doc?.references ?? []
return (
<section
data-testid="editor-tab-references"
aria-label={t('editor.tab.references')}
className="min-h-24"
/>
className="min-h-24 space-y-3"
>
{doc && references.length === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.reference.empty')}
</p>
)}
<ul className="space-y-3">
{references.map((reference, index) => (
<ReferenceRow
key={reference.id}
reference={reference}
index={index}
count={references.length}
readOnly={readOnly}
onDelete={() =>
setDeleting({
key: { kind: 'reference', id: reference.id },
label: reference.citation,
})
}
/>
))}
</ul>
{doc && !readOnly && <ReferenceForm />}
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
</section>
)
}
@@ -1,14 +1,51 @@
import { useState } from 'react'
import DeleteDialog, {
type DeleteTarget,
} from '@/components/editor/DeleteDialog'
import RegionForm from '@/components/editor/vertebrae/RegionForm'
import RegionRow from '@/components/editor/vertebrae/RegionRow'
import { useSheetDocument } from '@/editor/sheetDocument'
import type { EditorTabProps } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
/** Placeholder: Lot F replaces the body and keeps the props contract (`EditorTabProps`). */
export default function VertebraeTab(_props: EditorTabProps) {
/** Vertèbres: the vertebral regions (code, names, count, optional range), ordered. */
export default function VertebraeTab({ readOnly }: EditorTabProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const doc = useSheetDocument()
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
const regions = doc?.vertebral_config.regions ?? []
return (
<section
data-testid="editor-tab-vertebrae"
aria-label={t('editor.tab.vertebrae')}
className="min-h-24"
/>
className="min-h-24 space-y-3"
>
{doc && regions.length === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.vertebra.empty')}
</p>
)}
<ul className="space-y-3">
{regions.map((region, index) => (
<RegionRow
key={region.code}
region={region}
index={index}
count={regions.length}
readOnly={readOnly}
onDelete={() =>
setDeleting({
key: { kind: 'region', code: region.code },
label: region.name[locale],
})
}
/>
))}
</ul>
{doc && !readOnly && <RegionForm />}
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
</section>
)
}
@@ -0,0 +1,33 @@
import DraftField from '@/components/editor/fields/DraftField'
import { useT } from '@/i18n/useT'
/** The optional count range, two controlled bounds; the caller commits them. */
export default function RangeFields({
idPrefix,
min,
max,
onChange,
}: {
idPrefix: string
min: string
max: string
onChange: (min: string, max: string) => void
}) {
const t = useT()
return (
<>
<DraftField
id={`${idPrefix}-min`}
label={t('editor.vertebra.range_min')}
value={min}
onChange={(value) => onChange(value, max)}
/>
<DraftField
id={`${idPrefix}-max`}
label={t('editor.vertebra.range_max')}
value={max}
onChange={(value) => onChange(min, value)}
/>
</>
)
}
@@ -0,0 +1,89 @@
import { useState } from 'react'
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import { isBlank } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
import RangeFields from './RangeFields'
import { parseRange, wholeNumber } from './numbers'
const EMPTY = { code: '', fr: '', en: '', count: '', min: '', max: '' }
/** Adds a region: code (typed once, read-only afterwards), names, count, optional range. */
export default function RegionForm() {
const t = useT()
const edit = useEditCommand()
const [form, setForm] = useState(EMPTY)
const set = (patch: Partial<typeof EMPTY>) => setForm({ ...form, ...patch })
const create = () => {
const count = wholeNumber(form.count)
const range = parseRange(form.min, form.max)
if (isBlank(form.code) || isBlank(form.fr) || isBlank(form.en)) {
edit.reject('form', 'editor.vertebra.required')
} else if (count === null) {
edit.reject('form', 'editor.field.integer')
} else if (!range.ok) {
edit.reject('form', `editor.vertebra.range_${range.reason}`)
} else if (
edit.run('form', {
op: 'insert',
element: {
kind: 'region',
value: {
code: form.code,
name: { fr: form.fr, en: form.en },
count,
count_range: range.range,
},
},
})
) {
setForm(EMPTY)
}
}
return (
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
<p className="text-sm font-medium">{t('editor.vertebra.new')}</p>
<div className="grid gap-3 md:grid-cols-4">
<DraftField
id="region-new-code"
label={t('editor.vertebra.code')}
value={form.code}
onChange={(code) => set({ code })}
/>
<DraftField
id="region-new-fr"
label={t('editor.vertebra.name_fr')}
value={form.fr}
onChange={(fr) => set({ fr })}
/>
<DraftField
id="region-new-en"
label={t('editor.vertebra.name_en')}
value={form.en}
onChange={(en) => set({ en })}
/>
<DraftField
id="region-new-count"
label={t('editor.vertebra.count')}
value={form.count}
onChange={(count) => set({ count })}
/>
<RangeFields
idPrefix="region-new-range"
min={form.min}
max={form.max}
onChange={(min, max) => set({ min, max })}
/>
</div>
{edit.fieldError('form') && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('form')}
</p>
)}
<ActionButton readOnly={false} variant="secondary" onClick={create}>
{t('editor.vertebra.add')}
</ActionButton>
</div>
)
}
@@ -0,0 +1,176 @@
import { useState } from 'react'
import type { VertebralRegion } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import MoveButtons from '@/components/editor/fields/MoveButtons'
import ReadOnlyValue from '@/components/editor/fields/ReadOnlyValue'
import TextField from '@/components/editor/fields/TextField'
import { isBlank } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import type { Locale } from '@/i18n/types'
import { useT } from '@/i18n/useT'
import RangeFields from './RangeFields'
import { parseRange, wholeNumber } from './numbers'
const textOf = (value: number | undefined): string =>
value === undefined ? '' : String(value)
/** One vertebral region: the code is read-only after creation, the rest commits per field. */
export default function RegionRow({
region,
index,
count,
readOnly,
onDelete,
}: {
region: VertebralRegion
index: number
count: number
readOnly: boolean
onDelete: () => void
}) {
const t = useT()
const edit = useEditCommand()
const key = { kind: 'region', code: region.code } as const
const send = (field: string, patch: Partial<VertebralRegion>) =>
edit.run(field, {
op: 'update',
key,
element: { kind: 'region', value: { ...region, ...patch } },
})
const rename = (locale: Locale) => (value: string) =>
isBlank(value)
? edit.reject(locale, 'editor.field.required')
: send(locale, { name: { ...region.name, [locale]: value } })
const recount = (value: string) => {
const parsed = wholeNumber(value)
return parsed === null
? edit.reject('count', 'editor.field.integer')
: send('count', { count: parsed })
}
const prefix = `region-${region.code}`
return (
<li className="space-y-3 rounded-lg border border-[var(--color-border)] p-3">
<div className="grid gap-3 md:grid-cols-4">
<ReadOnlyValue
id={`${prefix}-code`}
label={t('editor.vertebra.code')}
value={region.code}
/>
{(['fr', 'en'] as const).map((locale) => (
<TextField
key={locale}
id={`${prefix}-name-${locale}`}
label={t(`editor.vertebra.name_${locale}`)}
value={region.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
error={edit.fieldError(locale)}
/>
))}
<TextField
id={`${prefix}-count`}
label={t('editor.vertebra.count')}
value={String(region.count)}
onCommit={recount}
readOnly={readOnly}
error={edit.fieldError('count')}
/>
</div>
<RangeEditor
region={region}
readOnly={readOnly}
onApply={(range) => send('range', { count_range: range })}
reject={(key) => edit.reject('range', key)}
error={edit.fieldError('range')}
/>
<div className="flex flex-wrap items-center justify-end gap-1">
<MoveButtons
index={index}
count={count}
readOnly={readOnly}
onMove={(position) => edit.run('move', { op: 'move', key, position })}
/>
<ActionButton
readOnly={readOnly}
variant="danger"
size="sm"
onClick={onDelete}
>
{t('editor.action.delete')}
</ActionButton>
</div>
{edit.fieldError('move') && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('move')}
</p>
)}
</li>
)
}
function RangeEditor({
region,
readOnly,
onApply,
reject,
error,
}: {
region: VertebralRegion
readOnly: boolean
onApply: (range: VertebralRegion['count_range']) => boolean
reject: (messageKey: string) => false
error?: string
}) {
const t = useT()
const saved = {
min: textOf(region.count_range?.min),
max: textOf(region.count_range?.max),
}
const [draft, setDraft] = useState(saved)
const [seen, setSeen] = useState(saved)
if (seen.min !== saved.min || seen.max !== saved.max) {
setSeen(saved)
setDraft(saved)
}
if (readOnly) {
const text = region.count_range ? `${saved.min} – ${saved.max}` : ''
return (
<ReadOnlyValue
id={`region-${region.code}-range`}
label={t('editor.vertebra.range')}
value={text}
/>
)
}
const apply = () => {
const parsed = parseRange(draft.min, draft.max)
if (parsed.ok) onApply(parsed.range)
else reject(`editor.vertebra.range_${parsed.reason}`)
}
return (
<div className="grid items-end gap-3 md:grid-cols-4">
<RangeFields
idPrefix={`region-${region.code}-range`}
min={draft.min}
max={draft.max}
onChange={(min, max) => setDraft({ min, max })}
/>
<ActionButton
readOnly={readOnly}
variant="secondary"
size="sm"
onClick={apply}
>
{t('editor.vertebra.range_apply')}
</ActionButton>
{error && (
<p
role="alert"
className="text-sm text-[var(--color-danger-strong)] md:col-span-4"
>
{error}
</p>
)}
</div>
)
}
@@ -0,0 +1,21 @@
import type { CountRange } from '@faunosteo/core-wasm/types'
/** Digits only: the core decides whether the value is allowed. */
export function wholeNumber(text: string): number | null {
return /^\d+$/.test(text.trim()) ? Number(text.trim()) : null
}
export type RangeInput =
| { ok: true; range: CountRange | null }
| { ok: false; reason: 'pair' | 'number' }
/** Both bounds or none: a half-filled range never reaches the core. */
export function parseRange(min: string, max: string): RangeInput {
const blank = (text: string) => text.trim() === ''
if (blank(min) && blank(max)) return { ok: true, range: null }
if (blank(min) || blank(max)) return { ok: false, reason: 'pair' }
const low = wholeNumber(min)
const high = wholeNumber(max)
if (low === null || high === null) return { ok: false, reason: 'number' }
return { ok: true, range: { min: low, max: high } }
}