feat(editor): no silent loss on a 1 024 px crossing

Drawing actions stay mounted and hidden while read-only: an upload in
flight, a cleaning candidate, a repair offer and a parent choice come
back on widening. Decision dialogs open only while editing, in a shown
place, references panel closed; otherwise the decision is held per
object, shown as a pending line, reopened by « Reprendre » (absent in
read-only). Picker, file input and parent button are disabled while one
is pending. Repair geometries move into RepairDialog, keyed on the offer.

R-24: no existing assertion rewritten; editor-no-loss-decisions.test.tsx
added (12 cases).
This commit is contained in:
bchanot committed 2026-11-06 21:23:46 +01:00
1 parent c57af6441f
commit 51fd3db608
5 files changed
+646 -65

No files matched your search

@@ -19,59 +19,77 @@ import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
export interface RepairDialogProps {
/** The refused attach to repair; null keeps the dialog closed. */
/** The refused attach to repair; null when there is none. Never gated by the caller. */
offer: RepairOffer | null
/** The decision may be shown now (editing, place shown); the typed values stay held. */
open?: boolean
onApplied: () => void
onCancel: () => void
}
/** The offer, the annotations it lists and the geometries typed so far. */
interface RepairDraft {
offer: RepairOffer
annotations: Annotation[]
geometries: Record<string, Geometry>
}
/**
* The geometries live here, keyed on the offer, so a dialog closed for a width change
* and shown again finds what was typed. The state resets only when a different, non-null
* offer arrives.
*/
function useRepairDraft(offer: RepairOffer | null) {
const doc = useSheetDocument()
const [draft, setDraft] = useState<RepairDraft | null>(null)
if (offer !== null && draft?.offer !== offer) {
const annotations = doc ? listedAnnotations(offer, doc) : []
const geometries = initialGeometries(offer, annotations)
setDraft({ offer, annotations, geometries })
}
const change = (id: string, geometry: Geometry) =>
setDraft((current) =>
current
? { ...current, geometries: { ...current.geometries, [id]: geometry } }
: current,
)
return { draft: draft?.offer === offer ? draft : null, change }
}
/**
* 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) {
export default function RepairDialog({
offer,
open = true,
...actions
}: RepairDialogProps) {
const t = useT()
const plural = usePlural()
const { draft, change } = useRepairDraft(offer)
const count = offer ? new Set(offer.refusal.annotationIds).size : 0
return (
<Dialog
open={offer !== null}
open={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} />}
{draft && <RepairForm draft={draft} onChange={change} {...actions} />}
</Dialog>
)
}
/** The listed annotations and their temporary geometries (proposals, then typed edits). */
function useRepairGeometries(offer: RepairOffer) {
const doc = useSheetDocument()
const [annotations] = useState(() =>
doc ? listedAnnotations(offer, doc) : [],
)
const [geometries, setGeometries] = useState(() =>
initialGeometries(offer, annotations),
)
const change = (id: string, geometry: Geometry) =>
setGeometries((current) => ({ ...current, [id]: geometry }))
return { annotations, geometries, change }
}
/** « Attacher et appliquer »: one batch, the refusal kept for the rows. */
function useRepairApply(
offer: RepairOffer,
geometries: Record<string, Geometry>,
onApplied: () => void,
) {
function useRepairApply(draft: RepairDraft, onApplied: () => void) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const [result, setResult] = useState<RepairResult | null>(null)
const apply = () => {
const applied = applyRepair(offer, geometries, { t, locale })
const applied = applyRepair(draft.offer, draft.geometries, { t, locale })
if (applied.ok) onApplied()
else setResult(applied)
}
@@ -93,33 +111,36 @@ function usePendingIds() {
return { pending: ids, report }
}
function useRepairState(offer: RepairOffer, onApplied: () => void) {
const geometry = useRepairGeometries(offer)
const applied = useRepairApply(offer, geometry.geometries, onApplied)
function useRepairState(draft: RepairDraft, onApplied: () => void) {
const applied = useRepairApply(draft, onApplied)
const pending = usePendingIds()
const blocked =
blockedIds(geometry.geometries).length > 0 || pending.pending.size > 0
return { ...geometry, ...applied, report: pending.report, blocked }
blockedIds(draft.geometries).length > 0 || pending.pending.size > 0
return { ...applied, report: pending.report, blocked }
}
function RepairForm({
offer,
draft,
onChange,
onApplied,
onCancel,
}: Omit<RepairDialogProps, 'offer'> & { offer: RepairOffer }) {
}: Pick<RepairDialogProps, 'onApplied' | 'onCancel'> & {
draft: RepairDraft
onChange: (id: string, geometry: Geometry) => void
}) {
const t = useT()
const state = useRepairState(offer, onApplied)
const state = useRepairState(draft, 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) => (
{draft.annotations.map((annotation) => (
<RepairRow
key={annotation.id}
annotation={annotation}
geometry={state.geometries[annotation.id]}
geometry={draft.geometries[annotation.id]}
messages={refused?.byAnnotation[annotation.id]}
onChange={(geometry) => state.change(annotation.id, geometry)}
onChange={(geometry) => onChange(annotation.id, geometry)}
onPending={state.report}
/>
))}
@@ -1,12 +1,18 @@
import { useRef, useState, type ChangeEvent } from 'react'
import { useId, useRef, useState, type ChangeEvent } from 'react'
import type { SheetView } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import KeptPlace from '@/components/editor/KeptPlace'
import Notice from '@/components/ui/Notice'
import {
drawingStatusOf,
drawingStatusText,
type DrawingStatus,
} from '@/editor/drawingStatus'
import {
useDecisionGate,
useDecisionsOpen,
useEverEditable,
} from '@/editor/editingContext'
import { unmeasuredViews } from '@/editor/issues'
import { useDrawingUpload, type DrawingUpload } from '@/editor/useDrawingUpload'
import { uploadErrorMessage } from '@/editor/uploadMessages'
@@ -33,6 +39,7 @@ export default function ViewDrawing({
}) {
const state = useEditorStore((s) => s.previewState)
const failures = useEditorStore((s) => s.drawingFailures)
const kept = useEverEditable(readOnly)
if (!state) return null
const status = drawingStatusOf(view, state, failures)
const unmeasured = unmeasuredViews(state).has(view.id)
@@ -44,8 +51,12 @@ export default function ViewDrawing({
unmeasured={unmeasured}
readOnly={readOnly}
/>
{!readOnly && (
<DrawingActions view={view} ownDrawing={status.kind !== 'none'} />
{kept && (
<DrawingActions
view={view}
ownDrawing={status.kind !== 'none'}
readOnly={readOnly}
/>
)}
</div>
)
@@ -84,52 +95,140 @@ function DrawingLines({
)
}
type Views = ReturnType<typeof useViewUpdate>
/** « Utiliser l'illustration de la vue parente »: one token per click is one decision. */
function useParentChoice(viewId: string, ownDrawing: boolean, views: Views) {
const [asking, setAsking] = useState<object | null>(null)
if (!ownDrawing && asking !== null) setAsking(null)
const cancel = () => setAsking(null)
const confirm = () => {
cancel()
views.update('drawing', viewId, { svg_asset_id: null }, true)
}
return { asking, ask: () => setAsking({}), confirm, cancel }
}
/** The three decisions of the section, each held back while it cannot be shown. */
function useDrawingDecisions(
upload: DrawingUpload,
parent: ReturnType<typeof useParentChoice>,
) {
const open = useDecisionsOpen()
const { ui } = upload
return {
cleaning: useDecisionGate(
ui.phase === 'cleaning' ? ui.candidate : null,
open,
),
repair: useDecisionGate(ui.phase === 'repair' ? ui.offer : null, open),
parent: useDecisionGate(parent.asking, open),
}
}
type Decisions = ReturnType<typeof useDrawingDecisions>
/** The id of the pending line, as `hold` while any decision is held back, else null. */
function useHold(decisions: Decisions) {
const pendingId = `${useId()}-pending`
const pending = Object.values(decisions).some((gate) => gate.withheld)
return { pendingId, hold: pending ? pendingId : null }
}
/**
* The drawing actions: mounted at the first editable render, then kept and hidden while
* the editor is read-only, so an upload in flight, a pending cleaning, a repair offer
* and a parent choice come back on widening. Their notices stay outside the hidden part.
*/
function DrawingActions({
view,
ownDrawing,
readOnly,
}: {
view: SheetView
ownDrawing: boolean
readOnly: boolean
}) {
const t = useT()
const upload = useDrawingUpload(view.id)
const views = useViewUpdate()
const [asking, setAsking] = useState(false)
const usesParent = () => {
setAsking(false)
views.update('drawing', view.id, { svg_asset_id: null }, true)
}
const parent = useParentChoice(view.id, ownDrawing, views)
const decisions = useDrawingDecisions(upload, parent)
const { pendingId, hold } = useHold(decisions)
return (
<>
<div className="flex flex-wrap items-center gap-2">
<FilePicker viewId={view.id} upload={upload} />
{view.kind === 'detail' && ownDrawing && (
<ActionButton
readOnly={false}
variant="ghost"
size="sm"
onClick={() => setAsking(true)}
>
{t('editor.views.drawing.use_parent')}
</ActionButton>
)}
</div>
<ParentDrawingDialog
open={asking}
onConfirm={usesParent}
onCancel={() => setAsking(false)}
/>
<KeptPlace visible={!readOnly} className="space-y-2">
<div className="flex flex-wrap items-center gap-2">
<FilePicker viewId={view.id} upload={upload} hold={hold} />
{view.kind === 'detail' && ownDrawing && (
<UseParentButton onClick={parent.ask} hold={hold} />
)}
</div>
<DecisionDialogs
upload={upload}
parent={parent}
decisions={decisions}
/>
</KeptPlace>
<UploadNotices upload={upload} />
{views.error('drawing') && (
<Notice tone="error">{views.error('drawing')}</Notice>
)}
<PendingNotices
id={pendingId}
decisions={decisions}
readOnly={readOnly}
/>
</>
)
}
function UseParentButton({
onClick,
hold,
}: {
onClick: () => void
hold: string | null
}) {
const t = useT()
return (
<ActionButton
readOnly={false}
variant="ghost"
size="sm"
disabled={hold !== null}
aria-describedby={hold ?? undefined}
onClick={onClick}
>
{t('editor.views.drawing.use_parent')}
</ActionButton>
)
}
function DecisionDialogs({
upload,
parent,
decisions,
}: {
upload: DrawingUpload
parent: ReturnType<typeof useParentChoice>
decisions: Decisions
}) {
const { ui } = upload
return (
<>
<ParentDrawingDialog
open={decisions.parent.shown !== null}
onConfirm={parent.confirm}
onCancel={parent.cancel}
/>
<CleaningDialog
candidate={upload.ui.phase === 'cleaning' ? upload.ui.candidate : null}
candidate={decisions.cleaning.shown}
onConfirm={() => void upload.confirmCleaning()}
onCancel={upload.cancelCleaning}
/>
<RepairDialog
offer={upload.ui.phase === 'repair' ? upload.ui.offer : null}
offer={ui.phase === 'repair' ? ui.offer : null}
open={decisions.repair.shown !== null}
onApplied={upload.finishRepair}
onCancel={upload.cancelRepair}
/>
@@ -137,12 +236,52 @@ function DrawingActions({
)
}
/** A decision held back shows as a pending line; « Reprendre » (editing only) reopens it. */
function PendingNotices({
id,
decisions,
readOnly,
}: {
id: string
decisions: Decisions
readOnly: boolean
}) {
const t = useT()
const open = useDecisionsOpen()
const kinds = (Object.keys(decisions) as (keyof Decisions)[]).filter(
(kind) => decisions[kind].withheld,
)
if (kinds.length === 0) return null
return (
<div id={id} className="space-y-2">
{kinds.map((kind) => (
<Notice key={kind} tone="info">
<p>{t(`editor.views.pending.${kind}`)}</p>
<ActionButton
readOnly={readOnly}
variant="secondary"
size="sm"
className="mt-2"
disabled={!open}
onClick={decisions[kind].resume}
>
{t('editor.views.pending.resume')}
</ActionButton>
</Notice>
))}
</div>
)
}
function FilePicker({
viewId,
upload,
hold,
}: {
viewId: string
upload: DrawingUpload
/** The id of the pending line while a decision is pending: no pick replaces it. */
hold: string | null
}) {
const t = useT()
const input = useRef<HTMLInputElement | null>(null)
@@ -161,13 +300,15 @@ function FilePicker({
tabIndex={-1}
aria-label={t('editor.views.drawing.file')}
data-testid={`view-${viewId}-file`}
disabled={hold !== null}
onChange={pick}
/>
<ActionButton
readOnly={false}
variant="secondary"
size="sm"
disabled={upload.ui.phase === 'working'}
disabled={upload.ui.phase === 'working' || hold !== null}
aria-describedby={hold ?? undefined}
onClick={() => input.current?.click()}
>
{t('editor.views.drawing.choose')}