chore(editor): split onZoneClick into per-behavior handlers

This commit is contained in:
Bastien Chanot committed 2026-07-12 17:05:32 +02:00
1 parent 0ec1b27d1f
commit fe307e1d8d
1 file changed
+104 -65
+104 -65
View File
@@ -21,6 +21,7 @@ import { parseSvgBones } from '@/anatomy/svgConvention'
import { collectSpineVertebrae, syncSpineZones } from '@/lib/spineZones'
import type { Bone, BoneState } from '@/anatomy/types'
import type { Observation, ObservationZone } from '@/domain/schema/observation'
import type { Report } from '@/domain/schema/report'
import type { SpeciesFindings } from '@/domain/schema/findings'
import type { Practitioner } from '@/domain/schema/practitioner'
import type { Animal, Owner } from '@/domain/schema/animal'
@@ -197,78 +198,84 @@ export default function EditorRoute() {
setActiveObservation(activeIndex === index ? -1 : index)
}
function onZoneClick(boneId: string, detailSourceId?: string) {
if (!report) return
const bone = bones.get(boneId) ?? detailBones.get(boneId)
if (!bone) return
/** Opens the detail-popup modal for a bone that carries data-anno-opens-detail. */
function handleDetailOpen(bone: Bone, detailId: string, detailSrc: string) {
setDetail({
id: detailId,
src: detailSrc,
title: boneLabel(bone, locale, detailId),
})
}
if (bone.opensDetail && species?.details[bone.opensDetail]) {
setDetail({
id: bone.opensDetail,
src: species.details[bone.opensDetail],
title: boneLabel(bone, locale, bone.opensDetail),
})
return
}
/**
* No expanded observation — auto-expand the most recent one (or the
* first if no rows yet) so the click lands somewhere. Defers the actual
* zone add to the next render where activeObs is set.
*/
function activateFallbackObservation(report: Report) {
const fallbackIndex =
report.observations.length > 0 ? report.observations.length - 1 : -1
if (fallbackIndex < 0) return
setActiveObservation(fallbackIndex)
}
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 matches = (z: ObservationZone) =>
z.boneId === boneId && z.detailSourceId === detailSourceId
const existingIndex = activeObs.zones.findIndex(matches)
if (bone.special === 'vertebral-precision') {
// Spine-zone behavior: always open VertebraPicker on click. Add the
// zone if missing; never toggle it off here (use the ✕ button).
let targetIndex = existingIndex
if (existingIndex < 0) {
const newZone: ObservationZone = {
boneId,
...(detailSourceId ? { detailSourceId } : {}),
}
const nextZones = [...activeObs.zones, newZone]
targetIndex = nextZones.length - 1
updateObservation(activeIndex, { ...activeObs, zones: nextZones })
}
setVertebraTarget(targetIndex)
return
}
if (bone.lateral === 'bilateral') {
const sidesPresent = activeObs.zones
.filter(
(z) =>
z.boneId === boneId &&
z.detailSourceId === detailSourceId,
)
.map((z) => z.side)
.filter(Boolean) as LateralSide[]
if (sidesPresent.includes('left') && sidesPresent.includes('right')) {
const nextZones = activeObs.zones.filter(
(z) =>
!(z.boneId === boneId && z.detailSourceId === detailSourceId),
)
updateObservation(activeIndex, { ...activeObs, zones: nextZones })
return
}
setLateralTarget({
/**
* Spine-zone behavior: always open VertebraPicker on click. Adds the
* zone if missing; never toggles it off here (use the ✕ button).
*/
function handleSpineZone(
activeObs: Observation,
boneId: string,
detailSourceId: string | undefined,
existingIndex: number,
) {
let targetIndex = existingIndex
if (existingIndex < 0) {
const newZone: ObservationZone = {
boneId,
...(detailSourceId ? { detailSourceId } : {}),
})
}
const nextZones = [...activeObs.zones, newZone]
targetIndex = nextZones.length - 1
updateObservation(activeIndex, { ...activeObs, zones: nextZones })
}
setVertebraTarget(targetIndex)
}
/** Bilateral zone: adding a side, or clearing both once left+right are set. */
function handleBilateralZone(
activeObs: Observation,
boneId: string,
detailSourceId: string | undefined,
) {
const sidesPresent = activeObs.zones
.filter(
(z) => z.boneId === boneId && z.detailSourceId === detailSourceId,
)
.map((z) => z.side)
.filter(Boolean) as LateralSide[]
if (sidesPresent.includes('left') && sidesPresent.includes('right')) {
const nextZones = activeObs.zones.filter(
(z) => !(z.boneId === boneId && z.detailSourceId === detailSourceId),
)
updateObservation(activeIndex, { ...activeObs, zones: nextZones })
return
}
setLateralTarget({
boneId,
...(detailSourceId ? { detailSourceId } : {}),
})
}
/** Plain zone: toggles it on the active observation. */
function toggleZone(
activeObs: Observation,
boneId: string,
detailSourceId: string | undefined,
existingIndex: number,
) {
const newZone: ObservationZone = {
boneId,
...(detailSourceId ? { detailSourceId } : {}),
@@ -280,6 +287,38 @@ export default function EditorRoute() {
updateObservation(activeIndex, { ...activeObs, zones: nextZones })
}
function onZoneClick(boneId: string, detailSourceId?: string) {
if (!report) return
const bone = bones.get(boneId) ?? detailBones.get(boneId)
if (!bone) return
if (bone.opensDetail && species?.details[bone.opensDetail]) {
handleDetailOpen(bone, bone.opensDetail, species.details[bone.opensDetail])
return
}
if (!activeObs || activeIndex < 0) {
activateFallbackObservation(report)
return
}
const matches = (z: ObservationZone) =>
z.boneId === boneId && z.detailSourceId === detailSourceId
const existingIndex = activeObs.zones.findIndex(matches)
if (bone.special === 'vertebral-precision') {
handleSpineZone(activeObs, boneId, detailSourceId, existingIndex)
return
}
if (bone.lateral === 'bilateral') {
handleBilateralZone(activeObs, boneId, detailSourceId)
return
}
toggleZone(activeObs, boneId, detailSourceId, existingIndex)
}
async function onExport() {
if (!report) return
setExporting(true)