feat(editor): unified spine picker — show all regions, click opens own tab

The vertebra picker now edits the whole spine at once instead of a single
zone. Seed it with the union of vertebrae across every spine zone of the
observation (collectSpineVertebrae) so switching tabs reveals selections made
on other zones. Clicking a zone always opens its own region tab (initialRegion
now takes priority over the first existing selection).

On confirm, syncSpineZones reconciles authoritatively per region: replace each
region's vertebrae (deselection sticks), prune a spine zone whose region drops
to zero, spawn a zone for a newly picked region. Non-spine zones and their
order are preserved untouched. Supersedes the additive distribute model.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude Opus 4.7 committed 2026-05-27 19:22:42 +02:00
1 parent fc7069c2cb
commit 38888cb3d1
5 files changed
+137 -96

No files matched your search

+9 -10
View File
@@ -19,7 +19,7 @@ import { writeCachedPractitioner } from '@/state/practitionerCache'
import { getSpecies } from '@/domain/species/registry' import { getSpecies } from '@/domain/species/registry'
import { loadSvg } from '@/anatomy/svgLoader' import { loadSvg } from '@/anatomy/svgLoader'
import { parseSvgBones } from '@/anatomy/svgConvention' import { parseSvgBones } from '@/anatomy/svgConvention'
import { distributeVertebraeToZones } 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 { SpeciesFindings } from '@/domain/schema/findings' import type { SpeciesFindings } from '@/domain/schema/findings'
@@ -439,18 +439,17 @@ export default function EditorRoute() {
setVertebraTarget(null) setVertebraTarget(null)
return return
} }
// Vertebrae picked in another region tab spawn/merge the matching // Picker edits every region at once: reconcile all spine zones —
// spine zone, so the SVG highlights it and its findings picker shows. // spawn newly picked regions, prune emptied ones, keep the rest.
const next = distributeVertebraeToZones( const next = syncSpineZones(fresh.zones, vertebrae, bones)
fresh.zones,
vertebraTarget,
vertebrae,
bones,
)
updateObservation(activeIndex, { ...fresh, zones: next }) updateObservation(activeIndex, { ...fresh, zones: next })
setVertebraTarget(null) setVertebraTarget(null)
}} }}
initial={activeObs.zones[vertebraTarget].vertebrae ?? []} initial={collectSpineVertebrae(
activeObs.zones,
bones,
activeObs.zones[vertebraTarget].boneId,
)}
formula={species.vertebralFormula} formula={species.vertebralFormula}
title={`${t('vertebra.title_prefix')}${boneLabel( title={`${t('vertebra.title_prefix')}${boneLabel(
bones.get(activeObs.zones[vertebraTarget].boneId), bones.get(activeObs.zones[vertebraTarget].boneId),
@@ -24,7 +24,7 @@ export interface VertebraPickerProps {
initial: Vertebra[] initial: Vertebra[]
formula: VertebralFormula formula: VertebralFormula
title: string title: string
/** Preselected tab. Overridden by the region of the first existing selection. */ /** Tab to open on. Takes priority so a click always lands on its own zone. */
initialRegion?: VertebralRegion initialRegion?: VertebralRegion
} }
@@ -40,7 +40,7 @@ export default function VertebraPicker({
const t = useT() const t = useT()
const [selected, setSelected] = useState<Vertebra[]>(initial) const [selected, setSelected] = useState<Vertebra[]>(initial)
const [activeRegion, setActiveRegion] = useState<VertebralRegion>( const [activeRegion, setActiveRegion] = useState<VertebralRegion>(
() => initial[0]?.region ?? initialRegion ?? 'C', () => initialRegion ?? initial[0]?.region ?? 'C',
) )
const has = (r: VertebralRegion, i: number) => const has = (r: VertebralRegion, i: number) =>
+62 -53
View File
@@ -42,81 +42,90 @@ export function findSpineZoneId(
const key = (v: Vertebra) => `${v.region}${v.index}` const key = (v: Vertebra) => `${v.region}${v.index}`
/** Set union of two vertebra lists, keyed by region+index, preserving order. */ const isSpineZone = (z: ObservationZone, bones: Map<string, Bone>) =>
function unionVertebrae(base: Vertebra[], extra: Vertebra[]): Vertebra[] { !z.detailSourceId && bones.get(z.boneId)?.special === 'vertebral-precision'
const seen = new Set(base.map(key))
const out = [...base] /**
for (const v of extra) { * Gathers the union of vertebrae across every spine zone of an observation so
if (seen.has(key(v))) continue * the picker can show selections from all regions at once. `priorityBoneId`'s
seen.add(key(v)) * vertebrae are listed first so the picker opens on the clicked zone's tab
out.push(v) * (the picker keys its default tab off the first vertebra's region).
*
* Deduplicated by region+index; order otherwise follows zone order.
*/
export function collectSpineVertebrae(
zones: ObservationZone[],
bones: Map<string, Bone>,
priorityBoneId?: string,
): Vertebra[] {
const spine = zones.filter((z) => isSpineZone(z, bones))
const ordered = priorityBoneId
? [
...spine.filter((z) => z.boneId === priorityBoneId),
...spine.filter((z) => z.boneId !== priorityBoneId),
]
: spine
const out: Vertebra[] = []
const seen = new Set<string>()
for (const z of ordered) {
for (const v of z.vertebrae ?? []) {
if (seen.has(key(v))) continue
seen.add(key(v))
out.push(v)
}
} }
return out return out
} }
/** /**
* Spreads vertebrae confirmed in the picker across one spine zone per region. * Reconciles the observation's spine zones with the full set of vertebrae the
* picker confirmed (the picker shows and edits every region at once).
* *
* The picker is anchored on the clicked zone (targetIndex) but lets the user * Authoritative per region: each region's zone gets exactly the vertebrae
* switch region tabs. Vertebrae picked in another region must behave as if the * picked for it (replace, so unchecking sticks); a region with no vertebrae
* matching spine zone was also selected: it gets created if missing so the SVG * left has its spine zone pruned; a region picked for the first time spawns its
* highlights it and its findings picker appears. * zone so the SVG highlights it and its findings picker appears. Non-spine
* * zones (and their order) are preserved untouched.
* The target region is replaced (the picker was seeded with its full set, so
* unchecking deselects). Other regions are merged additively so a side-tab pick
* never wipes a pre-existing zone the picker did not display.
* *
* Returns a new zones array; the original is not mutated. * Returns a new zones array; the original is not mutated.
*/ */
export function distributeVertebraeToZones( export function syncSpineZones(
zones: ObservationZone[], zones: ObservationZone[],
targetIndex: number,
vertebrae: Vertebra[], vertebrae: Vertebra[],
bones: Map<string, Bone>, bones: Map<string, Bone>,
): ObservationZone[] { ): ObservationZone[] {
const target = zones[targetIndex]
if (!target) return zones
const targetBone = bones.get(target.boneId)
const targetRegion =
regionFromLabel(targetBone?.fr ?? '') ?? regionFromLabel(targetBone?.en ?? '')
// Unknown target region: cannot split safely — keep all vertebrae on target.
if (!targetRegion) {
const next = [...zones]
next[targetIndex] = { ...target, vertebrae }
return next
}
const byRegion = new Map<VertebralRegion, Vertebra[]>() const byRegion = new Map<VertebralRegion, Vertebra[]>()
for (const v of vertebrae) { for (const v of vertebrae) {
byRegion.set(v.region, [...(byRegion.get(v.region) ?? []), v]) byRegion.set(v.region, [...(byRegion.get(v.region) ?? []), v])
} }
const next = [...zones] const result: ObservationZone[] = []
const unmapped: Vertebra[] = [] const handled = new Set<VertebralRegion>()
for (const [region, regionVerts] of byRegion) { for (const z of zones) {
if (region === targetRegion) continue const bone = bones.get(z.boneId)
const zoneId = findSpineZoneId(region, bones) const region =
if (!zoneId) { !z.detailSourceId && bone?.special === 'vertebral-precision'
unmapped.push(...regionVerts) ? (regionFromLabel(bone.fr) ?? regionFromLabel(bone.en))
: undefined
// Non-spine, detail-sourced, or unrecognized-label zones pass through.
if (!region) {
result.push(z)
continue continue
} }
const idx = next.findIndex((z) => z.boneId === zoneId && !z.detailSourceId) const verts = byRegion.get(region)
if (idx >= 0) { if (!verts) continue // region deselected to zero → prune the zone
next[idx] = { result.push({ ...z, vertebrae: verts })
...next[idx], handled.add(region)
vertebrae: unionVertebrae(next[idx].vertebrae ?? [], regionVerts),
}
} else {
next.push({ boneId: zoneId, vertebrae: regionVerts })
}
} }
next[targetIndex] = { // Regions picked that had no existing zone yet → spawn one.
...target, for (const [region, verts] of byRegion) {
vertebrae: [...(byRegion.get(targetRegion) ?? []), ...unmapped], if (handled.has(region)) continue
const zoneId = findSpineZoneId(region, bones)
if (zoneId) result.push({ boneId: zoneId, vertebrae: verts })
} }
return next
return result
} }
@@ -63,7 +63,7 @@ describe('VertebraPicker', () => {
) )
}) })
it('region of first existing selection wins over initialRegion', () => { it('opens on initialRegion even when other regions are pre-selected', () => {
render( render(
<VertebraPicker <VertebraPicker
open open
@@ -71,11 +71,11 @@ describe('VertebraPicker', () => {
onConfirm={() => {}} onConfirm={() => {}}
initial={[{ region: 'S', index: 2 }]} initial={[{ region: 'S', index: 2 }]}
formula={EQUINE_VERTEBRAL_FORMULA} formula={EQUINE_VERTEBRAL_FORMULA}
title="Vertèbres sacrales" title="Vertèbres cervicales"
initialRegion="C" initialRegion="C"
/>, />,
) )
expect(screen.getByRole('tab', { name: /Sacrales/ })).toHaveAttribute( expect(screen.getByRole('tab', { name: /Cervicales/ })).toHaveAttribute(
'aria-selected', 'aria-selected',
'true', 'true',
) )
+61 -28
View File
@@ -1,7 +1,8 @@
import { describe, it, expect } from 'vitest' import { describe, it, expect } from 'vitest'
import { import {
findSpineZoneId, findSpineZoneId,
distributeVertebraeToZones, collectSpineVertebrae,
syncSpineZones,
} from '@/lib/spineZones' } from '@/lib/spineZones'
import type { Bone } from '@/anatomy/types' import type { Bone } from '@/anatomy/types'
import type { ObservationZone, Vertebra } from '@/domain/schema/observation' import type { ObservationZone, Vertebra } from '@/domain/schema/observation'
@@ -36,52 +37,84 @@ describe('findSpineZoneId', () => {
}) })
}) })
describe('distributeVertebraeToZones', () => { describe('collectSpineVertebrae', () => {
it('keeps same-region vertebrae on the target zone', () => { it('unions vertebrae across all spine zones', () => {
const zones: ObservationZone[] = [{ boneId: 'z-l' }] const zones: ObservationZone[] = [
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('L', 2)], BONES) { boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] },
expect(next).toHaveLength(1) { boneId: 'z-c', vertebrae: [v('C', 3)] },
expect(next[0].vertebrae).toEqual([v('L', 1), v('L', 2)]) ]
expect(collectSpineVertebrae(zones, BONES)).toEqual([v('L', 1), v('L', 2), v('C', 3)])
}) })
it('spawns a new zone for cross-region vertebrae', () => { it('lists the priority zone vertebrae first', () => {
const zones: ObservationZone[] = [
{ boneId: 'z-c', vertebrae: [v('C', 3)] },
{ boneId: 'z-l', vertebrae: [v('L', 1)] },
]
expect(collectSpineVertebrae(zones, BONES, 'z-l')).toEqual([v('L', 1), v('C', 3)])
})
it('ignores non-spine zones and dedups', () => {
const zones: ObservationZone[] = [
{ boneId: 'z-shoulder' },
{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 1)] },
]
expect(collectSpineVertebrae(zones, BONES)).toEqual([v('L', 1)])
})
})
describe('syncSpineZones', () => {
it('keeps same-region vertebrae on the existing zone', () => {
const zones: ObservationZone[] = [{ boneId: 'z-l' }] const zones: ObservationZone[] = [{ boneId: 'z-l' }]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 3)], BONES) const next = syncSpineZones(zones, [v('L', 1), v('L', 2)], BONES)
expect(next).toEqual([{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] }])
})
it('spawns a new zone for a newly picked region', () => {
const zones: ObservationZone[] = [{ boneId: 'z-l' }]
const next = syncSpineZones(zones, [v('L', 1), v('C', 3)], BONES)
expect(next).toHaveLength(2) expect(next).toHaveLength(2)
expect(next[0]).toEqual({ boneId: 'z-l', vertebrae: [v('L', 1)] }) expect(next[0]).toEqual({ boneId: 'z-l', vertebrae: [v('L', 1)] })
expect(next[1]).toEqual({ boneId: 'z-c', vertebrae: [v('C', 3)] }) expect(next[1]).toEqual({ boneId: 'z-c', vertebrae: [v('C', 3)] })
}) })
it('merges (does not wipe) a pre-existing other-region zone', () => { it('replaces a region authoritatively (deselection sticks)', () => {
const zones: ObservationZone[] = [ const zones: ObservationZone[] = [
{ boneId: 'z-l', vertebrae: [v('L', 1)] }, { boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] },
{ boneId: 'z-c', vertebrae: [v('C', 2)] }, { boneId: 'z-c', vertebrae: [v('C', 2), v('C', 3)] },
] ]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 4)], BONES) const next = syncSpineZones(zones, [v('L', 1), v('C', 3)], BONES)
expect(next).toHaveLength(2) expect(next).toHaveLength(2)
expect(next[0].vertebrae).toEqual([v('L', 1)]) expect(next[0].vertebrae).toEqual([v('L', 1)])
expect(next[1].vertebrae).toEqual([v('C', 2), v('C', 4)]) expect(next[1].vertebrae).toEqual([v('C', 3)])
}) })
it('replaces the target region so deselection within it sticks', () => { it('prunes a spine zone whose region dropped to zero', () => {
const zones: ObservationZone[] = [{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] }] const zones: ObservationZone[] = [
const next = distributeVertebraeToZones(zones, 0, [v('L', 1)], BONES) { boneId: 'z-l', vertebrae: [v('L', 1)] },
expect(next[0].vertebrae).toEqual([v('L', 1)]) { boneId: 'z-c', vertebrae: [v('C', 1)] },
]
const next = syncSpineZones(zones, [v('C', 1)], BONES)
expect(next).toEqual([{ boneId: 'z-c', vertebrae: [v('C', 1)] }])
}) })
it('falls back to dumping all vertebrae on a non-spine target zone', () => { it('preserves non-spine zones untouched', () => {
const zones: ObservationZone[] = [{ boneId: 'z-shoulder' }] const zones: ObservationZone[] = [
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 2)], BONES) { boneId: 'z-shoulder' },
expect(next).toHaveLength(1) { boneId: 'z-l', vertebrae: [v('L', 1)] },
expect(next[0].vertebrae).toEqual([v('L', 1), v('C', 2)]) ]
const next = syncSpineZones(zones, [v('L', 2)], BONES)
expect(next).toEqual([
{ boneId: 'z-shoulder' },
{ boneId: 'z-l', vertebrae: [v('L', 2)] },
])
}) })
it('routes vertebrae of an unmapped region back onto the target zone', () => { it('drops vertebrae of a region with no spine zone in the SVG', () => {
const noCervical = new Map(BONES) const noCervical = new Map(BONES)
noCervical.delete('z-c') noCervical.delete('z-c')
const zones: ObservationZone[] = [{ boneId: 'z-l' }] const zones: ObservationZone[] = [{ boneId: 'z-l', vertebrae: [v('L', 1)] }]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 2)], noCervical) const next = syncSpineZones(zones, [v('L', 1), v('C', 2)], noCervical)
expect(next).toHaveLength(1) expect(next).toEqual([{ boneId: 'z-l', vertebrae: [v('L', 1)] }])
expect(next[0].vertebrae).toEqual([v('L', 1), v('C', 2)])
}) })
}) })