From de40c5f91e1b9bfbb222e21e966f843dda7451e9 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Wed, 20 May 2026 05:07:03 +0200 Subject: [PATCH] feat(editor+pdf): per-zone PDF sections + collapsible findings pickers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PDF --- The flat single-line "CONSTATS" section bundled every selected leaf from every zone plus the free-text additions on one line, which got hard to read as soon as an observation touched more than one zone. The export now groups catalog findings by their JSON `zoneId` and emits one section per zone, labelled with the zone's French name ("CONSTATS — SPHÉNOÏDE", "CONSTATS — ARTICULATION TEMPORO-MANDIBULAIRE (ATM)", …). Free-text additions move to a dedicated "CONSTATS LIBRES" section directly below. The catalog tree is loaded once at the top of exportReportToPdf and threaded through to addObservationPage, so the per-zone label lookup costs nothing extra per observation. Editor ------ Inside an expanded observation, each available zone's FindingsTreePicker is now wrapped in an accordion header that mirrors the ObservationCard expand/collapse semantics: - Click a zone header → that picker opens and any other picker on the card closes. - Click an open zone header → it collapses. - Each header shows a selected-count badge and a ▼/▲ caret. Default state is "all closed" so the card stays compact when the user expands a new observation. Motion-based height/opacity transition matches the observation card body animation for visual consistency. Co-Authored-By: Claude --- .../observation/ObservationCard.tsx | 100 ++++++++++++++---- src/pdf/export.ts | 33 +++++- src/pdf/render/observationPage.ts | 45 ++++++-- 3 files changed, 145 insertions(+), 33 deletions(-) diff --git a/src/components/observation/ObservationCard.tsx b/src/components/observation/ObservationCard.tsx index 8df02387..cb3a9d62 100644 --- a/src/components/observation/ObservationCard.tsx +++ b/src/components/observation/ObservationCard.tsx @@ -1,4 +1,4 @@ -import { useMemo } from 'react' +import { useMemo, useState } from 'react' import type { Observation, ObservationZone, @@ -66,6 +66,11 @@ export default function ObservationCard({ onOpenVertebraPicker, onRemoveZone, }: ObservationCardProps) { + // Per-zone picker accordion — only one tree picker open at a time, + // matching the observation-card accordion semantics. Closed by default + // so the card stays compact when first expanded. + const [expandedZoneId, setExpandedZoneId] = useState(null) + const zoneCount = observation.zones.length const findingCount = observation.findings.length + observation.freeTextFindings.length @@ -262,26 +267,79 @@ export default function ObservationCard({ : 'Aucun référentiel disponible pour ces zones. Utilisez le texte libre ci-dessous.'}

) : ( -
- {zonesWithTree.map(({ tree }) => ( -
-
- {tree.name_fr} -
- - onChange({ ...observation, findings: next }) - } - /> -
- ))} -
+
    + {zonesWithTree.map(({ tree }) => { + const expanded = expandedZoneId === tree.id + const selectedCount = observation.findings.filter( + (f) => f.zoneId === tree.id, + ).length + return ( +
  • + + + {expanded && ( + +
    + + onChange({ ...observation, findings: next }) + } + /> +
    +
    + )} +
    +
  • + ) + })} +
)} diff --git a/src/pdf/export.ts b/src/pdf/export.ts index f281432d..f8effe73 100644 --- a/src/pdf/export.ts +++ b/src/pdf/export.ts @@ -1,5 +1,6 @@ import { PDFDocument } from 'pdf-lib' import type { Report } from '@/domain/schema/report' +import type { SpeciesFindings } from '@/domain/schema/findings' import { embedFonts } from './fonts' import { addObservationPage } from './render/observationPage' import { addConclusionPage } from './render/conclusionPage' @@ -46,6 +47,27 @@ export async function buildZoneLabels(report: Report): Promise { + const species = getSpecies(speciesId) + if (!species) return null + try { + const m = await species.findings() + return m.default + } catch (err) { + console.warn(`loadSpeciesFindings: failed for ${speciesId}`, err) + return null + } +} + export async function exportReportToPdf(report: Report): Promise { const pdfDoc = await PDFDocument.create() pdfDoc.setTitle(`Bilan ostéo — ${report.animal.name}`) @@ -56,9 +78,18 @@ export async function exportReportToPdf(report: Report): Promise { const fonts = await embedFonts(pdfDoc) const zoneLabels = await buildZoneLabels(report) + const findingsTree = await loadSpeciesFindings(report.animal.speciesId) for (let i = 0; i < report.observations.length; i += 1) { - await addObservationPage(pdfDoc, report, report.observations[i], i, fonts, zoneLabels) + await addObservationPage( + pdfDoc, + report, + report.observations[i], + i, + fonts, + zoneLabels, + findingsTree, + ) } await addConclusionPage(pdfDoc, report, fonts) diff --git a/src/pdf/render/observationPage.ts b/src/pdf/render/observationPage.ts index a03e1985..a4865656 100644 --- a/src/pdf/render/observationPage.ts +++ b/src/pdf/render/observationPage.ts @@ -1,7 +1,8 @@ import { rgb, PageSizes, type PDFDocument, type PDFFont } from 'pdf-lib' import type { EmbeddedFonts } from '@/pdf/fonts' import type { Report } from '@/domain/schema/report' -import type { Observation } from '@/domain/schema/observation' +import type { Finding, Observation } from '@/domain/schema/observation' +import type { SpeciesFindings } from '@/domain/schema/findings' import { drawHeader } from './header' import { rasterizeSvg } from './svgToPng' import { loadSvg } from '@/anatomy/svgLoader' @@ -94,6 +95,7 @@ export async function addObservationPage( obsIndex: number, fonts: EmbeddedFonts, zoneLabels: Map, + findingsTree: SpeciesFindings | null, ): Promise { const page = pdfDoc.addPage(PageSizes.A4) const { width, height } = page.getSize() @@ -195,16 +197,37 @@ export async function addObservationPage( const sections: Array<{ label: string; value: string }> = [ { label: 'ZONES', value: zonesText || '—' }, ] - const findingLabels = [ - ...observation.findings.map((f) => - f.requiresVetReferral - ? `⚠ ${f.labelFrSnapshot}` - : f.labelFrSnapshot, - ), - ...observation.freeTextFindings, - ] - if (findingLabels.length) { - sections.push({ label: 'CONSTATS', value: findingLabels.join(' · ') }) + // Group catalog findings by their JSON zoneId so each zone gets its + // own labelled section. Preserves first-seen order so the on-screen + // pick order survives the export. + const findingsByZone = new Map() + for (const f of observation.findings) { + const list = findingsByZone.get(f.zoneId) + if (list) { + list.push(f) + } else { + findingsByZone.set(f.zoneId, [f]) + } + } + + for (const [zoneId, group] of findingsByZone) { + const zoneName = findingsTree?.zones[zoneId]?.name_fr ?? zoneId + const value = group + .map((f) => + f.requiresVetReferral ? `⚠ ${f.labelFrSnapshot}` : f.labelFrSnapshot, + ) + .join(' · ') + sections.push({ + label: `CONSTATS — ${zoneName.toUpperCase()}`, + value, + }) + } + + if (observation.freeTextFindings.length) { + sections.push({ + label: 'CONSTATS LIBRES', + value: observation.freeTextFindings.join(' · '), + }) } if (observation.severity) { sections.push({ label: 'SÉVÉRITÉ', value: observation.severity })