chore(workspace): move the app to apps/app under a pnpm workspace (step 2)
144 files moved with git mv (pure renames). Root package.json delegates dev/typecheck/test:run/lint/build to the packages; Node 24 LTS and pnpm 10.13.1 pinned (.nvmrc, .node-version, engines, packageManager, root devDependency, engine-strict). Dockerfile builds @faunosteo/app on node:24-alpine and copies apps/app/dist; nginx and runtime config unchanged. CI workflow for Gitea Actions. packages/ reserved (README only). Docs synced: WORKSPACE.md, CLAUDE.md, README, DEPLOY, CHANGELOG, plan. 271/271 tests, typecheck, lint, build green from the root.
This commit is contained in:
1 parent
c8d48ab4cc
commit
8673475293
164 files changed
+348
-128
No files matched your search
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Asserts a condition is truthy; throws with the given message otherwise.
|
||||
* Use to enforce invariants at runtime boundaries.
|
||||
*/
|
||||
export function assert(condition: unknown, message: string): asserts condition {
|
||||
if (!condition) {
|
||||
throw new Error(`Assertion failed: ${message}`)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { Locale } from '@/i18n/types'
|
||||
|
||||
export function todayIso(): string {
|
||||
return new Date().toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
export function nowIso(): string {
|
||||
return new Date().toISOString()
|
||||
}
|
||||
|
||||
const LOCALE_TAG: Record<Locale, string> = {
|
||||
fr: 'fr-FR',
|
||||
en: 'en-US',
|
||||
}
|
||||
|
||||
export function formatDate(isoDate: string, locale: Locale): string {
|
||||
const match = isoDate.match(/^(\d{4})-(\d{2})-(\d{2})$/)
|
||||
if (!match) throw new Error(`Invalid ISO date: ${isoDate}`)
|
||||
const [, y, mo, d] = match
|
||||
const date = new Date(Date.UTC(Number(y), Number(mo) - 1, Number(d)))
|
||||
if (Number.isNaN(date.getTime())) throw new Error(`Invalid ISO date: ${isoDate}`)
|
||||
return new Intl.DateTimeFormat(LOCALE_TAG[locale], {
|
||||
day: 'numeric',
|
||||
month: 'long',
|
||||
year: 'numeric',
|
||||
timeZone: 'UTC',
|
||||
}).format(date)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Generates a collision-resistant unique id using crypto.randomUUID().
|
||||
* Falls back to a timestamp-based id in environments without crypto.
|
||||
*/
|
||||
export function generateId(): string {
|
||||
if (
|
||||
typeof crypto !== 'undefined' &&
|
||||
typeof crypto.randomUUID === 'function'
|
||||
) {
|
||||
return crypto.randomUUID()
|
||||
}
|
||||
return `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
import type {
|
||||
ObservationZone,
|
||||
Vertebra,
|
||||
VertebralRegion,
|
||||
} from '@/domain/schema/observation'
|
||||
import type { Bone } from '@/anatomy/types'
|
||||
|
||||
/**
|
||||
* 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()
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the spine-zone boneId whose label maps to the given vertebral region.
|
||||
* Spine zones are the SVG elements flagged data-anno-special="vertebral-precision".
|
||||
* Returns undefined when the SVG exposes no zone for that region.
|
||||
*/
|
||||
export function findSpineZoneId(
|
||||
region: VertebralRegion,
|
||||
bones: Map<string, Bone>,
|
||||
): string | undefined {
|
||||
for (const [id, bone] of bones) {
|
||||
if (bone.special !== 'vertebral-precision') continue
|
||||
const r = regionFromLabel(bone.fr) ?? regionFromLabel(bone.en)
|
||||
if (r === region) return id
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
const key = (v: Vertebra) => `${v.region}${v.index}`
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconciles the observation's spine zones with the full set of vertebrae the
|
||||
* picker confirmed (the picker shows and edits every region at once).
|
||||
*
|
||||
* 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 syncSpineZones(
|
||||
zones: ObservationZone[],
|
||||
vertebrae: Vertebra[],
|
||||
bones: Map<string, Bone>,
|
||||
): ObservationZone[] {
|
||||
const byRegion = new Map<VertebralRegion, Vertebra[]>()
|
||||
for (const v of vertebrae) {
|
||||
byRegion.set(v.region, [...(byRegion.get(v.region) ?? []), v])
|
||||
}
|
||||
|
||||
const result: ObservationZone[] = []
|
||||
const handled = new Set<VertebralRegion>()
|
||||
|
||||
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 verts = byRegion.get(region)
|
||||
if (!verts) continue // region deselected to zero → prune the zone
|
||||
result.push({ ...z, vertebrae: verts })
|
||||
handled.add(region)
|
||||
}
|
||||
|
||||
// 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 result
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import type { ZoneFindings } from '@/domain/schema/findings'
|
||||
|
||||
/**
|
||||
* Bridges SVG anatomy labels to the JSON findings catalog when the two
|
||||
* carry slightly different strings (casse, accents, abbreviations like
|
||||
* "(ATM)" vs "Articulation Temporo-Mandibulaire (ATM)", or genuine typos).
|
||||
*
|
||||
* Strategy: normalize aggressively (lowercase + NFD-strip accents + drop
|
||||
* non-alphanumerics), then score each (boneLabel, zoneLabel) pair:
|
||||
*
|
||||
* 0 exact normalized match
|
||||
* 1 one normalized string is contained in the other (substring)
|
||||
* 2+d Levenshtein distance d, within a length-relative threshold
|
||||
* ∞ beyond tolerance — discarded
|
||||
*
|
||||
* Language strategy: English first (no accents → fewer normalization
|
||||
* edge cases), French as fallback, fuzzy matching across both languages
|
||||
* as last resort. Falls through silently when nothing matches.
|
||||
*/
|
||||
|
||||
export function normalizeLabel(s: string | undefined | null): string {
|
||||
if (!s) return ''
|
||||
return s
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/[^a-z0-9]+/g, '')
|
||||
}
|
||||
|
||||
export function levenshtein(a: string, b: string): number {
|
||||
if (a === b) return 0
|
||||
const al = a.length
|
||||
const bl = b.length
|
||||
if (al === 0) return bl
|
||||
if (bl === 0) return al
|
||||
let prev: number[] = new Array(bl + 1)
|
||||
let curr: number[] = new Array(bl + 1)
|
||||
for (let j = 0; j <= bl; j += 1) prev[j] = j
|
||||
for (let i = 1; i <= al; i += 1) {
|
||||
curr[0] = i
|
||||
for (let j = 1; j <= bl; j += 1) {
|
||||
const cost = a.charCodeAt(i - 1) === b.charCodeAt(j - 1) ? 0 : 1
|
||||
curr[j] = Math.min(prev[j] + 1, curr[j - 1] + 1, prev[j - 1] + cost)
|
||||
}
|
||||
const tmp = prev
|
||||
prev = curr
|
||||
curr = tmp
|
||||
}
|
||||
return prev[bl]
|
||||
}
|
||||
|
||||
const SCORE_EXACT = 0
|
||||
const SCORE_SUBSTRING = 1
|
||||
const SCORE_FUZZY_BASE = 2
|
||||
const SCORE_NO_MATCH = Number.POSITIVE_INFINITY
|
||||
|
||||
/**
|
||||
* Compares two raw labels after normalization. Lower scores beat higher
|
||||
* ones; SCORE_NO_MATCH means the inputs are too dissimilar to be linked
|
||||
* confidently.
|
||||
*/
|
||||
export function scorePair(boneLabel: string, zoneLabel: string): number {
|
||||
const a = normalizeLabel(boneLabel)
|
||||
const b = normalizeLabel(zoneLabel)
|
||||
if (!a || !b) return SCORE_NO_MATCH
|
||||
if (a === b) return SCORE_EXACT
|
||||
if (a.includes(b) || b.includes(a)) return SCORE_SUBSTRING
|
||||
const distance = levenshtein(a, b)
|
||||
const longest = Math.max(a.length, b.length)
|
||||
// 25% character tolerance, with a minimum of 2 edits so short labels
|
||||
// (e.g. "Hip") still admit one typo without exploding into the matcher.
|
||||
const threshold = Math.max(2, Math.floor(longest * 0.25))
|
||||
if (distance > threshold) return SCORE_NO_MATCH
|
||||
return SCORE_FUZZY_BASE + distance
|
||||
}
|
||||
|
||||
export interface BoneLabels {
|
||||
fr: string
|
||||
en: string
|
||||
}
|
||||
|
||||
interface ScoredZone {
|
||||
zone: ZoneFindings
|
||||
score: number
|
||||
}
|
||||
|
||||
function bestOnLanguage(
|
||||
boneLabel: string,
|
||||
zones: ZoneFindings[],
|
||||
pickZoneLabel: (zone: ZoneFindings) => string,
|
||||
): ScoredZone | undefined {
|
||||
let best: ScoredZone | undefined
|
||||
for (const zone of zones) {
|
||||
const score = scorePair(boneLabel, pickZoneLabel(zone))
|
||||
if (score === SCORE_NO_MATCH) continue
|
||||
if (!best || score < best.score) best = { zone, score }
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the findings tree most likely to describe a clicked SVG zone.
|
||||
*
|
||||
* 1. If the English bone label exactly matches (or is contained in) an
|
||||
* English zone label, return that zone — cheapest, most reliable.
|
||||
* 2. Otherwise try the same on the French side.
|
||||
* 3. As a last resort, take the lowest fuzzy score across all four
|
||||
* (bone-lang, zone-lang) pairings.
|
||||
*/
|
||||
export function matchZoneFindings(
|
||||
bone: BoneLabels,
|
||||
zones: ZoneFindings[],
|
||||
): ZoneFindings | undefined {
|
||||
if (zones.length === 0) return undefined
|
||||
|
||||
const enOnEn = bestOnLanguage(bone.en, zones, (z) => z.name_en)
|
||||
if (enOnEn && enOnEn.score <= SCORE_SUBSTRING) return enOnEn.zone
|
||||
|
||||
const frOnFr = bestOnLanguage(bone.fr, zones, (z) => z.name_fr)
|
||||
if (frOnFr && frOnFr.score <= SCORE_SUBSTRING) return frOnFr.zone
|
||||
|
||||
const enOnFr = bestOnLanguage(bone.en, zones, (z) => z.name_fr)
|
||||
const frOnEn = bestOnLanguage(bone.fr, zones, (z) => z.name_en)
|
||||
const candidates = [enOnEn, frOnFr, enOnFr, frOnEn].filter(
|
||||
(x): x is ScoredZone => x !== undefined,
|
||||
)
|
||||
if (candidates.length === 0) return undefined
|
||||
candidates.sort((a, b) => a.score - b.score)
|
||||
return candidates[0].zone
|
||||
}
|
||||
Reference in new issue
Block a user