feat(editor): wire canvas + observation panel + conclusion + export
This commit is contained in:
1 parent
2b2a7db63c
commit
337f8c3aea
1 file changed
+207
-1
@@ -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() {
|
export default function EditorRoute() {
|
||||||
return <div>EditorRoute placeholder</div>
|
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<Map<string, Bone>>(new Map())
|
||||||
|
const [pathologies, setPathologies] = useState<Pathology[]>([])
|
||||||
|
const [detail, setDetail] = useState<{ id: string; src: string; title: string } | null>(null)
|
||||||
|
const [vertebraTarget, setVertebraTarget] = useState<number | null>(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<Map<string, BoneState>>(() => {
|
||||||
|
const map = new Map<string, BoneState>()
|
||||||
|
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 (
|
||||||
|
<main className="min-h-screen flex flex-col">
|
||||||
|
<ReportHeader report={report} />
|
||||||
|
<div className="flex-1 grid grid-cols-1 lg:grid-cols-[2fr_3fr] gap-6 p-6">
|
||||||
|
<section className="border border-[var(--color-border)] rounded-2xl bg-[var(--color-surface)] overflow-hidden">
|
||||||
|
<AnatomyCanvas
|
||||||
|
src={species.mainSvg}
|
||||||
|
boneStates={boneStates}
|
||||||
|
onZoneClick={(id) => onZoneClick(id)}
|
||||||
|
/>
|
||||||
|
</section>
|
||||||
|
<section className="space-y-6">
|
||||||
|
<ObservationList
|
||||||
|
observations={report.observations}
|
||||||
|
activeIndex={activeIndex}
|
||||||
|
onSelect={setActiveObservation}
|
||||||
|
onAdd={addObservation}
|
||||||
|
onDelete={removeObservation}
|
||||||
|
onMoveUp={moveObservationUp}
|
||||||
|
onMoveDown={moveObservationDown}
|
||||||
|
/>
|
||||||
|
<ObservationEditor
|
||||||
|
observation={activeObs}
|
||||||
|
bones={bones}
|
||||||
|
speciesId={species.id}
|
||||||
|
pathologyPool={pathologies}
|
||||||
|
onChange={(next: Observation) => updateObservation(activeIndex, next)}
|
||||||
|
onOpenVertebraPicker={(zoneIndex) => setVertebraTarget(zoneIndex)}
|
||||||
|
onRemoveZone={(zoneIndex) => {
|
||||||
|
const zones = activeObs.zones.filter((_, i) => i !== zoneIndex)
|
||||||
|
updateObservation(activeIndex, { ...activeObs, zones })
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<ConclusionPanel
|
||||||
|
conclusion={report.conclusion}
|
||||||
|
recommendations={report.recommendations}
|
||||||
|
nextAppointment={report.nextAppointment}
|
||||||
|
onChangeConclusion={setConclusion}
|
||||||
|
onChangeRecommendations={setRecommendations}
|
||||||
|
onChangeNextAppointment={setNextAppointment}
|
||||||
|
/>
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button onClick={onExport} disabled={exporting}>
|
||||||
|
{exporting ? 'Génération…' : 'Exporter en PDF'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{detail && (
|
||||||
|
<DetailModal
|
||||||
|
open
|
||||||
|
onClose={() => 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] && (
|
||||||
|
<VertebraPicker
|
||||||
|
open
|
||||||
|
onClose={() => 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 ?? ''}`}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user