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:
1 parent
fc7069c2cb
commit
38888cb3d1
5 files changed
+134
-93
No files matched your search
@@ -19,7 +19,7 @@ import { writeCachedPractitioner } from '@/state/practitionerCache'
|
||||
import { getSpecies } from '@/domain/species/registry'
|
||||
import { loadSvg } from '@/anatomy/svgLoader'
|
||||
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 { Observation, ObservationZone } from '@/domain/schema/observation'
|
||||
import type { SpeciesFindings } from '@/domain/schema/findings'
|
||||
@@ -439,18 +439,17 @@ export default function EditorRoute() {
|
||||
setVertebraTarget(null)
|
||||
return
|
||||
}
|
||||
// Vertebrae picked in another region tab spawn/merge the matching
|
||||
// spine zone, so the SVG highlights it and its findings picker shows.
|
||||
const next = distributeVertebraeToZones(
|
||||
fresh.zones,
|
||||
vertebraTarget,
|
||||
vertebrae,
|
||||
bones,
|
||||
)
|
||||
// Picker edits every region at once: reconcile all spine zones —
|
||||
// spawn newly picked regions, prune emptied ones, keep the rest.
|
||||
const next = syncSpineZones(fresh.zones, vertebrae, bones)
|
||||
updateObservation(activeIndex, { ...fresh, zones: next })
|
||||
setVertebraTarget(null)
|
||||
}}
|
||||
initial={activeObs.zones[vertebraTarget].vertebrae ?? []}
|
||||
initial={collectSpineVertebrae(
|
||||
activeObs.zones,
|
||||
bones,
|
||||
activeObs.zones[vertebraTarget].boneId,
|
||||
)}
|
||||
formula={species.vertebralFormula}
|
||||
title={`${t('vertebra.title_prefix')}${boneLabel(
|
||||
bones.get(activeObs.zones[vertebraTarget].boneId),
|
||||
|
||||
@@ -24,7 +24,7 @@ export interface VertebraPickerProps {
|
||||
initial: Vertebra[]
|
||||
formula: VertebralFormula
|
||||
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
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ export default function VertebraPicker({
|
||||
const t = useT()
|
||||
const [selected, setSelected] = useState<Vertebra[]>(initial)
|
||||
const [activeRegion, setActiveRegion] = useState<VertebralRegion>(
|
||||
() => initial[0]?.region ?? initialRegion ?? 'C',
|
||||
() => initialRegion ?? initial[0]?.region ?? 'C',
|
||||
)
|
||||
|
||||
const has = (r: VertebralRegion, i: number) =>
|
||||
|
||||
+59
-50
@@ -42,81 +42,90 @@ export function findSpineZoneId(
|
||||
|
||||
const key = (v: Vertebra) => `${v.region}${v.index}`
|
||||
|
||||
/** Set union of two vertebra lists, keyed by region+index, preserving order. */
|
||||
function unionVertebrae(base: Vertebra[], extra: Vertebra[]): Vertebra[] {
|
||||
const seen = new Set(base.map(key))
|
||||
const out = [...base]
|
||||
for (const v of extra) {
|
||||
const isSpineZone = (z: ObservationZone, bones: Map<string, Bone>) =>
|
||||
!z.detailSourceId && bones.get(z.boneId)?.special === 'vertebral-precision'
|
||||
|
||||
/**
|
||||
* Gathers the union of vertebrae across every spine zone of an observation so
|
||||
* the picker can show selections from all regions at once. `priorityBoneId`'s
|
||||
* vertebrae are listed first so the picker opens on the clicked zone's tab
|
||||
* (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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* switch region tabs. Vertebrae picked in another region must behave as if the
|
||||
* matching spine zone was also selected: it gets created if missing so the SVG
|
||||
* highlights it and its findings picker appears.
|
||||
*
|
||||
* 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.
|
||||
* Authoritative per region: each region's zone gets exactly the vertebrae
|
||||
* picked for it (replace, so unchecking sticks); a region with no vertebrae
|
||||
* left has its spine zone pruned; a region picked for the first time spawns its
|
||||
* zone so the SVG highlights it and its findings picker appears. Non-spine
|
||||
* zones (and their order) are preserved untouched.
|
||||
*
|
||||
* Returns a new zones array; the original is not mutated.
|
||||
*/
|
||||
export function distributeVertebraeToZones(
|
||||
export function syncSpineZones(
|
||||
zones: ObservationZone[],
|
||||
targetIndex: number,
|
||||
vertebrae: Vertebra[],
|
||||
bones: Map<string, Bone>,
|
||||
): 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[]>()
|
||||
for (const v of vertebrae) {
|
||||
byRegion.set(v.region, [...(byRegion.get(v.region) ?? []), v])
|
||||
}
|
||||
|
||||
const next = [...zones]
|
||||
const unmapped: Vertebra[] = []
|
||||
const result: ObservationZone[] = []
|
||||
const handled = new Set<VertebralRegion>()
|
||||
|
||||
for (const [region, regionVerts] of byRegion) {
|
||||
if (region === targetRegion) continue
|
||||
const zoneId = findSpineZoneId(region, bones)
|
||||
if (!zoneId) {
|
||||
unmapped.push(...regionVerts)
|
||||
for (const z of zones) {
|
||||
const bone = bones.get(z.boneId)
|
||||
const region =
|
||||
!z.detailSourceId && bone?.special === 'vertebral-precision'
|
||||
? (regionFromLabel(bone.fr) ?? regionFromLabel(bone.en))
|
||||
: undefined
|
||||
// Non-spine, detail-sourced, or unrecognized-label zones pass through.
|
||||
if (!region) {
|
||||
result.push(z)
|
||||
continue
|
||||
}
|
||||
const idx = next.findIndex((z) => z.boneId === zoneId && !z.detailSourceId)
|
||||
if (idx >= 0) {
|
||||
next[idx] = {
|
||||
...next[idx],
|
||||
vertebrae: unionVertebrae(next[idx].vertebrae ?? [], regionVerts),
|
||||
}
|
||||
} else {
|
||||
next.push({ boneId: zoneId, vertebrae: regionVerts })
|
||||
}
|
||||
const verts = byRegion.get(region)
|
||||
if (!verts) continue // region deselected to zero → prune the zone
|
||||
result.push({ ...z, vertebrae: verts })
|
||||
handled.add(region)
|
||||
}
|
||||
|
||||
next[targetIndex] = {
|
||||
...target,
|
||||
vertebrae: [...(byRegion.get(targetRegion) ?? []), ...unmapped],
|
||||
// Regions picked that had no existing zone yet → spawn one.
|
||||
for (const [region, verts] of byRegion) {
|
||||
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(
|
||||
<VertebraPicker
|
||||
open
|
||||
@@ -71,11 +71,11 @@ describe('VertebraPicker', () => {
|
||||
onConfirm={() => {}}
|
||||
initial={[{ region: 'S', index: 2 }]}
|
||||
formula={EQUINE_VERTEBRAL_FORMULA}
|
||||
title="Vertèbres sacrales"
|
||||
title="Vertèbres cervicales"
|
||||
initialRegion="C"
|
||||
/>,
|
||||
)
|
||||
expect(screen.getByRole('tab', { name: /Sacrales/ })).toHaveAttribute(
|
||||
expect(screen.getByRole('tab', { name: /Cervicales/ })).toHaveAttribute(
|
||||
'aria-selected',
|
||||
'true',
|
||||
)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
findSpineZoneId,
|
||||
distributeVertebraeToZones,
|
||||
collectSpineVertebrae,
|
||||
syncSpineZones,
|
||||
} from '@/lib/spineZones'
|
||||
import type { Bone } from '@/anatomy/types'
|
||||
import type { ObservationZone, Vertebra } from '@/domain/schema/observation'
|
||||
@@ -36,52 +37,84 @@ describe('findSpineZoneId', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('distributeVertebraeToZones', () => {
|
||||
it('keeps same-region vertebrae on the target zone', () => {
|
||||
const zones: ObservationZone[] = [{ boneId: 'z-l' }]
|
||||
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('L', 2)], BONES)
|
||||
expect(next).toHaveLength(1)
|
||||
expect(next[0].vertebrae).toEqual([v('L', 1), v('L', 2)])
|
||||
describe('collectSpineVertebrae', () => {
|
||||
it('unions vertebrae across all spine zones', () => {
|
||||
const zones: ObservationZone[] = [
|
||||
{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] },
|
||||
{ boneId: 'z-c', vertebrae: [v('C', 3)] },
|
||||
]
|
||||
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 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[0]).toEqual({ boneId: 'z-l', vertebrae: [v('L', 1)] })
|
||||
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[] = [
|
||||
{ boneId: 'z-l', vertebrae: [v('L', 1)] },
|
||||
{ boneId: 'z-c', vertebrae: [v('C', 2)] },
|
||||
{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 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[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', () => {
|
||||
const zones: ObservationZone[] = [{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] }]
|
||||
const next = distributeVertebraeToZones(zones, 0, [v('L', 1)], BONES)
|
||||
expect(next[0].vertebrae).toEqual([v('L', 1)])
|
||||
it('prunes a spine zone whose region dropped to zero', () => {
|
||||
const zones: ObservationZone[] = [
|
||||
{ boneId: 'z-l', vertebrae: [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', () => {
|
||||
const zones: ObservationZone[] = [{ boneId: 'z-shoulder' }]
|
||||
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 2)], BONES)
|
||||
expect(next).toHaveLength(1)
|
||||
expect(next[0].vertebrae).toEqual([v('L', 1), v('C', 2)])
|
||||
it('preserves non-spine zones untouched', () => {
|
||||
const zones: ObservationZone[] = [
|
||||
{ boneId: 'z-shoulder' },
|
||||
{ boneId: 'z-l', vertebrae: [v('L', 1)] },
|
||||
]
|
||||
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)
|
||||
noCervical.delete('z-c')
|
||||
const zones: ObservationZone[] = [{ boneId: 'z-l' }]
|
||||
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 2)], noCervical)
|
||||
expect(next).toHaveLength(1)
|
||||
expect(next[0].vertebrae).toEqual([v('L', 1), v('C', 2)])
|
||||
const zones: ObservationZone[] = [{ boneId: 'z-l', vertebrae: [v('L', 1)] }]
|
||||
const next = syncSpineZones(zones, [v('L', 1), v('C', 2)], noCervical)
|
||||
expect(next).toEqual([{ boneId: 'z-l', vertebrae: [v('L', 1)] }])
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user