From 337f8c3aea50bf81e19ee250f6d8ce889cdff4a3 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Mon, 18 May 2026 21:38:38 +0200 Subject: [PATCH] feat(editor): wire canvas + observation panel + conclusion + export --- src/app/routes/EditorRoute.tsx | 208 ++++++++++++++++++++++++++++++++- 1 file changed, 207 insertions(+), 1 deletion(-) diff --git a/src/app/routes/EditorRoute.tsx b/src/app/routes/EditorRoute.tsx index 0ddff822..5697c977 100644 --- a/src/app/routes/EditorRoute.tsx +++ b/src/app/routes/EditorRoute.tsx @@ -1,3 +1,209 @@ +import { useEffect, useMemo, useState } from 'react' +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' +import Button from '@/components/ui/Button' +import { useReportStore } from '@/state/reportStore' +import { getSpecies } from '@/domain/species/registry' +import { loadSvg } from '@/anatomy/svgLoader' +import { parseSvgBones } from '@/anatomy/svgConvention' +import type { Bone, BoneState } from '@/anatomy/types' +import type { Observation, ObservationZone } from '@/domain/schema/observation' +import type { Pathology } from '@/domain/schema/pathology' +import { exportReportToPdf } from '@/pdf/export' + export default function EditorRoute() { - return
EditorRoute placeholder
+ const navigate = useNavigate() + const report = useReportStore((s) => s.report) + const activeIndex = useReportStore((s) => s.activeObservationIndex) + const addObservation = useReportStore((s) => s.addObservation) + const updateObservation = useReportStore((s) => s.updateObservation) + const removeObservation = useReportStore((s) => s.removeObservation) + const moveObservationUp = useReportStore((s) => s.moveObservationUp) + const moveObservationDown = useReportStore((s) => s.moveObservationDown) + const setActiveObservation = useReportStore((s) => s.setActiveObservation) + const setConclusion = useReportStore((s) => s.setConclusion) + const setRecommendations = useReportStore((s) => s.setRecommendations) + const setNextAppointment = useReportStore((s) => s.setNextAppointment) + + const [bones, setBones] = useState>(new Map()) + const [pathologies, setPathologies] = useState([]) + const [detail, setDetail] = useState<{ id: string; src: string; title: string } | null>(null) + const [vertebraTarget, setVertebraTarget] = useState(null) + const [exporting, setExporting] = useState(false) + + useEffect(() => { + if (!report) navigate('/') + }, [report, navigate]) + + const species = useMemo( + () => (report ? getSpecies(report.animal.speciesId) : undefined), + [report], + ) + + useEffect(() => { + if (!species) return + loadSvg(species.mainSvg).then((root) => setBones(parseSvgBones(root))) + species.pathologies().then((m) => setPathologies(m.default)) + }, [species]) + + useEffect(() => { + if (report && report.observations.length === 0) addObservation() + }, [report?.observations.length, addObservation]) + + const activeObs = report?.observations[activeIndex] + + const boneStates = useMemo>(() => { + const map = new Map() + if (!report) return map + report.observations.forEach((obs, i) => { + obs.zones.forEach((z) => { + const prev = map.get(z.boneId) + if (i === activeIndex) { + map.set(z.boneId, prev === undefined ? 'current' : 'multi') + } else if (prev === undefined) { + map.set(z.boneId, 'linked') + } else { + map.set(z.boneId, 'multi') + } + }) + }) + return map + }, [report, activeIndex]) + + function onZoneClick(boneId: string, detailSourceId?: string) { + if (!report || !activeObs) return + const bone = bones.get(boneId) + if (!bone) return + + if (bone.opensDetail && species?.details[bone.opensDetail]) { + setDetail({ + id: bone.opensDetail, + src: species.details[bone.opensDetail], + title: bone.fr, + }) + return + } + + const newZone: ObservationZone = { + boneId, + ...(detailSourceId ? { detailSourceId } : {}), + } + const exists = activeObs.zones.some( + (z) => z.boneId === boneId && z.detailSourceId === detailSourceId, + ) + const nextZones = exists + ? activeObs.zones.filter( + (z) => !(z.boneId === boneId && z.detailSourceId === detailSourceId), + ) + : [...activeObs.zones, newZone] + updateObservation(activeIndex, { ...activeObs, zones: nextZones }) + + if (!exists && bone.category === 'spine-zone') { + setVertebraTarget(nextZones.length - 1) + } + } + + async function onExport() { + if (!report) return + setExporting(true) + try { + const bytes = await exportReportToPdf(report) + const blob = new Blob([bytes as BlobPart], { type: 'application/pdf' }) + const url = URL.createObjectURL(blob) + const a = document.createElement('a') + a.href = url + a.download = `bilan-${report.animal.name}-${report.date}.pdf` + a.click() + URL.revokeObjectURL(url) + } finally { + setExporting(false) + } + } + + if (!report || !species || !activeObs) return null + + return ( +
+ +
+
+ onZoneClick(id)} + /> +
+
+ + updateObservation(activeIndex, next)} + onOpenVertebraPicker={(zoneIndex) => setVertebraTarget(zoneIndex)} + onRemoveZone={(zoneIndex) => { + const zones = activeObs.zones.filter((_, i) => i !== zoneIndex) + updateObservation(activeIndex, { ...activeObs, zones }) + }} + /> + +
+ +
+
+
+ + {detail && ( + setDetail(null)} + detailSrc={detail.src} + detailId={detail.id} + title={`Détail — ${detail.title}`} + boneStates={boneStates} + onZoneClick={(id, did) => onZoneClick(id, did)} + /> + )} + + {vertebraTarget !== null && activeObs.zones[vertebraTarget] && ( + setVertebraTarget(null)} + onConfirm={(vertebrae) => { + const next = [...activeObs.zones] + next[vertebraTarget] = { ...next[vertebraTarget], vertebrae } + updateObservation(activeIndex, { ...activeObs, 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 ?? ''}`} + /> + )} +
+ ) }