feat(editor+pdf): per-zone PDF sections + collapsible findings pickers

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 <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude committed 2026-05-20 05:07:03 +02:00
1 parent 1e12f2252f
commit de40c5f91e
3 files changed
+145 -33

No files matched your search

+32 -1
View File
@@ -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<Map<string, strin
return labels
}
/**
* Loads the parsed findings catalog for a species so the PDF renderer
* can resolve a Finding's `zoneId` (JSON catalog key) back to its
* human-readable zone name. Returns null if the species lacks a
* catalog or the lazy import fails — the PDF still renders, but
* per-zone groups fall back to the raw zoneId.
*/
async function loadSpeciesFindings(
speciesId: string,
): Promise<SpeciesFindings | null> {
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<Uint8Array> {
const pdfDoc = await PDFDocument.create()
pdfDoc.setTitle(`Bilan ostéo — ${report.animal.name}`)
@@ -56,9 +78,18 @@ export async function exportReportToPdf(report: Report): Promise<Uint8Array> {
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)
+34 -11
View File
@@ -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<string, string>,
findingsTree: SpeciesFindings | null,
): Promise<void> {
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<string, Finding[]>()
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 })