// State, den der Service-Worker per postMessage befüllt. Die App // spiegelt den Sync-Fortschritt im SyncIndicator. export type SyncState = | { kind: 'idle' } | { kind: 'syncing'; current: number; total: number } | { kind: 'error'; message: string }; export type SWMessage = | { type: 'sync-start'; total: number } | { type: 'sync-progress'; current: number; total: number } | { type: 'sync-done'; lastSynced: number } | { type: 'sync-error'; message: string }; const STORAGE_KEY = 'kochwas.sw.lastSynced'; function loadLastSynced(): number | null { if (typeof localStorage === 'undefined') return null; const raw = localStorage.getItem(STORAGE_KEY); if (!raw) return null; const n = Number(raw); return Number.isFinite(n) ? n : null; } function saveLastSynced(ts: number): void { if (typeof localStorage === 'undefined') return; localStorage.setItem(STORAGE_KEY, String(ts)); } class SyncStatusStore { state = $state({ kind: 'idle' }); lastSynced = $state(loadLastSynced()); handle(msg: SWMessage): void { switch (msg.type) { case 'sync-start': this.state = { kind: 'syncing', current: 0, total: msg.total }; break; case 'sync-progress': this.state = { kind: 'syncing', current: msg.current, total: msg.total }; break; case 'sync-done': this.state = { kind: 'idle' }; this.lastSynced = msg.lastSynced; saveLastSynced(msg.lastSynced); break; case 'sync-error': this.state = { kind: 'error', message: msg.message }; break; } } } export const syncStatus = new SyncStatusStore();