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

@@ -3,13 +3,18 @@ import type { PendingFinding } from '@/components/editor/findings/FindingCreateF
import { canAddNode, type FindingItem } from '@/editor/findings/tree'
import { useT } from '@/i18n/useT'
/** « Ajouter une zone / une catégorie / un constat »: each starts a creation form. */
/**
* « Ajouter une zone / une catégorie / un constat »: each starts a creation form. Only
* the node depends on the selection; a category names its zone in its own form (C46).
*/
export default function AddBar({
selected,
hasZone,
readOnly,
onStart,
}: {
selected: FindingItem | undefined
hasZone: boolean
readOnly: boolean
onStart: (pending: PendingFinding) => void
}) {
@@ -28,8 +33,8 @@ export default function AddBar({
readOnly={readOnly}
variant="secondary"
size="sm"
disabled={!selected}
onClick={() => onStart({ kind: 'category', parent: selected ?? null })}
disabled={!hasZone}
onClick={() => onStart({ kind: 'category', parent: null })}
>
{t('editor.findings.add_category')}
</ActionButton>
@@ -42,6 +47,11 @@ export default function AddBar({
>
{t('editor.findings.add_node')}
</ActionButton>
{!hasZone && !readOnly && (
<p className="basis-full text-sm text-[var(--color-ink-muted)]">
{t('editor.findings.add_category_no_zone')}
</p>
)}
</div>
)
}
@@ -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: {
@@ -72,10 +72,10 @@ export default function FindingsTab({ readOnly, focus }: EditorTabProps) {
setSelectedId(found.item.id)
setFocusId({ id: found.item.id })
})
const created = (id: string) => {
const created = (id: string, parent: FindingItem | null) => {
if (!pending) return
if (pending.parent) toggle(pending.parent.id, true)
setSelectedId(createdItemId(pending.kind, pending.parent, id))
if (parent) toggle(parent.id, true)
setSelectedId(createdItemId(pending.kind, parent, id))
setPending(null)
}
const askDelete = (item: FindingItem) => {
@@ -96,7 +96,12 @@ export default function FindingsTab({ readOnly, focus }: EditorTabProps) {
aria-label={t('editor.tab.findings')}
className="min-h-24 space-y-3"
>
<AddBar selected={selected} readOnly={readOnly} onStart={setPending} />
<AddBar
selected={selected}
hasZone={items.some((item) => item.kind === 'zone')}
readOnly={readOnly}
onStart={setPending}
/>
{pending && !readOnly && (
<FindingCreateForm
key={`${pending.kind}-${pending.parent?.id ?? ''}`}
@@ -11,7 +11,6 @@ import {
blockedIds,
initialGeometries,
listedAnnotations,
polygonReplacement,
type RepairOffer,
type RepairResult,
} from '@/editor/repair'
@@ -119,7 +118,6 @@ function RepairForm({
key={annotation.id}
annotation={annotation}
geometry={state.geometries[annotation.id]}
offer={offer}
messages={refused?.byAnnotation[annotation.id]}
onChange={(geometry) => state.change(annotation.id, geometry)}
onPending={state.report}
@@ -155,7 +153,6 @@ function Refusal({ general }: { general: string | null }) {
interface RowProps {
annotation: Annotation
geometry: Geometry | undefined
offer: RepairOffer
messages: string[] | undefined
onChange: (geometry: Geometry) => void
onPending: (id: string, pending: boolean) => void
@@ -164,7 +161,6 @@ interface RowProps {
function RepairRow({
annotation,
geometry,
offer,
messages,
onChange,
onPending,
@@ -194,11 +190,7 @@ function RepairRow({
}}
/>
) : (
<PathBlock
onConvert={() =>
onChange(polygonReplacement(offer.frames[annotation.view_id]))
}
/>
<PathBlock />
)}
{messages && messages.length > 0 && (
<ul
@@ -214,16 +206,11 @@ function RepairRow({
)
}
function PathBlock({ onConvert }: { onConvert: () => void }) {
function PathBlock() {
const t = useT()
return (
<div className="space-y-2">
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.repair.path_blocked')}
</p>
<Button type="button" variant="secondary" size="sm" onClick={onConvert}>
{t('editor.annotations.replace_polygon')}
</Button>
</div>
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.repair.path_blocked')}
</p>
)
}