fix(editor): Task 2 review round 1, trace calls, form hooks, keyboard proof

This commit is contained in:
bchanot committed 2026-11-13 19:39:29 +01:00
1 parent c893dec60b
commit b703333b20
15 files changed
+476 -261

No files matched your search

@@ -6,10 +6,9 @@ import type {
} from '@faunosteo/core-wasm/types'
import DraftField from '@/components/editor/fields/DraftField'
import SelectField from '@/components/editor/fields/SelectField'
import DiscardConfirm from '@/components/editor/findings/DiscardConfirm'
import { FormActions } from '@/components/editor/findings/DiscardConfirm'
import ReadOnlyNotice from '@/components/editor/findings/ReadOnlyNotice'
import { useDirtyGuard } from '@/components/editor/findings/useDirtyGuard'
import Button from '@/components/ui/Button'
import { useFormDraft } from '@/components/editor/findings/useDirtyGuard'
import {
buildItems,
type FindingItem,
@@ -45,32 +44,35 @@ export interface FindingCreateFormProps {
onClose: (forced: boolean) => void
}
/**
* Collects what the core requires (names FR/EN, the kind of a category), then one
* `insert`: no element exists with default labels. The fields are a local draft: nothing
* is written before « Créer », a close or a move never creates (R2F-3).
*/
export default function FindingCreateForm(props: FindingCreateFormProps) {
const { pending, readOnly, guards, onCreated, onClose } = props
const t = useT()
interface Draft {
fr: string
en: string
kind: string
}
const EMPTY: Draft = { fr: '', en: '', kind: '' }
const sameDraft = (a: Draft, b: Draft) =>
a.fr === b.fr && a.en === b.en && a.kind === b.kind
/** What the core requires before an insert: both names, a zone and a kind for a category. */
function isComplete(draft: Draft, pending: PendingFinding, parent: unknown) {
if (isBlank(draft.fr) || isBlank(draft.en)) return false
return pending.kind !== 'category' || (parent !== null && draft.kind !== '')
}
/** The form's state and its one `insert` (« Créer »), refused on a missing field. */
function useCreateForm(props: FindingCreateFormProps) {
const { pending, guards, onCreated } = props
const edit = useEditCommand()
const zones = useZoneChoice()
const [fr, setFr] = useState('')
const [en, setEn] = useState('')
const [kind, setKind] = useState('')
const guard = useDirtyGuard(guards, fr !== '' || en !== '' || kind !== '')
const form = useFormDraft(EMPTY, guards, sameDraft)
const create = () => {
const parent = pending.kind === 'category' ? zones.chosen : pending.parent
const missingParent = pending.kind === 'category' && !parent
const incomplete =
isBlank(fr) ||
isBlank(en) ||
missingParent ||
(pending.kind === 'category' && !kind)
if (incomplete) {
if (!isComplete(form.draft, pending, parent)) {
edit.reject('form', 'editor.findings.required')
return
}
const { fr, en, kind } = form.draft
let createdId = ''
const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
createdId = id
@@ -79,6 +81,58 @@ export default function FindingCreateForm(props: FindingCreateFormProps) {
})
if (added) onCreated(createdId, parent)
}
return { ...form, zones, create, error: edit.fieldError('form') }
}
/** The kind choice of a category and its two names, as draft fields. */
function CreateFields(props: {
form: ReturnType<typeof useCreateForm>
category: boolean
readOnly: boolean
}) {
const { form, category, readOnly } = props
const t = useT()
const kinds = CATEGORY_KINDS.map((k) => ({
value: k,
label: t(`editor.findings.kind.${k}`),
}))
return (
<div className="grid gap-3 md:grid-cols-2">
{category && <ZoneSelect zones={form.zones} readOnly={readOnly} />}
{category && (
<SelectField
id="finding-new-kind"
label={t('editor.findings.kind')}
value={form.draft.kind}
readOnly={readOnly}
options={[{ value: '', label: t('editor.field.choose') }, ...kinds]}
onCommit={(kind) => form.set({ kind })}
/>
)}
{(['fr', 'en'] as const).map((l) => (
<DraftField
key={l}
id={`finding-new-name-${l}`}
label={t(`editor.general.name_${l}`)}
value={form.draft[l]}
onChange={(value) => form.set({ [l]: value })}
readOnly={readOnly}
/>
))}
</div>
)
}
/**
* Collects what the core requires (names FR/EN, the kind of a category), then one
* `insert`: no element exists with default labels. The fields are a local draft: nothing
* is written before « Créer », a close or a move never creates (R2F-3).
*/
export default function FindingCreateForm(props: FindingCreateFormProps) {
const { pending, readOnly, onClose } = props
const t = useT()
const form = useCreateForm(props)
const { guard } = form
const category = pending.kind === 'category'
return (
<form
@@ -87,68 +141,25 @@ export default function FindingCreateForm(props: FindingCreateFormProps) {
noValidate
onSubmit={(event) => {
event.preventDefault()
create()
form.create()
}}
>
{readOnly && <ReadOnlyNotice />}
{category && zones.chosen && <ZoneTitle zone={zones.chosen} />}
<div className="grid gap-3 md:grid-cols-2">
{category && <ZoneSelect zones={zones} readOnly={readOnly} />}
{category && (
<SelectField
id="finding-new-kind"
label={t('editor.findings.kind')}
value={kind}
readOnly={readOnly}
options={[
{ value: '', label: t('editor.field.choose') },
...CATEGORY_KINDS.map((k) => ({
value: k,
label: t(`editor.findings.kind.${k}`),
})),
]}
onCommit={setKind}
/>
)}
<DraftField
id="finding-new-name-fr"
label={t('editor.general.name_fr')}
value={fr}
onChange={setFr}
readOnly={readOnly}
/>
<DraftField
id="finding-new-name-en"
label={t('editor.general.name_en')}
value={en}
onChange={setEn}
readOnly={readOnly}
/>
</div>
{edit.fieldError('form') && (
{category && form.zones.chosen && <ZoneTitle zone={form.zones.chosen} />}
<CreateFields form={form} category={category} readOnly={readOnly} />
{form.error && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('form')}
{form.error}
</p>
)}
{guard.confirming ? (
<DiscardConfirm
onKeepTyping={guard.keepTyping}
onDiscard={() => onClose(true)}
/>
) : (
<div className="flex flex-wrap justify-end gap-2">
<Button
type="button"
variant="ghost"
onClick={() => onClose(readOnly)}
>
{t(readOnly ? 'editor.findings.discard' : 'editor.findings.cancel')}
</Button>
<Button type="submit" disabled={readOnly}>
{t('editor.findings.create')}
</Button>
</div>
)}
<FormActions
confirming={guard.confirming}
readOnly={readOnly}
submitLabel={t('editor.findings.create')}
onCancel={() => onClose(readOnly)}
onKeepTyping={guard.keepTyping}
onDiscard={() => onClose(true)}
/>
</form>
)
}