feat(editor): repair out-of-frame annotations when attaching a drawing

This commit is contained in:
bchanot committed 2026-11-04 20:10:07 +01:00
1 parent 4ab85adeb0
commit 3776a33b79
22 files changed
+1277 -19

No files matched your search

@@ -114,9 +114,8 @@ function useFormTitle(pending: PendingFinding): string {
const t = useT()
const doc = useSheetDocument()
const locale = useLocaleStore((s) => s.locale)
if (pending.kind !== 'category' || !doc || !pending.parent) {
return t(`editor.findings.new.${pending.kind}`)
}
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 },
@@ -0,0 +1,196 @@
import { useState } from 'react'
import type { Annotation, Geometry } from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import GeometryGroup from '@/components/editor/annotations/GeometryGroup'
import { annotationName } from '@/components/editor/annotations/AnnotationList'
import { isEditable } from '@/editor/annotations/geometryEdit'
import { usePlural } from '@/editor/plural'
import {
applyRepair,
blockedIds,
initialGeometries,
listedAnnotations,
polygonReplacement,
type RepairOffer,
type RepairResult,
} from '@/editor/repair'
import { useSheetDocument } from '@/editor/sheetDocument'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
export interface RepairDialogProps {
/** The refused attach to repair; null keeps the dialog closed. */
offer: RepairOffer | null
onApplied: () => void
onCancel: () => void
}
/**
* The temporary form of an attach refused for annotations outside the new frame (owner
* decision 7). Values are proposals in the new frame; nothing reaches the document before
* « Attacher et appliquer », which sends ONE batch the core judges on its end state.
*/
export default function RepairDialog({ offer, ...actions }: RepairDialogProps) {
const t = useT()
const plural = usePlural()
const count = offer ? new Set(offer.refusal.annotationIds).size : 0
return (
<Dialog
open={offer !== null}
onClose={actions.onCancel}
title={t('editor.views.repair.title')}
description={offer && plural('editor.views.repair.body', count)}
size="lg"
>
{offer && <RepairForm offer={offer} {...actions} />}
</Dialog>
)
}
/** The temporary state of the form: proposals, typed edits and the last refusal. */
function useRepairState(offer: RepairOffer, onApplied: () => void) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const doc = useSheetDocument()
const [annotations] = useState(() =>
doc ? listedAnnotations(offer, doc) : [],
)
const [geometries, setGeometries] = useState(() =>
initialGeometries(offer, annotations),
)
const [result, setResult] = useState<RepairResult | null>(null)
const apply = () => {
const applied = applyRepair(offer, geometries, { t, locale })
if (applied.ok) onApplied()
else setResult(applied)
}
const change = (id: string, geometry: Geometry) =>
setGeometries((current) => ({ ...current, [id]: geometry }))
return { annotations, geometries, result, apply, change }
}
function RepairForm({
offer,
onApplied,
onCancel,
}: Omit<RepairDialogProps, 'offer'> & { offer: RepairOffer }) {
const t = useT()
const state = useRepairState(offer, onApplied)
const refused = state.result && !state.result.ok ? state.result : null
return (
<div className="mt-4 space-y-4">
<ul className="space-y-4">
{state.annotations.map((annotation) => (
<RepairRow
key={annotation.id}
annotation={annotation}
geometry={state.geometries[annotation.id]}
offer={offer}
messages={refused?.byAnnotation[annotation.id]}
onChange={(geometry) => state.change(annotation.id, geometry)}
/>
))}
</ul>
{refused && <Refusal general={refused.general} />}
<div className="flex flex-wrap justify-end gap-2">
<Button type="button" variant="ghost" onClick={onCancel}>
{t('editor.views.repair.cancel')}
</Button>
<Button
type="button"
disabled={blockedIds(state.geometries).length > 0}
onClick={state.apply}
>
{t('editor.views.repair.apply')}
</Button>
</div>
</div>
)
}
function Refusal({ general }: { general: string | null }) {
const t = useT()
return (
<div
role="alert"
className="space-y-1 text-sm text-[var(--color-danger-strong)]"
>
<p className="font-medium">{t('editor.views.repair.refused')}</p>
{general && <p>{general}</p>}
</div>
)
}
interface RowProps {
annotation: Annotation
geometry: Geometry | undefined
offer: RepairOffer
messages: string[] | undefined
onChange: (geometry: Geometry) => void
}
function RepairRow({
annotation,
geometry,
offer,
messages,
onChange,
}: RowProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const doc = useSheetDocument()
const name = annotationName(annotation, doc?.views ?? [], locale)
const title = t('editor.views.repair.row')
.replace('{kind}', t(`editor.annotations.kind.${annotation.kind}`))
.replace('{name}', name)
return (
<li
data-testid={`repair-${annotation.id}`}
className="space-y-2 rounded-lg border border-[var(--color-border)] p-3"
>
<h4 className="text-sm font-semibold">{title}</h4>
{geometry && isEditable(geometry) ? (
<GeometryGroup
idPrefix={`repair-${annotation.id}`}
geometry={geometry}
locked={false}
onApply={(next) => {
onChange(next)
return { ok: true }
}}
/>
) : (
<PathBlock
onConvert={() =>
onChange(polygonReplacement(offer.frames[annotation.view_id]))
}
/>
)}
{messages && messages.length > 0 && (
<ul
role="alert"
className="list-disc pl-5 text-sm text-[var(--color-danger-strong)]"
>
{messages.map((message) => (
<li key={message}>{message}</li>
))}
</ul>
)}
</li>
)
}
function PathBlock({ onConvert }: { onConvert: () => void }) {
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>
)
}
@@ -17,6 +17,7 @@ import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import CleaningDialog from './CleaningDialog'
import ParentDrawingDialog from './ParentDrawingDialog'
import RepairDialog from './RepairDialog'
/**
* A view's drawing: its state, the hint when it is not usable, « Choisir une
@@ -127,6 +128,11 @@ function DrawingActions({
onConfirm={() => void upload.confirmCleaning()}
onCancel={upload.cancelCleaning}
/>
<RepairDialog
offer={upload.ui.phase === 'repair' ? upload.ui.offer : null}
onApplied={upload.finishRepair}
onCancel={upload.cancelRepair}
/>
</>
)
}