214 lines
6.5 KiB
TypeScript
214 lines
6.5 KiB
TypeScript
import type {
|
|
Annotation,
|
|
LateralSide,
|
|
Observation,
|
|
ReportZone,
|
|
Vertebra,
|
|
} from '@faunosteo/domain'
|
|
import { isObservable, type SheetIndex } from './sheetIndex'
|
|
|
|
/**
|
|
* Zone interaction rules of the report editor, pure: what a click on an
|
|
* annotation means for the active observation, and the observation
|
|
* reducers the route applies. The rules are the legacy ones (click chain
|
|
* of EditorRoute, BDR-019 vertebra sync), keyed by annotation ids.
|
|
*/
|
|
|
|
export interface ZoneRef {
|
|
annotationId: string
|
|
viewId: string
|
|
}
|
|
|
|
export type ZoneClickDecision =
|
|
| { kind: 'ignore' }
|
|
| { kind: 'open_view'; viewId: string }
|
|
| { kind: 'activate_fallback' }
|
|
| { kind: 'open_vertebra_picker' }
|
|
| { kind: 'open_laterality_picker' }
|
|
| { kind: 'remove_both_sides' }
|
|
| { kind: 'toggle' }
|
|
|
|
export function sameZone(zone: ReportZone, ref: ZoneRef): boolean {
|
|
return zone.annotationId === ref.annotationId && zone.viewId === ref.viewId
|
|
}
|
|
|
|
export function refOf(annotation: Annotation): ZoneRef {
|
|
return { annotationId: annotation.id, viewId: annotation.view_id }
|
|
}
|
|
|
|
export function sidesPresent(obs: Observation, ref: ZoneRef): LateralSide[] {
|
|
return obs.zones
|
|
.filter((z) => sameZone(z, ref) && z.side)
|
|
.map((z) => z.side as LateralSide)
|
|
}
|
|
|
|
/**
|
|
* Legacy click chain: detail trigger opens its view and is never added; no
|
|
* active observation → the click only activates one; spine zones always open
|
|
* the picker; bilateral zones go through the side picker, or clear both sides
|
|
* when both are set; anything else toggles.
|
|
*/
|
|
export function decideZoneClick(
|
|
annotation: Annotation | undefined,
|
|
active: Observation | undefined,
|
|
): ZoneClickDecision {
|
|
if (!annotation) return { kind: 'ignore' }
|
|
if (annotation.kind === 'detail_trigger') {
|
|
return { kind: 'open_view', viewId: annotation.opens_view_id }
|
|
}
|
|
if (!active) return { kind: 'activate_fallback' }
|
|
if (annotation.vertebral_region_code !== null) {
|
|
return { kind: 'open_vertebra_picker' }
|
|
}
|
|
if (annotation.laterality === 'bilateral') {
|
|
const sides = sidesPresent(active, refOf(annotation))
|
|
return sides.includes('left') && sides.includes('right')
|
|
? { kind: 'remove_both_sides' }
|
|
: { kind: 'open_laterality_picker' }
|
|
}
|
|
return { kind: 'toggle' }
|
|
}
|
|
|
|
function newZone(annotation: Annotation, side?: LateralSide): ReportZone {
|
|
const zone: ReportZone = refOf(annotation)
|
|
if (isObservable(annotation)) {
|
|
zone.labelFrSnapshot = annotation.name.fr
|
|
zone.labelEnSnapshot = annotation.name.en
|
|
}
|
|
if (side) zone.side = side
|
|
return zone
|
|
}
|
|
|
|
function withZones(obs: Observation, zones: ReportZone[]): Observation {
|
|
return { ...obs, zones }
|
|
}
|
|
|
|
/** Plain zone: added when absent, removed when present (no side). */
|
|
export function toggleZone(
|
|
obs: Observation,
|
|
annotation: Annotation,
|
|
): Observation {
|
|
const index = obs.zones.findIndex((z) => sameZone(z, refOf(annotation)))
|
|
if (index >= 0)
|
|
return withZones(
|
|
obs,
|
|
obs.zones.filter((_, i) => i !== index),
|
|
)
|
|
return withZones(obs, [...obs.zones, newZone(annotation)])
|
|
}
|
|
|
|
/** The zone exists after the call; returns its index. */
|
|
export function ensureZone(
|
|
obs: Observation,
|
|
annotation: Annotation,
|
|
): { observation: Observation; index: number } {
|
|
const index = obs.zones.findIndex((z) => sameZone(z, refOf(annotation)))
|
|
if (index >= 0) return { observation: obs, index }
|
|
const zones = [...obs.zones, newZone(annotation)]
|
|
return { observation: withZones(obs, zones), index: zones.length - 1 }
|
|
}
|
|
|
|
/** Appends the sides not present yet; never removes one (legacy semantics). */
|
|
export function addSides(
|
|
obs: Observation,
|
|
annotation: Annotation,
|
|
sides: readonly LateralSide[],
|
|
): Observation {
|
|
const present = sidesPresent(obs, refOf(annotation))
|
|
const added = sides
|
|
.filter((side) => !present.includes(side))
|
|
.map((side) => newZone(annotation, side))
|
|
return added.length === 0 ? obs : withZones(obs, [...obs.zones, ...added])
|
|
}
|
|
|
|
export function removeAllSides(obs: Observation, ref: ZoneRef): Observation {
|
|
return withZones(
|
|
obs,
|
|
obs.zones.filter((z) => !sameZone(z, ref)),
|
|
)
|
|
}
|
|
|
|
export function removeZoneAt(obs: Observation, index: number): Observation {
|
|
return withZones(
|
|
obs,
|
|
obs.zones.filter((_, i) => i !== index),
|
|
)
|
|
}
|
|
|
|
function isSpineZone(zone: ReportZone, index: SheetIndex): boolean {
|
|
return index.isSpine(zone.annotationId)
|
|
}
|
|
|
|
/**
|
|
* Vertebrae of every spine zone of the observation, the zone at `priority`
|
|
* first (the one the picker was opened from), without duplicates.
|
|
*/
|
|
export function collectVertebrae(
|
|
obs: Observation,
|
|
index: SheetIndex,
|
|
priority: number,
|
|
): Vertebra[] {
|
|
const ordered = [
|
|
...obs.zones.filter((_, i) => i === priority),
|
|
...obs.zones.filter((_, i) => i !== priority),
|
|
]
|
|
const seen = new Set<string>()
|
|
const out: Vertebra[] = []
|
|
for (const zone of ordered) {
|
|
if (!isSpineZone(zone, index)) continue
|
|
for (const v of zone.vertebrae ?? []) {
|
|
const key = `${v.region}${v.index}`
|
|
if (seen.has(key)) continue
|
|
seen.add(key)
|
|
out.push({ ...v })
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
function regionOf(zone: ReportZone, index: SheetIndex): string | null {
|
|
const a = index.annotation(zone.annotationId)
|
|
return isObservable(a) ? a.vertebral_region_code : null
|
|
}
|
|
|
|
/**
|
|
* BDR-019: the picker edits every region at once. Per region the selection
|
|
* REPLACES the zone's vertebrae; a spine zone left without vertebrae is
|
|
* pruned; a region picked without a zone gets one on the view the picker
|
|
* was opened from (or the main view).
|
|
*/
|
|
export function syncVertebrae(
|
|
obs: Observation,
|
|
selection: readonly Vertebra[],
|
|
index: SheetIndex,
|
|
viewId: string,
|
|
): Observation {
|
|
const byRegion = new Map<string, Vertebra[]>()
|
|
for (const v of selection) {
|
|
const list = byRegion.get(v.region) ?? []
|
|
list.push({ ...v })
|
|
byRegion.set(v.region, list)
|
|
}
|
|
const kept: ReportZone[] = []
|
|
for (const zone of obs.zones) {
|
|
const region = isSpineZone(zone, index) ? regionOf(zone, index) : null
|
|
if (region === null) {
|
|
kept.push(zone)
|
|
continue
|
|
}
|
|
const picked = byRegion.get(region)
|
|
byRegion.delete(region)
|
|
if (!picked || picked.length === 0) continue
|
|
kept.push({ ...zone, vertebrae: picked })
|
|
}
|
|
for (const [region, picked] of byRegion) {
|
|
if (picked.length === 0) continue
|
|
const annotation =
|
|
index.spineAnnotation(region, viewId) ??
|
|
index.spineAnnotation(region, index.mainView.id)
|
|
if (!annotation) continue
|
|
kept.push({ ...newZone(annotation), vertebrae: picked })
|
|
}
|
|
return withZones(obs, kept)
|
|
}
|