feat(app): application on the canonical sheet — report v3 store with v2 draft migration, bundled SheetProvider (SHEET_BUNDLE), annotation layer rendered from the document, zone interaction service, pickers and cards on the sheet index, PDF v3 export with v2 archive import; legacy registry, matchers, editors and assets removed
This commit is contained in:
1 parent
0a17e390cf
commit
ce2ec26112
114 files changed
+3560
-14637
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import {
|
||||
readReportDocument,
|
||||
type Report,
|
||||
type ReportMigrationMap,
|
||||
} from '@faunosteo/domain'
|
||||
|
||||
/**
|
||||
* Where the report document lives between renders. One implementation today
|
||||
* (the per-tab sessionStorage draft, migrated from v2 on read); the Supabase
|
||||
* and outbox adapters of later steps implement the same interface.
|
||||
*/
|
||||
|
||||
export interface ReportPersistence {
|
||||
load(map: ReportMigrationMap): Report | null
|
||||
save(report: Report | null): void
|
||||
}
|
||||
|
||||
export const DRAFT_KEY = 'osteo:draft'
|
||||
|
||||
function storage(): Storage | null {
|
||||
return typeof sessionStorage === 'undefined' ? null : sessionStorage
|
||||
}
|
||||
|
||||
export const sessionStoragePersistence: ReportPersistence = {
|
||||
load(map) {
|
||||
const raw = storage()?.getItem(DRAFT_KEY)
|
||||
if (!raw) return null
|
||||
try {
|
||||
return readReportDocument(JSON.parse(raw), map).report
|
||||
} catch (err) {
|
||||
if (import.meta.env.DEV) console.warn('osteo:draft ignored', err)
|
||||
return null
|
||||
}
|
||||
},
|
||||
save(report) {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
if (report) store.setItem(DRAFT_KEY, JSON.stringify(report))
|
||||
else store.removeItem(DRAFT_KEY)
|
||||
},
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
import {
|
||||
PractitionerSchema,
|
||||
type Practitioner,
|
||||
} from '@/domain/schema/practitioner'
|
||||
import { PractitionerSchema, type Practitioner } from '@faunosteo/domain'
|
||||
|
||||
const STORAGE_KEY = 'osteo:lastPractitioner'
|
||||
|
||||
|
||||
+141
-121
@@ -1,16 +1,42 @@
|
||||
import { create } from 'zustand'
|
||||
import { ReportSchema, type Report } from '@/domain/schema/report'
|
||||
import type { Observation } from '@/domain/schema/observation'
|
||||
import type { Practitioner } from '@/domain/schema/practitioner'
|
||||
import type { Animal, Owner } from '@/domain/schema/animal'
|
||||
import {
|
||||
REPORT_VERSION,
|
||||
type Animal,
|
||||
type NextAppointment,
|
||||
type Observation,
|
||||
type Owner,
|
||||
type Practitioner,
|
||||
type Report,
|
||||
type ReportMigrationMap,
|
||||
type SheetRef,
|
||||
} from '@faunosteo/domain'
|
||||
import { generateId } from '@/lib/id'
|
||||
import { nowIso } from '@/lib/dates'
|
||||
import {
|
||||
sessionStoragePersistence,
|
||||
type ReportPersistence,
|
||||
} from './persistence'
|
||||
|
||||
/**
|
||||
* In-memory report document (v3) and its editing actions. Observation-level
|
||||
* zone rules live in services/zoneInteraction; the store only replaces
|
||||
* documents. The per-tab draft is mirrored through the persistence adapter
|
||||
* and restored once the sheet (and its migration map) is known.
|
||||
*/
|
||||
|
||||
/** What a new report needs before the store stamps version, id and dates. */
|
||||
export type NewReportDraft = Omit<
|
||||
Report,
|
||||
'version' | 'id' | 'createdAt' | 'sheet' | 'localAnnotations'
|
||||
>
|
||||
|
||||
export interface ReportStore {
|
||||
report: Report | null
|
||||
activeObservationIndex: number
|
||||
initReport: (draft: Omit<Report, 'id' | 'createdAt' | 'version'>) => void
|
||||
initReport: (draft: NewReportDraft, sheet: SheetRef) => void
|
||||
hydrateReport: (report: Report) => void
|
||||
/** Restores the per-tab draft; a no-op when a report is already open. */
|
||||
restoreDraft: (map: ReportMigrationMap) => void
|
||||
addObservation: () => void
|
||||
removeObservation: (index: number) => void
|
||||
moveObservationUp: (index: number) => void
|
||||
@@ -22,11 +48,11 @@ export interface ReportStore {
|
||||
setOwner: (next: Owner) => void
|
||||
setConclusion: (text: string) => void
|
||||
setRecommendations: (text: string) => void
|
||||
setNextAppointment: (next: Report['nextAppointment']) => void
|
||||
setNextAppointment: (next: NextAppointment | undefined) => void
|
||||
reset: () => void
|
||||
}
|
||||
|
||||
function emptyObservation(): Observation {
|
||||
export function emptyObservation(): Observation {
|
||||
return {
|
||||
id: generateId(),
|
||||
zones: [],
|
||||
@@ -36,131 +62,125 @@ function emptyObservation(): Observation {
|
||||
}
|
||||
}
|
||||
|
||||
const DRAFT_KEY = 'osteo:draft'
|
||||
|
||||
/** Reads the auto-saved draft back from sessionStorage (null if absent/invalid). */
|
||||
export function readPersistedDraft(): Report | null {
|
||||
if (typeof sessionStorage === 'undefined') return null
|
||||
const raw = sessionStorage.getItem(DRAFT_KEY)
|
||||
if (!raw) return null
|
||||
try {
|
||||
const parsed = ReportSchema.safeParse(JSON.parse(raw))
|
||||
return parsed.success ? parsed.data : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
function swapped(list: Observation[], i: number, j: number): Observation[] {
|
||||
const next = [...list]
|
||||
;[next[i], next[j]] = [next[j]!, next[i]!]
|
||||
return next
|
||||
}
|
||||
|
||||
export const useReportStore = create<ReportStore>((set) => ({
|
||||
// Restore the per-tab draft so a page refresh never loses the session.
|
||||
report: readPersistedDraft(),
|
||||
activeObservationIndex: 0,
|
||||
type Patch = Partial<Pick<ReportStore, 'report' | 'activeObservationIndex'>>
|
||||
type Setter = (fn: (s: ReportStore) => Patch | ReportStore) => void
|
||||
|
||||
initReport: (draft) =>
|
||||
set({
|
||||
report: {
|
||||
...draft,
|
||||
version: 2,
|
||||
id: generateId(),
|
||||
createdAt: nowIso(),
|
||||
observations:
|
||||
draft.observations.length === 0
|
||||
? [emptyObservation()]
|
||||
: draft.observations,
|
||||
},
|
||||
activeObservationIndex: 0,
|
||||
}),
|
||||
function onReport(set: Setter, update: (r: Report, s: ReportStore) => Patch) {
|
||||
set((s) => (s.report ? update(s.report, s) : s))
|
||||
}
|
||||
|
||||
hydrateReport: (report) => set({ report, activeObservationIndex: 0 }),
|
||||
export function createReportStore(persistence: ReportPersistence) {
|
||||
const store = create<ReportStore>((set) => ({
|
||||
report: null,
|
||||
activeObservationIndex: 0,
|
||||
|
||||
addObservation: () =>
|
||||
set((s) => {
|
||||
if (!s.report) return s
|
||||
const obs = emptyObservation()
|
||||
const observations = [...s.report.observations, obs]
|
||||
return {
|
||||
report: { ...s.report, observations },
|
||||
activeObservationIndex: observations.length - 1,
|
||||
}
|
||||
}),
|
||||
initReport: (draft, sheet) =>
|
||||
set({
|
||||
report: {
|
||||
...draft,
|
||||
version: REPORT_VERSION,
|
||||
id: generateId(),
|
||||
createdAt: nowIso(),
|
||||
sheet,
|
||||
observations:
|
||||
draft.observations.length === 0
|
||||
? [emptyObservation()]
|
||||
: draft.observations,
|
||||
localAnnotations: [],
|
||||
},
|
||||
activeObservationIndex: 0,
|
||||
}),
|
||||
|
||||
removeObservation: (index) =>
|
||||
set((s) => {
|
||||
if (!s.report) return s
|
||||
const observations = s.report.observations.filter((_, i) => i !== index)
|
||||
const active = Math.min(
|
||||
s.activeObservationIndex,
|
||||
Math.max(0, observations.length - 1),
|
||||
)
|
||||
return {
|
||||
report: { ...s.report, observations },
|
||||
activeObservationIndex: active,
|
||||
}
|
||||
}),
|
||||
hydrateReport: (report) => set({ report, activeObservationIndex: 0 }),
|
||||
|
||||
moveObservationUp: (index) =>
|
||||
set((s) => {
|
||||
if (!s.report || index <= 0) return s
|
||||
const observations = [...s.report.observations]
|
||||
;[observations[index - 1], observations[index]] = [
|
||||
observations[index],
|
||||
observations[index - 1],
|
||||
]
|
||||
return { report: { ...s.report, observations } }
|
||||
}),
|
||||
restoreDraft: (map) =>
|
||||
set((s) => {
|
||||
if (s.report) return s
|
||||
const report = persistence.load(map)
|
||||
return report ? { report, activeObservationIndex: 0 } : s
|
||||
}),
|
||||
|
||||
moveObservationDown: (index) =>
|
||||
set((s) => {
|
||||
if (!s.report || index >= s.report.observations.length - 1) return s
|
||||
const observations = [...s.report.observations]
|
||||
;[observations[index], observations[index + 1]] = [
|
||||
observations[index + 1],
|
||||
observations[index],
|
||||
]
|
||||
return { report: { ...s.report, observations } }
|
||||
}),
|
||||
addObservation: () =>
|
||||
onReport(set, (r) => {
|
||||
const observations = [...r.observations, emptyObservation()]
|
||||
return {
|
||||
report: { ...r, observations },
|
||||
activeObservationIndex: observations.length - 1,
|
||||
}
|
||||
}),
|
||||
|
||||
updateObservation: (index, obs) =>
|
||||
set((s) => {
|
||||
if (!s.report) return s
|
||||
const observations = s.report.observations.map((o, i) =>
|
||||
i === index ? obs : o,
|
||||
)
|
||||
return { report: { ...s.report, observations } }
|
||||
}),
|
||||
removeObservation: (index) =>
|
||||
onReport(set, (r, s) => {
|
||||
const observations = r.observations.filter((_, i) => i !== index)
|
||||
const active = Math.min(
|
||||
s.activeObservationIndex,
|
||||
Math.max(0, observations.length - 1),
|
||||
)
|
||||
return {
|
||||
report: { ...r, observations },
|
||||
activeObservationIndex: active,
|
||||
}
|
||||
}),
|
||||
|
||||
setActiveObservation: (index) => set({ activeObservationIndex: index }),
|
||||
moveObservationUp: (index) =>
|
||||
onReport(set, (r) =>
|
||||
index <= 0
|
||||
? {}
|
||||
: {
|
||||
report: {
|
||||
...r,
|
||||
observations: swapped(r.observations, index - 1, index),
|
||||
},
|
||||
},
|
||||
),
|
||||
|
||||
setPractitioner: (next) =>
|
||||
set((s) => (s.report ? { report: { ...s.report, practitioner: next } } : s)),
|
||||
moveObservationDown: (index) =>
|
||||
onReport(set, (r) =>
|
||||
index >= r.observations.length - 1
|
||||
? {}
|
||||
: {
|
||||
report: {
|
||||
...r,
|
||||
observations: swapped(r.observations, index, index + 1),
|
||||
},
|
||||
},
|
||||
),
|
||||
|
||||
setAnimal: (next) =>
|
||||
set((s) => (s.report ? { report: { ...s.report, animal: next } } : s)),
|
||||
updateObservation: (index, obs) =>
|
||||
onReport(set, (r) => ({
|
||||
report: {
|
||||
...r,
|
||||
observations: r.observations.map((o, i) => (i === index ? obs : o)),
|
||||
},
|
||||
})),
|
||||
|
||||
setOwner: (next) =>
|
||||
set((s) => (s.report ? { report: { ...s.report, owner: next } } : s)),
|
||||
setActiveObservation: (index) => set({ activeObservationIndex: index }),
|
||||
setPractitioner: (next) =>
|
||||
onReport(set, (r) => ({ report: { ...r, practitioner: next } })),
|
||||
setAnimal: (next) =>
|
||||
onReport(set, (r) => ({ report: { ...r, animal: next } })),
|
||||
setOwner: (next) =>
|
||||
onReport(set, (r) => ({ report: { ...r, owner: next } })),
|
||||
setConclusion: (text) =>
|
||||
onReport(set, (r) => ({ report: { ...r, conclusion: text } })),
|
||||
setRecommendations: (text) =>
|
||||
onReport(set, (r) => ({ report: { ...r, recommendations: text } })),
|
||||
setNextAppointment: (next) =>
|
||||
onReport(set, (r) => {
|
||||
const rest = { ...r }
|
||||
delete rest.nextAppointment
|
||||
return { report: next ? { ...rest, nextAppointment: next } : rest }
|
||||
}),
|
||||
reset: () => set({ report: null, activeObservationIndex: 0 }),
|
||||
}))
|
||||
store.subscribe((state) => persistence.save(state.report))
|
||||
return store
|
||||
}
|
||||
|
||||
setConclusion: (text) =>
|
||||
set((s) => (s.report ? { report: { ...s.report, conclusion: text } } : s)),
|
||||
|
||||
setRecommendations: (text) =>
|
||||
set((s) =>
|
||||
s.report ? { report: { ...s.report, recommendations: text } } : s,
|
||||
),
|
||||
|
||||
setNextAppointment: (next) =>
|
||||
set((s) =>
|
||||
s.report ? { report: { ...s.report, nextAppointment: next } } : s,
|
||||
),
|
||||
|
||||
reset: () => set({ report: null, activeObservationIndex: 0 }),
|
||||
}))
|
||||
|
||||
useReportStore.subscribe((state) => {
|
||||
if (typeof sessionStorage === 'undefined') return
|
||||
if (state.report) {
|
||||
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(state.report))
|
||||
} else {
|
||||
sessionStorage.removeItem(DRAFT_KEY)
|
||||
}
|
||||
})
|
||||
export const useReportStore = createReportStore(sessionStoragePersistence)
|
||||
Reference in new issue
Block a user