Files
faunosteo/apps/app/src/state/persistence.ts
T

42 lines
1.1 KiB
TypeScript

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)
},
}