fix(editor): free paths never reshaped on attach, category zone chosen explicitly

This commit is contained in:
bchanot committed 2026-11-04 22:14:59 +01:00
1 parent f1175bb3a1
commit a69eba569b
13 files changed
+333 -117

No files matched your search

@@ -7,9 +7,13 @@ import type {
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import SelectField from '@/components/editor/fields/SelectField'
import type { FindingItem, FindingKind } from '@/editor/findings/tree'
import {
buildItems,
type FindingItem,
type FindingKind,
} from '@/editor/findings/tree'
import type { IdPrefix } from '@/editor/ids'
import { elementLabel, isBlank, useSheetDocument } from '@/editor/sheetDocument'
import { isBlank, useSheetDocument } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
@@ -23,7 +27,7 @@ export const CATEGORY_KINDS: readonly FindingCategoryKind[] = [
export interface PendingFinding {
kind: FindingKind
/** The zone, category or node the new element goes under; null for a zone. */
/** The category or node a node goes under; null for a zone and a category. */
parent: FindingItem | null
}
@@ -37,18 +41,24 @@ export default function FindingCreateForm({
onCancel,
}: {
pending: PendingFinding
onCreated: (id: string) => void
onCreated: (id: string, parent: FindingItem | null) => void
onCancel: () => void
}) {
const t = useT()
const title = useFormTitle(pending)
const edit = useEditCommand()
const zones = useZoneChoice()
const title = useFormTitle(pending, zones.chosen)
const [fr, setFr] = useState('')
const [en, setEn] = useState('')
const [kind, setKind] = useState('')
const create = () => {
const parent = pending.kind === 'category' ? zones.chosen : pending.parent
const missingParent = pending.kind === 'category' && !parent
const incomplete =
isBlank(fr) || isBlank(en) || (pending.kind === 'category' && !kind)
isBlank(fr) ||
isBlank(en) ||
missingParent ||
(pending.kind === 'category' && !kind)
if (incomplete) {
edit.reject('form', 'editor.findings.required')
return
@@ -56,14 +66,16 @@ export default function FindingCreateForm({
let createdId = ''
const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
createdId = id
return insertCommand(pending, id, { fr: fr.trim(), en: en.trim() }, kind)
const name = { fr: fr.trim(), en: en.trim() }
return insertCommand({ ...pending, parent }, id, name, kind)
})
if (added) onCreated(createdId)
if (added) onCreated(createdId, parent)
}
return (
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
<p className="text-sm font-medium">{title}</p>
<div className="grid gap-3 md:grid-cols-2">
{pending.kind === 'category' && <ZoneSelect zones={zones} />}
{pending.kind === 'category' && (
<SelectField
id="finding-new-kind"
@@ -109,19 +121,58 @@ export default function FindingCreateForm({
)
}
/** A category lands at zone level, so its title names the zone, whatever is selected. */
function useFormTitle(pending: PendingFinding): string {
const t = useT()
interface ZoneChoice {
zones: readonly FindingItem[]
zoneId: string
setZoneId: (id: string) => void
chosen: FindingItem | null
}
/** The zones to choose from; with exactly one, it is preselected, otherwise nothing is. */
function useZoneChoice(): ZoneChoice {
const doc = useSheetDocument()
const zones = doc ? buildItems(doc).filter((i) => i.kind === 'zone') : []
const [zoneId, setZoneId] = useState(
zones.length === 1 ? zones[0].zoneId : '',
)
const chosen = zones.find((zone) => zone.zoneId === zoneId) ?? null
return { zones, zoneId, setZoneId, chosen }
}
function ZoneSelect({ zones }: { zones: ZoneChoice }) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
if (pending.kind !== 'category') return t(`editor.findings.new.${pending.kind}`)
if (!doc || !pending.parent) return t('editor.findings.add_category')
const zone = elementLabel(
doc,
{ kind: 'zone', id: pending.parent.zoneId },
locale,
return (
<SelectField
id="finding-new-zone"
label={t('editor.findings.zone')}
value={zones.zoneId}
options={[
{ value: '', label: t('editor.field.choose') },
...zones.zones.map((zone) => ({
value: zone.zoneId,
label: zone.name[locale],
})),
]}
onCommit={zones.setZoneId}
/>
)
}
/** A category lands at zone level: its title names the zone chosen in the form. */
function useFormTitle(
pending: PendingFinding,
zone: FindingItem | null,
): string {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
if (pending.kind !== 'category')
return t(`editor.findings.new.${pending.kind}`)
if (!zone) return t('editor.findings.add_category')
return t('editor.findings.new.category_in_zone').replace(
'{zone}',
zone.name[locale],
)
return t('editor.findings.new.category_in_zone').replace('{zone}', zone)
}
const PREFIX: Record<FindingKind, IdPrefix> = {
@@ -146,7 +197,7 @@ function insertCommand(
if (pending.kind === 'category') {
return {
op: 'insert',
parent: { kind: 'zone', id: pending.parent?.zoneId ?? '' },
parent: pending.parent?.key,
element: {
kind: 'finding_category',
value: {