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
+69 -30
+69 -30
View File
@@ -21,6 +21,7 @@ import { parseSvgBones } from '@/anatomy/svgConvention'
import { collectSpineVertebrae, syncSpineZones } from '@/lib/spineZones' import { collectSpineVertebrae, syncSpineZones } from '@/lib/spineZones'
import type { Bone, BoneState } from '@/anatomy/types' import type { Bone, BoneState } from '@/anatomy/types'
import type { Observation, ObservationZone } from '@/domain/schema/observation' import type { Observation, ObservationZone } from '@/domain/schema/observation'
import type { Report } from '@/domain/schema/report'
import type { SpeciesFindings } from '@/domain/schema/findings' import type { SpeciesFindings } from '@/domain/schema/findings'
import type { Practitioner } from '@/domain/schema/practitioner' import type { Practitioner } from '@/domain/schema/practitioner'
import type { Animal, Owner } from '@/domain/schema/animal' import type { Animal, Owner } from '@/domain/schema/animal'
@@ -197,38 +198,37 @@ export default function EditorRoute() {
setActiveObservation(activeIndex === index ? -1 : index) setActiveObservation(activeIndex === index ? -1 : index)
} }
function onZoneClick(boneId: string, detailSourceId?: string) { /** Opens the detail-popup modal for a bone that carries data-anno-opens-detail. */
if (!report) return function handleDetailOpen(bone: Bone, detailId: string, detailSrc: string) {
const bone = bones.get(boneId) ?? detailBones.get(boneId)
if (!bone) return
if (bone.opensDetail && species?.details[bone.opensDetail]) {
setDetail({ setDetail({
id: bone.opensDetail, id: detailId,
src: species.details[bone.opensDetail], src: detailSrc,
title: boneLabel(bone, locale, bone.opensDetail), title: boneLabel(bone, locale, detailId),
}) })
return
} }
if (!activeObs || activeIndex < 0) { /**
// No expanded observation — auto-expand the most recent one (or the * No expanded observation — auto-expand the most recent one (or the
// first if no rows yet) so the click lands somewhere. * 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 = const fallbackIndex =
report.observations.length > 0 ? report.observations.length - 1 : -1 report.observations.length > 0 ? report.observations.length - 1 : -1
if (fallbackIndex < 0) return if (fallbackIndex < 0) return
setActiveObservation(fallbackIndex) 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 * Spine-zone behavior: always open VertebraPicker on click. Adds the
const existingIndex = activeObs.zones.findIndex(matches) * zone if missing; never toggles it off here (use the ✕ button).
*/
if (bone.special === 'vertebral-precision') { function handleSpineZone(
// Spine-zone behavior: always open VertebraPicker on click. Add the activeObs: Observation,
// zone if missing; never toggle it off here (use the ✕ button). boneId: string,
detailSourceId: string | undefined,
existingIndex: number,
) {
let targetIndex = existingIndex let targetIndex = existingIndex
if (existingIndex < 0) { if (existingIndex < 0) {
const newZone: ObservationZone = { const newZone: ObservationZone = {
@@ -240,23 +240,24 @@ export default function EditorRoute() {
updateObservation(activeIndex, { ...activeObs, zones: nextZones }) updateObservation(activeIndex, { ...activeObs, zones: nextZones })
} }
setVertebraTarget(targetIndex) setVertebraTarget(targetIndex)
return
} }
if (bone.lateral === 'bilateral') { /** 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 const sidesPresent = activeObs.zones
.filter( .filter(
(z) => (z) => z.boneId === boneId && z.detailSourceId === detailSourceId,
z.boneId === boneId &&
z.detailSourceId === detailSourceId,
) )
.map((z) => z.side) .map((z) => z.side)
.filter(Boolean) as LateralSide[] .filter(Boolean) as LateralSide[]
if (sidesPresent.includes('left') && sidesPresent.includes('right')) { if (sidesPresent.includes('left') && sidesPresent.includes('right')) {
const nextZones = activeObs.zones.filter( const nextZones = activeObs.zones.filter(
(z) => (z) => !(z.boneId === boneId && z.detailSourceId === detailSourceId),
!(z.boneId === boneId && z.detailSourceId === detailSourceId),
) )
updateObservation(activeIndex, { ...activeObs, zones: nextZones }) updateObservation(activeIndex, { ...activeObs, zones: nextZones })
return return
@@ -266,9 +267,15 @@ export default function EditorRoute() {
boneId, boneId,
...(detailSourceId ? { detailSourceId } : {}), ...(detailSourceId ? { detailSourceId } : {}),
}) })
return
} }
/** Plain zone: toggles it on the active observation. */
function toggleZone(
activeObs: Observation,
boneId: string,
detailSourceId: string | undefined,
existingIndex: number,
) {
const newZone: ObservationZone = { const newZone: ObservationZone = {
boneId, boneId,
...(detailSourceId ? { detailSourceId } : {}), ...(detailSourceId ? { detailSourceId } : {}),
@@ -280,6 +287,38 @@ export default function EditorRoute() {
updateObservation(activeIndex, { ...activeObs, zones: nextZones }) 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() { async function onExport() {
if (!report) return if (!report) return
setExporting(true) setExporting(true)