feat(editor): VertebraPicker preselects region tab inferred from zone label
When opened from a spine zone, the picker now lands on the matching region tab instead of defaulting to Cervicales every time. Inference is substring-based on the FR label first, EN as fallback (cervic→C, thorac/dors→T, lombair/lumbar→L, sacral/sacre→S, caudal/coccyg→Cd), case- and accent-insensitive. An existing selection still wins over the inferred region so re-opening lands where the user left off. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
6a0ab22f76
commit
b3bd75d2e2
3 files changed
+94
-3
No files matched your search
@@ -3,7 +3,9 @@ import { useNavigate } from 'react-router-dom'
|
||||
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
|
||||
import DetailModal from '@/components/canvas/DetailModal'
|
||||
import ObservationList from '@/components/observation/ObservationList'
|
||||
import VertebraPicker from '@/components/observation/VertebraPicker'
|
||||
import VertebraPicker, {
|
||||
regionFromLabel,
|
||||
} from '@/components/observation/VertebraPicker'
|
||||
import ConclusionPanel from '@/components/report/ConclusionPanel'
|
||||
import ReportHeader from '@/components/report/ReportHeader'
|
||||
import Button from '@/components/ui/Button'
|
||||
@@ -260,6 +262,10 @@ export default function EditorRoute() {
|
||||
initial={activeObs.zones[vertebraTarget].vertebrae ?? []}
|
||||
formula={species.vertebralFormula}
|
||||
title={`Préciser les vertèbres — ${bones.get(activeObs.zones[vertebraTarget].boneId)?.fr ?? ''}`}
|
||||
initialRegion={(() => {
|
||||
const b = bones.get(activeObs.zones[vertebraTarget].boneId)
|
||||
return regionFromLabel(b?.fr ?? '') ?? regionFromLabel(b?.en ?? '')
|
||||
})()}
|
||||
/>
|
||||
)}
|
||||
</main>
|
||||
|
||||
@@ -13,6 +13,26 @@ const REGION_LABELS: Record<VertebralRegion, string> = {
|
||||
Cd: 'Caudales',
|
||||
}
|
||||
|
||||
/**
|
||||
* Infers the matching vertebral region from a free-form anatomical label
|
||||
* (zone name in FR or EN). Matches on substring roots, case-insensitive,
|
||||
* accent-insensitive. Returns undefined when no root matches.
|
||||
*/
|
||||
export function regionFromLabel(label: string): VertebralRegion | undefined {
|
||||
const norm = label
|
||||
.normalize('NFD')
|
||||
.replace(/\p{Diacritic}/gu, '')
|
||||
.toLowerCase()
|
||||
// Order matters: 'caudal'/'coccyg' before 'cervic' is fine; just be
|
||||
// explicit so no root subsumes another.
|
||||
if (norm.includes('cervic')) return 'C'
|
||||
if (norm.includes('thorac') || norm.includes('dors')) return 'T'
|
||||
if (norm.includes('lombair') || norm.includes('lumbar')) return 'L'
|
||||
if (norm.includes('sacral') || norm.includes('sacre')) return 'S'
|
||||
if (norm.includes('caudal') || norm.includes('coccyg')) return 'Cd'
|
||||
return undefined
|
||||
}
|
||||
|
||||
export interface VertebraPickerProps {
|
||||
open: boolean
|
||||
onClose: () => void
|
||||
@@ -20,6 +40,8 @@ export interface VertebraPickerProps {
|
||||
initial: Vertebra[]
|
||||
formula: VertebralFormula
|
||||
title: string
|
||||
/** Preselected tab. Overridden by the region of the first existing selection. */
|
||||
initialRegion?: VertebralRegion
|
||||
}
|
||||
|
||||
export default function VertebraPicker({
|
||||
@@ -29,9 +51,12 @@ export default function VertebraPicker({
|
||||
initial,
|
||||
formula,
|
||||
title,
|
||||
initialRegion,
|
||||
}: VertebraPickerProps) {
|
||||
const [selected, setSelected] = useState<Vertebra[]>(initial)
|
||||
const [activeRegion, setActiveRegion] = useState<VertebralRegion>('C')
|
||||
const [activeRegion, setActiveRegion] = useState<VertebralRegion>(
|
||||
() => initial[0]?.region ?? initialRegion ?? 'C',
|
||||
)
|
||||
|
||||
const has = (r: VertebralRegion, i: number) =>
|
||||
selected.some((v) => v.region === r && v.index === i)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import VertebraPicker from '@/components/observation/VertebraPicker'
|
||||
import VertebraPicker, {
|
||||
regionFromLabel,
|
||||
} from '@/components/observation/VertebraPicker'
|
||||
import { EQUINE_VERTEBRAL_FORMULA } from '@/domain/species/equine/vertebrae'
|
||||
|
||||
describe('VertebraPicker', () => {
|
||||
@@ -37,4 +39,62 @@ describe('VertebraPicker', () => {
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Valider' }))
|
||||
expect(onConfirm).toHaveBeenCalledWith([{ region: 'T', index: 12 }])
|
||||
})
|
||||
|
||||
it('preselects the tab matching initialRegion', () => {
|
||||
render(
|
||||
<VertebraPicker
|
||||
open
|
||||
onClose={() => {}}
|
||||
onConfirm={() => {}}
|
||||
initial={[]}
|
||||
formula={EQUINE_VERTEBRAL_FORMULA}
|
||||
title="Vertèbres lombaires"
|
||||
initialRegion="L"
|
||||
/>,
|
||||
)
|
||||
expect(screen.getByRole('tab', { name: /Lombaires/ })).toHaveAttribute(
|
||||
'aria-selected',
|
||||
'true',
|
||||
)
|
||||
})
|
||||
|
||||
it('region of first existing selection wins over initialRegion', () => {
|
||||
render(
|
||||
<VertebraPicker
|
||||
open
|
||||
onClose={() => {}}
|
||||
onConfirm={() => {}}
|
||||
initial={[{ region: 'S', index: 2 }]}
|
||||
formula={EQUINE_VERTEBRAL_FORMULA}
|
||||
title="Vertèbres sacrales"
|
||||
initialRegion="C"
|
||||
/>,
|
||||
)
|
||||
expect(screen.getByRole('tab', { name: /Sacrales/ })).toHaveAttribute(
|
||||
'aria-selected',
|
||||
'true',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('regionFromLabel', () => {
|
||||
it('matches FR roots (accent-insensitive)', () => {
|
||||
expect(regionFromLabel('Vertèbres cervicales')).toBe('C')
|
||||
expect(regionFromLabel('Vertèbres thoraciques')).toBe('T')
|
||||
expect(regionFromLabel('Vertèbres lombaires')).toBe('L')
|
||||
expect(regionFromLabel('Vertèbres sacrales')).toBe('S')
|
||||
expect(regionFromLabel('Vertèbres caudales')).toBe('Cd')
|
||||
})
|
||||
|
||||
it('matches EN roots', () => {
|
||||
expect(regionFromLabel('Cervical Vertebrae')).toBe('C')
|
||||
expect(regionFromLabel('Thoracic Vertebrae')).toBe('T')
|
||||
expect(regionFromLabel('Lumbar Vertebrae')).toBe('L')
|
||||
expect(regionFromLabel('Coccygeal Vertebrae')).toBe('Cd')
|
||||
})
|
||||
|
||||
it('returns undefined when no root matches', () => {
|
||||
expect(regionFromLabel('Épaule')).toBeUndefined()
|
||||
expect(regionFromLabel('')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user