feat: offline-first client

All reads now come directly from Dexie; all mutations write to Dexie +
outbox immediately without waiting for the server. The background sync
loop (every 30s) pushes the outbox and pulls server changes.

Day/week close and reopen remain server-only (require server-side
computation). triggerSync() is called after them to update Dexie
promptly. The optimistic closedDaysMap update in the week page is kept
separate from the Dexie reload to avoid a race that was causing the
reopen button and day actions to disappear until a page reload.

- client.ts: remove online-first fetch paths; all reads from Dexie
- sync.ts: add triggerSync() and waitForSync() exports
- DayDetail: pass ClosedDay | null to oninvalidate after close/reopen
- week/+page.svelte: update closedDaysMap optimistically on close/reopen;
  only reload from Dexie on entry mutations
- settings/+page.svelte: read history() directly (never throws 503);
  derive current locally
- layout: remove offline banner and online.ts (behaviour is now
  identical online and offline)
This commit is contained in:
2026-05-01 16:35:02 +02:00
parent f4836a6fa2
commit 57697ec2aa
8 changed files with 205 additions and 266 deletions
-14
View File
@@ -4,7 +4,6 @@
import { onMount, onDestroy } from 'svelte';
import { goto } from '$app/navigation';
import { startSync, stopSync } from '$lib/stores/sync';
import { isOnline } from '$lib/stores/online';
import { todayKey, currentWeekKey } from '$lib/utils';
let { children } = $props();
@@ -40,10 +39,6 @@
<a href="/settings" class:active={page.url.pathname === '/settings'}>Settings</a>
</nav>
{#if !$isOnline}
<div class="offline-banner" role="status">Offline — showing cached data</div>
{/if}
<main>
{@render children()}
</main>
@@ -88,15 +83,6 @@
background: #0f3460;
color: #e9ecef;
}
.offline-banner {
background: #856404;
color: #fff3cd;
text-align: center;
padding: 0.35rem 1rem;
font-size: 0.8rem;
font-weight: 500;
letter-spacing: 0.02em;
}
main {
max-width: 800px;
margin: 0 auto;
+7 -6
View File
@@ -53,13 +53,14 @@
if (!hasToken()) return;
error = '';
try {
const [c, h] = await Promise.all([settings.current(), settings.history()]);
current = c;
const h = await settings.history();
history = h ?? [];
if (c) {
formHoursPerWeek = c.hours_per_week;
formWorkdaysMask = c.workdays_mask;
formTimezone = c.timezone;
const today = new Date().toISOString().slice(0, 10);
current = history.find((s) => s.effective_from <= today) ?? history[0] ?? null;
if (current) {
formHoursPerWeek = current.hours_per_week;
formWorkdaysMask = current.workdays_mask;
formTimezone = current.timezone;
}
} catch (e) {
error = e instanceof ApiError ? e.message : String(e);
+32 -4
View File
@@ -2,6 +2,7 @@
import { page } from '$app/stores';
import { goto } from '$app/navigation';
import { entries, days, weeks, settings, type Entry, type ClosedDay, type ClosedWeek, type Settings, ApiError } from '$lib/api/client';
import { triggerSync } from '$lib/stores/sync';
import {
currentWeekKey, weekDayKeys, formatDurationShort, formatDelta,
todayKey, isWorkday, dayCapabilities
@@ -80,19 +81,25 @@
const from = dayKeys[0];
const to = dayKeys[6];
try {
const [ds, ws, s, es] = await Promise.all([
const [ds, ws, es] = await Promise.all([
days.list(from, to),
weeks.list(weekKey, weekKey),
settings.current(),
entries.list(from, to)
]);
closedDaysMap = Object.fromEntries((ds ?? []).map((d) => [d.day_key, d]));
closedWeek = (ws ?? []).find((w) => w.week_key === weekKey) ?? null;
currentSettings = s;
weekEntries = es ?? [];
} catch (e) {
error = e instanceof ApiError ? e.message : String(e);
}
// Load settings independently so a missing settings row doesn't break the rest.
try {
currentSettings = await settings.current();
} catch (e) {
if (!(e instanceof ApiError && e.status === 503)) {
error = e instanceof ApiError ? e.message : String(e);
}
}
}
$effect(() => { weekKey; load(); });
@@ -117,6 +124,7 @@
try {
const cw = await weeks.close(weekKey);
closedWeek = cw;
triggerSync();
} catch (e) {
error = e instanceof ApiError ? e.message : String(e);
}
@@ -127,6 +135,7 @@
try {
await weeks.reopen(weekKey);
closedWeek = null;
triggerSync();
} catch (e) {
error = e instanceof ApiError ? e.message : String(e);
}
@@ -246,7 +255,26 @@
<!-- Day detail panel -->
<div class="day-detail-panel" role="tabpanel">
<h2 class="detail-heading">{selectedDay}</h2>
<DayDetail dayKey={selectedDay} capabilities={detailCaps} oninvalidate={load} />
<DayDetail
dayKey={selectedDay}
capabilities={detailCaps}
oninvalidate={(cd) => {
if (cd !== undefined) {
// Day was closed or reopened — update map immediately so detailCaps reacts.
// Do NOT call load() here; Dexie doesn't have the server result yet and
// would overwrite the optimistic update.
if (cd === null) {
const { [selectedDay]: _, ...rest } = closedDaysMap;
closedDaysMap = rest;
} else {
closedDaysMap = { ...closedDaysMap, [selectedDay]: cd };
}
} else {
// Entry mutation — reload entries/days from Dexie.
load();
}
}}
/>
</div>
</div>