From e8c29c1fdfea9cc40bcd7759b93b20601d191b79 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Mon, 18 May 2026 23:32:01 +0200 Subject: [PATCH] fix(editor,pdf): accordion observation cards + seed 1 obs + native SVG rasterization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three issues from manual QA: 1. UX restructure — observations are now an accordion list. Each card has a header that's always visible (title + chips + chevron + action buttons) and a body that expands inline showing zones / pathologies / severity / comment. Click the header (or the chevron) to toggle. ObservationEditor has been merged into ObservationCard so each card owns its own details. Only one observation is expanded at a time; the chevron on the active card collapses everything. EditorRoute uses activeObservationIndex === -1 to represent "all collapsed". 2. App used to start with 2 observations instead of 1. Root cause: EditorRoute had a "seed first observation" useEffect that fired twice under React 19 StrictMode in dev, both invocations seeing length=0 and queueing addObservation(). Fixed by moving the seed into the store's initReport (synchronous, one-shot) and removing the useEffect entirely. Store tests updated to assert the seed behaviour (90/90 pass). 3. PDF export still failed with "The input is not a PNG file!" — canvg was returning bytes that pdf-lib rejected at embedPng(). Switched rasterizeSvg to the browser-native pipeline: serialize the SVG into a Blob URL, load it with an Image element, drawImage onto a 2D canvas, then toBlob → PNG bytes (with a toDataURL fallback). Validates the PNG signature before returning. Tree-shaking dropped canvg from the bundle (979 kB → 874 kB JS). Tests updated for the new initReport behaviour and the new svgToPng pipeline (Image mock + dataURL fallback case). lint + typecheck + build all clean. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/app/routes/EditorRoute.tsx | 94 ++++++---- .../observation/ObservationCard.tsx | 176 ++++++++++++++++-- .../observation/ObservationList.tsx | 42 ++++- src/pdf/render/svgToPng.ts | 130 +++++++++---- src/state/reportStore.ts | 11 +- tests/unit/pdf.svgToPng.test.ts | 83 ++++++++- tests/unit/state.reportStore.test.ts | 26 ++- 7 files changed, 449 insertions(+), 113 deletions(-) diff --git a/src/app/routes/EditorRoute.tsx b/src/app/routes/EditorRoute.tsx index 0359045d..032d172f 100644 --- a/src/app/routes/EditorRoute.tsx +++ b/src/app/routes/EditorRoute.tsx @@ -3,7 +3,6 @@ import { useNavigate } from 'react-router-dom' import AnatomyCanvas from '@/components/canvas/AnatomyCanvas' import DetailModal from '@/components/canvas/DetailModal' import ObservationList from '@/components/observation/ObservationList' -import ObservationEditor from '@/components/observation/ObservationEditor' import VertebraPicker from '@/components/observation/VertebraPicker' import ConclusionPanel from '@/components/report/ConclusionPanel' import ReportHeader from '@/components/report/ReportHeader' @@ -33,7 +32,11 @@ export default function EditorRoute() { const [bones, setBones] = useState>(new Map()) const [pathologies, setPathologies] = useState([]) - const [detail, setDetail] = useState<{ id: string; src: string; title: string } | null>(null) + const [detail, setDetail] = useState<{ + id: string + src: string + title: string + } | null>(null) const [vertebraTarget, setVertebraTarget] = useState(null) const [exporting, setExporting] = useState(false) @@ -52,12 +55,8 @@ export default function EditorRoute() { species.pathologies().then((m) => setPathologies(m.default)) }, [species]) - const observationsCount = report?.observations.length ?? -1 - useEffect(() => { - if (observationsCount === 0) addObservation() - }, [observationsCount, addObservation]) - - const activeObs = report?.observations[activeIndex] + const activeObs = + activeIndex >= 0 ? report?.observations[activeIndex] : undefined const boneStates = useMemo>(() => { const map = new Map() @@ -77,8 +76,12 @@ export default function EditorRoute() { return map }, [report, activeIndex]) + function onToggleCard(index: number) { + setActiveObservation(activeIndex === index ? -1 : index) + } + function onZoneClick(boneId: string, detailSourceId?: string) { - if (!report || !activeObs) return + if (!report) return const bone = bones.get(boneId) if (!bone) return @@ -91,6 +94,17 @@ export default function EditorRoute() { return } + if (!activeObs || activeIndex < 0) { + // No expanded observation — auto-expand the most recent one (or the + // first if no rows yet) so the click lands somewhere. + const fallbackIndex = + report.observations.length > 0 ? report.observations.length - 1 : -1 + if (fallbackIndex < 0) return + setActiveObservation(fallbackIndex) + // Defer the actual zone add to the next render where activeObs is set. + return + } + const newZone: ObservationZone = { boneId, ...(detailSourceId ? { detailSourceId } : {}), @@ -132,7 +146,7 @@ export default function EditorRoute() { } } - if (!report || !species || !activeObs) return null + if (!report || !species) return null return (
@@ -149,22 +163,23 @@ export default function EditorRoute() { - updateObservation(activeIndex, next)} - onOpenVertebraPicker={(zoneIndex) => setVertebraTarget(zoneIndex)} - onRemoveZone={(zoneIndex) => { - const zones = activeObs.zones.filter((_, i) => i !== zoneIndex) - updateObservation(activeIndex, { ...activeObs, zones }) + onChange={(i, next: Observation) => updateObservation(i, next)} + onOpenVertebraPicker={(_i, zoneIndex) => + setVertebraTarget(zoneIndex) + } + onRemoveZone={(i, zoneIndex) => { + const obs = report.observations[i] + if (!obs) return + const zones = obs.zones.filter((_, k) => k !== zoneIndex) + updateObservation(i, { ...obs, zones }) }} /> )} - {vertebraTarget !== null && activeObs.zones[vertebraTarget] && ( - setVertebraTarget(null)} - onConfirm={(vertebrae) => { - const fresh = useReportStore.getState().report?.observations[activeIndex] - if (!fresh?.zones[vertebraTarget]) { + {vertebraTarget !== null && + activeObs && + activeObs.zones[vertebraTarget] && ( + setVertebraTarget(null)} + onConfirm={(vertebrae) => { + 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, { ...fresh, zones: next }) setVertebraTarget(null) - return - } - const next = [...fresh.zones] - next[vertebraTarget] = { ...next[vertebraTarget], vertebrae } - updateObservation(activeIndex, { ...fresh, zones: next }) - setVertebraTarget(null) - }} - initial={activeObs.zones[vertebraTarget].vertebrae ?? []} - formula={species.vertebralFormula} - title={`Préciser les vertèbres — ${bones.get(activeObs.zones[vertebraTarget].boneId)?.fr ?? ''}`} - /> - )} + }} + initial={activeObs.zones[vertebraTarget].vertebrae ?? []} + formula={species.vertebralFormula} + title={`Préciser les vertèbres — ${bones.get(activeObs.zones[vertebraTarget].boneId)?.fr ?? ''}`} + /> + )}
) } diff --git a/src/components/observation/ObservationCard.tsx b/src/components/observation/ObservationCard.tsx index b024a4c0..16b7eed2 100644 --- a/src/components/observation/ObservationCard.tsx +++ b/src/components/observation/ObservationCard.tsx @@ -1,39 +1,72 @@ -import type { Observation } from '@/domain/schema/observation' +import type { Observation, ObservationZone, Vertebra } from '@/domain/schema/observation' +import type { Pathology } from '@/domain/schema/pathology' +import type { Bone } from '@/anatomy/types' +import { AnimatePresence, motion } from 'motion/react' import Card from '@/components/ui/Card' import Badge from '@/components/ui/Badge' import Button from '@/components/ui/Button' +import Label from '@/components/ui/Label' +import Textarea from '@/components/ui/Textarea' +import PathologyPicker from './PathologyPicker' +import FreeTextPathologyInput from './FreeTextPathologyInput' +import SeveritySelector from './SeveritySelector' export interface ObservationCardProps { observation: Observation index: number - active: boolean - onSelect: () => void + expanded: boolean + onToggle: () => void onDelete: () => void onMoveUp: () => void onMoveDown: () => void canMoveUp: boolean canMoveDown: boolean + // Editor body props (used only when expanded) + bones: Map + speciesId: string + pathologyPool: Pathology[] + onChange: (next: Observation) => void + onOpenVertebraPicker: (zoneIndex: number) => void + onRemoveZone: (zoneIndex: number) => void +} + +function describeZone(z: ObservationZone, bones: Map): string { + const b = bones.get(z.boneId) + const label = b?.fr ?? z.boneId + if (z.vertebrae?.length) { + const vs = z.vertebrae + .map((v: Vertebra) => `${v.region}${v.index}`) + .join(', ') + return `${label} (${vs})` + } + return label } export default function ObservationCard({ observation, index, - active, - onSelect, + expanded, + onToggle, onDelete, onMoveUp, onMoveDown, canMoveUp, canMoveDown, + bones, + speciesId, + pathologyPool, + onChange, + onOpenVertebraPicker, + onRemoveZone, }: ObservationCardProps) { const zoneCount = observation.zones.length const patCount = observation.pathologyIds.length + observation.freeTextPathologies.length - function onCardKey(event: React.KeyboardEvent) { + function onHeaderKey(event: React.KeyboardEvent) { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault() - onSelect() + onToggle() } } @@ -44,11 +77,13 @@ export default function ObservationCard({ } } + const zoneBoneIds = observation.zones.map((z) => z.boneId) + return (
@@ -83,14 +118,14 @@ export default function ObservationCard({ {zoneCount} zone{zoneCount > 1 ? 's' : ''} · {patCount} pathologie {patCount > 1 ? 's' : ''}
- {observation.comment && ( + {!expanded && observation.comment && (

{observation.comment}

)}
event.stopPropagation()} > +
+ + + {expanded && ( + +
+
+ + {observation.zones.length === 0 ? ( +

+ Cliquez sur les zones du schéma pour les ajouter à cette + observation. +

+ ) : ( +
    + {observation.zones.map((z, i) => { + const b = bones.get(z.boneId) + const isSpine = b?.category === 'spine-zone' + return ( +
  • + + {describeZone(z, bones)} + {isSpine && ( + + )} + + +
  • + ) + })} +
+ )} +
+ +
+ + + onChange({ ...observation, pathologyIds: ids }) + } + /> +
+ +
+ + + onChange({ ...observation, freeTextPathologies: items }) + } + /> +
+ +
+ + onChange({ ...observation, severity: s })} + /> +
+ +
+ +