♻️ Read history and balance from SignalDB

Closed weeks, closed days, the balance and the adjustments are derived
from the synced collections instead of being loaded once on mount, so
every change shows up without a reload.
This commit is contained in:
2026-11-30 17:43:13 +00:00
parent f0cf796ef6
commit cd39c7457e
+40 -61
View File
@@ -1,11 +1,18 @@
<script lang="ts">
import { onMount } from 'svelte';
import {
days, weeks, balance as balanceApi,
type ClosedDay, type ClosedWeek, type BalanceAdjustment, type BalanceSummary,
ApiError
} from '$lib/api/client';
import { formatDurationShort, formatDelta, composeDeltaMs, decomposeDeltaMs, todayKey } from '$lib/utils';
adjustments as allAdjustments,
addAdjustment,
balance,
closedDaysInRange,
closedWeeksInRange,
deleteAdjustment,
todayKey,
updateAdjustment
} from '$lib/app.svelte';
import type { Doc } from '$lib/db.svelte';
import { errorMessage } from '$lib/pb';
import type { BalanceAdjustment } from '$lib/types';
import { composeDeltaMs, decomposeDeltaMs, formatDelta, formatDurationShort } from '$lib/utils';
// Show last 12 weeks
function pastWeekKeys(n: number): string[] {
@@ -24,12 +31,17 @@
}
let weekKeys = pastWeekKeys(12);
let closedWeeksMap: Record<string, ClosedWeek> = $state({});
let closedDaysMap: Record<string, ClosedDay> = $state({});
let summary: BalanceSummary | null = $state(null);
let adjustments: BalanceAdjustment[] = $state([]);
const closedWeeksMap = $derived(
Object.fromEntries(
closedWeeksInRange(weekKeys[0], weekKeys[weekKeys.length - 1]).map((w) => [w.week_key, w])
)
);
const closedDaysMap = $derived(
Object.fromEntries(closedDaysInRange('2000-01-01', '2100-01-01').map((d) => [d.day_key, d]))
);
const summary = $derived(balance());
const adjustments = $derived(allAdjustments());
let error = $state('');
let loading = $state(false);
// ── Add form state ────────────────────────────────────────────────────────
let showAddForm = $state(false);
@@ -51,31 +63,6 @@
let editError = $state('');
let editSaving = $state(false);
async function load() {
loading = true;
error = '';
try {
const from = weekKeys[0];
const to = weekKeys[weekKeys.length - 1];
const [ws, ds, sum, adjs] = await Promise.all([
weeks.list(from, to),
days.list('2000-01-01', '2100-01-01'),
weeks.balance(),
balanceApi.list()
]);
closedWeeksMap = Object.fromEntries((ws ?? []).map((w) => [w.week_key, w]));
closedDaysMap = Object.fromEntries((ds ?? []).map((d) => [d.day_key, d]));
summary = sum;
adjustments = adjs ?? [];
} catch (e) {
error = e instanceof ApiError ? e.message : String(e);
} finally {
loading = false;
}
}
onMount(load);
// ── Helpers ───────────────────────────────────────────────────────────────
function msToDateInput(ms: number): string {
@@ -109,15 +96,14 @@
if (deltaMs === 0) { addError = 'Delta must be non-zero.'; return; }
addSaving = true;
try {
await balanceApi.create({
await addAdjustment({
delta_ms: deltaMs,
note: addNote,
effective_at: dateInputToMs(addEffectiveAt)
});
showAddForm = false;
await load();
} catch (e) {
addError = e instanceof ApiError ? e.message : String(e);
addError = errorMessage(e);
} finally {
addSaving = false;
}
@@ -125,7 +111,7 @@
// ── Edit ──────────────────────────────────────────────────────────────────
function startEdit(a: BalanceAdjustment) {
function startEdit(a: Doc<BalanceAdjustment>) {
const { sign, hours, minutes } = decomposeDeltaMs(a.delta_ms);
editingId = a.id;
editSign = sign;
@@ -147,15 +133,14 @@
if (deltaMs === 0) { editError = 'Delta must be non-zero.'; return; }
editSaving = true;
try {
await balanceApi.update(id, {
await updateAdjustment(id, {
delta_ms: deltaMs,
note: editNote,
effective_at: dateInputToMs(editEffectiveAt)
});
editingId = null;
await load();
} catch (e) {
editError = e instanceof ApiError ? e.message : String(e);
editError = errorMessage(e);
} finally {
editSaving = false;
}
@@ -167,10 +152,9 @@
if (!confirm('Delete this adjustment?')) return;
error = '';
try {
await balanceApi.delete(id);
await load();
await deleteAdjustment(id);
} catch (e) {
error = e instanceof ApiError ? e.message : String(e);
error = errorMessage(e);
}
}
</script>
@@ -178,25 +162,20 @@
<div class="history">
<h1>History</h1>
{#if error}<p class="error">{error}</p>{/if}
{#if loading}<p>Loading…</p>{/if}
<!-- Balance card -->
<div class="balance-card">
<span class="balance-label">Overall balance</span>
{#if summary !== null}
<span class="balance-value" class:positive={summary.total_delta_ms > 0} class:negative={summary.total_delta_ms < 0}>
{formatDelta(summary.total_delta_ms)}
</span>
<span class="balance-meta">
across {summary.closed_week_count} closed {summary.closed_week_count === 1 ? 'week' : 'weeks'}
{#if summary.adjustment_count > 0}
· {summary.adjustment_count} {summary.adjustment_count === 1 ? 'adjustment' : 'adjustments'}
(weeks {formatDelta(summary.weeks_delta_ms)}, adj {formatDelta(summary.adjustments_delta_ms)})
{/if}
</span>
{:else}
<span class="balance-value">—</span>
{/if}
<span class="balance-value" class:positive={summary.total_delta_ms > 0} class:negative={summary.total_delta_ms < 0}>
{formatDelta(summary.total_delta_ms)}
</span>
<span class="balance-meta">
across {summary.closed_week_count} closed {summary.closed_week_count === 1 ? 'week' : 'weeks'}
{#if summary.adjustment_count > 0}
· {summary.adjustment_count} {summary.adjustment_count === 1 ? 'adjustment' : 'adjustments'}
(weeks {formatDelta(summary.weeks_delta_ms)}, adj {formatDelta(summary.adjustments_delta_ms)})
{/if}
</span>
</div>
<!-- Closed weeks table -->