From 7ea9619aa0178e795571bef0e1447e27e61950db Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 13:36:09 +0100 Subject: [PATCH] refactor(app): split the crop group hook, closed reason in drawing refusals --- .../components/editor/views/useCropGroup.ts | 74 +++++++++++++------ apps/app/src/editor/frameRefusal.ts | 13 +++- .../tests/unit/editor-drawing-upload.test.tsx | 20 +++++ 3 files changed, 82 insertions(+), 25 deletions(-) diff --git a/apps/app/src/components/editor/views/useCropGroup.ts b/apps/app/src/components/editor/views/useCropGroup.ts index 24fdaa82..38bd975c 100644 --- a/apps/app/src/components/editor/views/useCropGroup.ts +++ b/apps/app/src/components/editor/views/useCropGroup.ts @@ -23,13 +23,24 @@ export interface CropGroup { const differs = (a: BoxFields, b: BoxFields) => BOX_KEYS.some((key) => a[key] !== b[key]) +type Proposal = + | { kind: 'invalid'; reason: string } + | { kind: 'same' } + | { kind: 'change'; box: ViewBox | null } + +function proposalOf(draft: BoxFields, box: ViewBox | null): Proposal { + const parsed = parseBox(draft) + if (!parsed.ok) return { kind: 'invalid', reason: parsed.reason } + return sameBox(parsed.box, box) + ? { kind: 'same' } + : { kind: 'change', box: parsed.box } +} + /** - * The four `view_box` fields as one group: `apply` proposes all four together under the - * geometry check; a refused group stays typed and `refused`. A new saved box (undo, reload, - * restore, an accepted apply) replaces what was typed; unmounting applies a pending group. + * The typed fields and the refused flag. A new saved box (undo, reload, restore, an + * accepted apply) replaces what was typed. */ -export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup { - const views = useViewUpdate() +function useCropDraft(box: ViewBox | null) { const saved = fieldsOf(box) const [draft, setDraft] = useState(saved) const [seen, setSeen] = useState(saved) @@ -39,35 +50,50 @@ export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup { setDraft(saved) setRefused(false) } - const apply = (): void => { - const parsed = parseBox(draft) - if (!parsed.ok) { - views.reject('crop', `editor.views.crop.${parsed.reason}`) - setRefused(true) - } else if (sameBox(parsed.box, box)) { - views.clear() - setRefused(false) - } else { - const done = views.update('crop', viewId, { view_box: parsed.box }, true) - setRefused(!done) - } - } - const latest = useRef({ draft, saved, apply }) + return { saved, draft, setDraft, refused, setRefused } +} + +/** Runs the latest `flush` once, when the component unmounts with `pending` true. */ +function useFlushOnUnmount(pending: boolean, flush: () => void): void { + const latest = useRef({ pending, flush }) useEffect(() => { - latest.current = { draft, saved, apply } + latest.current = { pending, flush } }) useEffect( () => () => { - if (differs(latest.current.draft, latest.current.saved)) { - latest.current.apply() - } + if (latest.current.pending) latest.current.flush() }, [], ) +} + +/** + * The four `view_box` fields as one group: `apply` proposes all four together under the + * geometry check; a refused group stays typed and `refused`. Unmounting applies a pending + * group. + */ +export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup { + const views = useViewUpdate() + const form = useCropDraft(box) + const { saved, draft, setDraft, setRefused } = form + const apply = (): void => { + const proposal = proposalOf(draft, box) + if (proposal.kind === 'invalid') { + views.reject('crop', `editor.views.crop.${proposal.reason}`) + setRefused(true) + } else if (proposal.kind === 'same') { + views.clear() + setRefused(false) + } else { + const patch = { view_box: proposal.box } + setRefused(!views.update('crop', viewId, patch, true)) + } + } + useFlushOnUnmount(differs(draft, saved), apply) const shown = parseBox(draft) return { draft, - refused, + refused: form.refused, views, rectangle: shown.ok ? shown.box : box, setField: (key, value) => setDraft({ ...draft, [key]: value }), diff --git a/apps/app/src/editor/frameRefusal.ts b/apps/app/src/editor/frameRefusal.ts index a25bf19c..11817e8c 100644 --- a/apps/app/src/editor/frameRefusal.ts +++ b/apps/app/src/editor/frameRefusal.ts @@ -1,10 +1,21 @@ import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types' import type { Locale } from '@/i18n/types' +import { rejectionReason } from './drawingReason' import { elementLabel } from './sheetDocument' import type { GeometryRefusal } from './issues' type Translate = (key: string) => string +function drawingRejectedMessage(t: Translate, detail: string | null): string { + const reason = rejectionReason(detail) + if (reason === 'not_canonical') + return t('editor.upload.err.svg_not_canonical') + return t('editor.upload.err.svg_rejected').replace( + '{reason}', + t(`editor.upload.reason.${reason}`), + ) +} + /** * The words for a frame change the geometry check refused: the annotations (or views) * the change would break, by name. Nothing here re-judges the geometry. @@ -20,7 +31,7 @@ export function frameRefusalMessage( .map((id) => (doc ? elementLabel(doc, { kind, id }, locale) : id)) .join(', ') if (refusal.reason === 'drawing_rejected') { - return t('editor.upload.err.svg_not_canonical') + return drawingRejectedMessage(t, refusal.drawingDetail ?? null) } if (refusal.reason === 'view_unmeasured') { return t('editor.views.frame.view_unmeasured').replace( diff --git a/apps/app/tests/unit/editor-drawing-upload.test.tsx b/apps/app/tests/unit/editor-drawing-upload.test.tsx index 8078c3fb..ac5afd1a 100644 --- a/apps/app/tests/unit/editor-drawing-upload.test.tsx +++ b/apps/app/tests/unit/editor-drawing-upload.test.tsx @@ -14,6 +14,7 @@ import ViewsTab from '@/components/editor/tabs/ViewsTab' import { cleanSvg } from '@/editor/core/cleaning' import { createHash } from 'node:crypto' import { getDict } from '@/i18n' +import { frameRefusalMessage } from '@/editor/frameRefusal' import { uploadErrorMessage } from '@/editor/uploadMessages' import { MAX_DRAWING_BYTES, attachToView } from '@/editor/drawingUpload' import { useLocaleStore } from '@/state/localeStore' @@ -296,3 +297,22 @@ describe('drawing upload flow', () => { ) }) }) + +describe('frame refusal for a rejected drawing', () => { + it('phrases not_canonical and a reader reason as closed messages', () => { + const t = (key: string) => getDict('fr')[key as never] as string + const refusal = (drawingDetail: string) => ({ + reason: 'drawing_rejected' as const, + annotationIds: [], + issues: [], + viewIds: [], + drawingDetail, + }) + expect( + frameRefusalMessage(t, refusal('not_canonical'), null, 'fr'), + ).toMatch(/pas sous la forme enregistrée/) + expect(frameRefusalMessage(t, refusal('doctype'), null, 'fr')).toMatch( + /DOCTYPE/, + ) + }) +})