feat(app): export and import through @faunosteo/pdf — snapshot, archived branding and labels, browser rasterizer, imported archive kept in session, explicit rebase with messages; legacy PDF modules removed

This commit is contained in:
bchanot committed 2026-10-11 13:32:14 +02:00
1 parent 20dafc0b00
commit 9bd5173a9c
43 files changed
+1492 -1989

No files matched your search

+46 -7
View File
@@ -1,13 +1,18 @@
import {
readReportDocument,
ReportArchiveEnvelopeSchema,
type Report,
type ReportArchiveEnvelope,
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.
* Where the session lives between renders. The report draft is mirrored on
* every change; the archive an import brought along (the envelope of a
* Step 6 PDF) is written only when the session starts or ends, so a stale
* draft can still be rebased with the labels of its original sheet. One
* implementation today (per-tab sessionStorage); the Supabase and outbox
* adapters of later steps implement the same interfaces.
*/
export interface ReportPersistence {
@@ -15,12 +20,33 @@ export interface ReportPersistence {
save(report: Report | null): void
}
export interface ArchivePersistence {
load(): ReportArchiveEnvelope | null
save(archive: ReportArchiveEnvelope | null): void
}
export const DRAFT_KEY = 'osteo:draft'
export const ARCHIVE_KEY = 'osteo:archive'
function storage(): Storage | null {
return typeof sessionStorage === 'undefined' ? null : sessionStorage
}
function writeOrRemove(key: string, value: unknown): void {
const store = storage()
if (!store) return
if (value === null) {
store.removeItem(key)
return
}
try {
store.setItem(key, JSON.stringify(value))
} catch (err) {
// Quota exceeded: the session goes on, only the mirror is lost.
if (import.meta.env.DEV) console.warn(`${key} not saved`, err)
}
}
export const sessionStoragePersistence: ReportPersistence = {
load(map) {
const raw = storage()?.getItem(DRAFT_KEY)
@@ -33,9 +59,22 @@ export const sessionStoragePersistence: ReportPersistence = {
}
},
save(report) {
const store = storage()
if (!store) return
if (report) store.setItem(DRAFT_KEY, JSON.stringify(report))
else store.removeItem(DRAFT_KEY)
writeOrRemove(DRAFT_KEY, report)
},
}
export const sessionStorageArchivePersistence: ArchivePersistence = {
load() {
const raw = storage()?.getItem(ARCHIVE_KEY)
if (!raw) return null
try {
const parsed = ReportArchiveEnvelopeSchema.safeParse(JSON.parse(raw))
return parsed.success ? parsed.data : null
} catch {
return null
}
},
save(archive) {
writeOrRemove(ARCHIVE_KEY, archive)
},
}
+86 -31
View File
@@ -1,19 +1,24 @@
import { create } from 'zustand'
import {
rebaseReportOnSheet,
REPORT_VERSION,
type Animal,
type NextAppointment,
type Observation,
type Owner,
type Practitioner,
type RebaseOutcome,
type Report,
type ReportMigrationMap,
type ReportArchiveEnvelope,
type SheetRef,
} from '@faunosteo/domain'
import { generateId } from '@/lib/id'
import { nowIso } from '@/lib/dates'
import type { SheetIndex } from '@/services/sheetIndex'
import {
sessionStorageArchivePersistence,
sessionStoragePersistence,
type ArchivePersistence,
type ReportPersistence,
} from './persistence'
@@ -21,7 +26,10 @@ import {
* 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.
* and restored once the sheet is known; a draft written on another revision
* of the sheet is rebased on restore. The archive an import brought along
* (STEP6 §14 "App session") is kept beside the report for later operations
* and written only when the session starts or ends.
*/
/** What a new report needs before the store stamps version, id and dates. */
@@ -30,13 +38,26 @@ export type NewReportDraft = Omit<
'version' | 'id' | 'createdAt' | 'sheet' | 'localAnnotations'
>
export interface ImportedSession {
report: Report
archive: ReportArchiveEnvelope | null
}
export interface ReportStore {
report: Report | null
/** The envelope of the imported archive, as exported; null otherwise. */
archive: ReportArchiveEnvelope | null
activeObservationIndex: number
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
/** Opens an imported report (already rebased) with its archive. */
hydrateImport: (session: ImportedSession) => void
/**
* Restores the per-tab draft on the loaded sheet, rebasing it when it was
* written on another revision; a no-op when a report is already open.
* Returns the rebase outcome, null when nothing was restored.
*/
restoreDraft: (index: SheetIndex) => RebaseOutcome | null
addObservation: () => void
removeObservation: (index: number) => void
moveObservationUp: (index: number) => void
@@ -62,49 +83,79 @@ export function emptyObservation(): Observation {
}
}
/** A fresh v3 document: the store stamps version, id, dates and the sheet. */
function newReport(draft: NewReportDraft, sheet: SheetRef): Report {
return {
...draft,
version: REPORT_VERSION,
id: generateId(),
createdAt: nowIso(),
sheet,
observations:
draft.observations.length === 0
? [emptyObservation()]
: draft.observations,
localAnnotations: [],
}
}
function swapped(list: Observation[], i: number, j: number): Observation[] {
const next = [...list]
;[next[i], next[j]] = [next[j]!, next[i]!]
return next
}
type Patch = Partial<Pick<ReportStore, 'report' | 'activeObservationIndex'>>
type Patch = Partial<
Pick<ReportStore, 'report' | 'archive' | 'activeObservationIndex'>
>
type Setter = (fn: (s: ReportStore) => Patch | ReportStore) => void
function onReport(set: Setter, update: (r: Report, s: ReportStore) => Patch) {
set((s) => (s.report ? update(s.report, s) : s))
}
export function createReportStore(persistence: ReportPersistence) {
const store = create<ReportStore>((set) => ({
export function createReportStore(
persistence: ReportPersistence,
archives: ArchivePersistence,
) {
/** The archive mirror is explicit: these actions are the session's edges. */
const withArchive = (patch: Patch, archive: ReportArchiveEnvelope | null) => {
archives.save(archive)
return { ...patch, archive }
}
const store = create<ReportStore>((set, get) => ({
report: null,
archive: null,
activeObservationIndex: 0,
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,
}),
set(
withArchive(
{ report: newReport(draft, sheet), activeObservationIndex: 0 },
null,
),
),
hydrateReport: (report) => set({ report, activeObservationIndex: 0 }),
hydrateReport: (report) =>
set(withArchive({ report, activeObservationIndex: 0 }, null)),
restoreDraft: (map) =>
set((s) => {
if (s.report) return s
const report = persistence.load(map)
return report ? { report, activeObservationIndex: 0 } : s
}),
hydrateImport: ({ report, archive }) =>
set(withArchive({ report, activeObservationIndex: 0 }, archive)),
restoreDraft: (index) => {
if (get().report) return null
const draft = persistence.load(index.sheet.migrationMap)
if (!draft) return null
const archive = archives.load()
const rebase = rebaseReportOnSheet(
draft,
index,
archive?.sheetSnapshot ?? null,
)
set({ report: rebase.report, archive, activeObservationIndex: 0 })
return rebase
},
addObservation: () =>
onReport(set, (r) => {
@@ -177,10 +228,14 @@ export function createReportStore(persistence: ReportPersistence) {
delete rest.nextAppointment
return { report: next ? { ...rest, nextAppointment: next } : rest }
}),
reset: () => set({ report: null, activeObservationIndex: 0 }),
reset: () =>
set(withArchive({ report: null, activeObservationIndex: 0 }, null)),
}))
store.subscribe((state) => persistence.save(state.report))
return store
}
export const useReportStore = createReportStore(sessionStoragePersistence)
export const useReportStore = createReportStore(
sessionStoragePersistence,
sessionStorageArchivePersistence,
)