From 188e79e76d47075bf114e3988ea5f4787348420b Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Mon, 18 May 2026 22:16:12 +0200 Subject: [PATCH] =?UTF-8?q?fix(review):=20address=20code-review=20yellows?= =?UTF-8?q?=20=E2=80=94=20onExport=20catch,=20vertebra=20picker=20fresh-re?= =?UTF-8?q?ad,=20draft=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three impactful fixes from the post-impl code review: 1. EditorRoute.onExport now catches PDF generation failures, logs to console, and alerts the praticien in French — previously errors were swallowed by try/finally with only `setExporting(false)`. 2. VertebraPicker onConfirm reads the active observation from the store at confirm time instead of closing over the render-scope `activeObs`. Prevents mutating a stale observation if the praticien switches active observation while the picker is open. 3. readPersistedDraft validates the parsed JSON with ReportSchema instead of a raw `as Report` cast. Corrupted drafts now return null cleanly rather than poisoning the store. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/app/routes/EditorRoute.tsx | 14 ++++++++++++-- src/state/reportStore.ts | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/app/routes/EditorRoute.tsx b/src/app/routes/EditorRoute.tsx index 885ac3fe..0359045d 100644 --- a/src/app/routes/EditorRoute.tsx +++ b/src/app/routes/EditorRoute.tsx @@ -122,6 +122,11 @@ export default function EditorRoute() { a.download = `bilan-${report.animal.name}-${report.date}.pdf` a.click() URL.revokeObjectURL(url) + } catch (err) { + console.error('PDF export failed', err) + window.alert( + "Échec de la génération du PDF. Réessayez ou contactez le support.", + ) } finally { setExporting(false) } @@ -195,9 +200,14 @@ export default function EditorRoute() { open onClose={() => setVertebraTarget(null)} onConfirm={(vertebrae) => { - const next = [...activeObs.zones] + const fresh = useReportStore.getState().report?.observations[activeIndex] + if (!fresh?.zones[vertebraTarget]) { + setVertebraTarget(null) + return + } + const next = [...fresh.zones] next[vertebraTarget] = { ...next[vertebraTarget], vertebrae } - updateObservation(activeIndex, { ...activeObs, zones: next }) + updateObservation(activeIndex, { ...fresh, zones: next }) setVertebraTarget(null) }} initial={activeObs.zones[vertebraTarget].vertebrae ?? []} diff --git a/src/state/reportStore.ts b/src/state/reportStore.ts index 17ac081b..c2a7371c 100644 --- a/src/state/reportStore.ts +++ b/src/state/reportStore.ts @@ -1,5 +1,5 @@ import { create } from 'zustand' -import type { Report } from '@/domain/schema/report' +import { ReportSchema, type Report } from '@/domain/schema/report' import type { Observation } from '@/domain/schema/observation' import { generateId } from '@/lib/id' import { nowIso } from '@/lib/dates' @@ -133,7 +133,8 @@ export function readPersistedDraft(): Report | null { const raw = sessionStorage.getItem(DRAFT_KEY) if (!raw) return null try { - return JSON.parse(raw) as Report + const parsed = ReportSchema.safeParse(JSON.parse(raw)) + return parsed.success ? parsed.data : null } catch { return null }