Files
faunosteo/apps/app/src/components/editor/vertebrae/RegionRow.tsx
T

181 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
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
{...focusAttrs({ kind: 'region', code: region.code })}
className={`space-y-3 rounded-lg border border-[var(--color-border)] p-3 ${FOCUS_CLASS}`}
>
<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>
)
}