♻️ 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:
@@ -1,11 +1,18 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import {
|
import {
|
||||||
days, weeks, balance as balanceApi,
|
adjustments as allAdjustments,
|
||||||
type ClosedDay, type ClosedWeek, type BalanceAdjustment, type BalanceSummary,
|
addAdjustment,
|
||||||
ApiError
|
balance,
|
||||||
} from '$lib/api/client';
|
closedDaysInRange,
|
||||||
import { formatDurationShort, formatDelta, composeDeltaMs, decomposeDeltaMs, todayKey } from '$lib/utils';
|
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
|
// Show last 12 weeks
|
||||||
function pastWeekKeys(n: number): string[] {
|
function pastWeekKeys(n: number): string[] {
|
||||||
@@ -24,12 +31,17 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let weekKeys = pastWeekKeys(12);
|
let weekKeys = pastWeekKeys(12);
|
||||||
let closedWeeksMap: Record<string, ClosedWeek> = $state({});
|
const closedWeeksMap = $derived(
|
||||||
let closedDaysMap: Record<string, ClosedDay> = $state({});
|
Object.fromEntries(
|
||||||
let summary: BalanceSummary | null = $state(null);
|
closedWeeksInRange(weekKeys[0], weekKeys[weekKeys.length - 1]).map((w) => [w.week_key, w])
|
||||||
let adjustments: BalanceAdjustment[] = $state([]);
|
)
|
||||||
|
);
|
||||||
|
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 error = $state('');
|
||||||
let loading = $state(false);
|
|
||||||
|
|
||||||
// ── Add form state ────────────────────────────────────────────────────────
|
// ── Add form state ────────────────────────────────────────────────────────
|
||||||
let showAddForm = $state(false);
|
let showAddForm = $state(false);
|
||||||
@@ -51,31 +63,6 @@
|
|||||||
let editError = $state('');
|
let editError = $state('');
|
||||||
let editSaving = $state(false);
|
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 ───────────────────────────────────────────────────────────────
|
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function msToDateInput(ms: number): string {
|
function msToDateInput(ms: number): string {
|
||||||
@@ -109,15 +96,14 @@
|
|||||||
if (deltaMs === 0) { addError = 'Delta must be non-zero.'; return; }
|
if (deltaMs === 0) { addError = 'Delta must be non-zero.'; return; }
|
||||||
addSaving = true;
|
addSaving = true;
|
||||||
try {
|
try {
|
||||||
await balanceApi.create({
|
await addAdjustment({
|
||||||
delta_ms: deltaMs,
|
delta_ms: deltaMs,
|
||||||
note: addNote,
|
note: addNote,
|
||||||
effective_at: dateInputToMs(addEffectiveAt)
|
effective_at: dateInputToMs(addEffectiveAt)
|
||||||
});
|
});
|
||||||
showAddForm = false;
|
showAddForm = false;
|
||||||
await load();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
addError = e instanceof ApiError ? e.message : String(e);
|
addError = errorMessage(e);
|
||||||
} finally {
|
} finally {
|
||||||
addSaving = false;
|
addSaving = false;
|
||||||
}
|
}
|
||||||
@@ -125,7 +111,7 @@
|
|||||||
|
|
||||||
// ── Edit ──────────────────────────────────────────────────────────────────
|
// ── Edit ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function startEdit(a: BalanceAdjustment) {
|
function startEdit(a: Doc<BalanceAdjustment>) {
|
||||||
const { sign, hours, minutes } = decomposeDeltaMs(a.delta_ms);
|
const { sign, hours, minutes } = decomposeDeltaMs(a.delta_ms);
|
||||||
editingId = a.id;
|
editingId = a.id;
|
||||||
editSign = sign;
|
editSign = sign;
|
||||||
@@ -147,15 +133,14 @@
|
|||||||
if (deltaMs === 0) { editError = 'Delta must be non-zero.'; return; }
|
if (deltaMs === 0) { editError = 'Delta must be non-zero.'; return; }
|
||||||
editSaving = true;
|
editSaving = true;
|
||||||
try {
|
try {
|
||||||
await balanceApi.update(id, {
|
await updateAdjustment(id, {
|
||||||
delta_ms: deltaMs,
|
delta_ms: deltaMs,
|
||||||
note: editNote,
|
note: editNote,
|
||||||
effective_at: dateInputToMs(editEffectiveAt)
|
effective_at: dateInputToMs(editEffectiveAt)
|
||||||
});
|
});
|
||||||
editingId = null;
|
editingId = null;
|
||||||
await load();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
editError = e instanceof ApiError ? e.message : String(e);
|
editError = errorMessage(e);
|
||||||
} finally {
|
} finally {
|
||||||
editSaving = false;
|
editSaving = false;
|
||||||
}
|
}
|
||||||
@@ -167,10 +152,9 @@
|
|||||||
if (!confirm('Delete this adjustment?')) return;
|
if (!confirm('Delete this adjustment?')) return;
|
||||||
error = '';
|
error = '';
|
||||||
try {
|
try {
|
||||||
await balanceApi.delete(id);
|
await deleteAdjustment(id);
|
||||||
await load();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e instanceof ApiError ? e.message : String(e);
|
error = errorMessage(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -178,25 +162,20 @@
|
|||||||
<div class="history">
|
<div class="history">
|
||||||
<h1>History</h1>
|
<h1>History</h1>
|
||||||
{#if error}<p class="error">{error}</p>{/if}
|
{#if error}<p class="error">{error}</p>{/if}
|
||||||
{#if loading}<p>Loading…</p>{/if}
|
|
||||||
|
|
||||||
<!-- Balance card -->
|
<!-- Balance card -->
|
||||||
<div class="balance-card">
|
<div class="balance-card">
|
||||||
<span class="balance-label">Overall balance</span>
|
<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}>
|
||||||
<span class="balance-value" class:positive={summary.total_delta_ms > 0} class:negative={summary.total_delta_ms < 0}>
|
{formatDelta(summary.total_delta_ms)}
|
||||||
{formatDelta(summary.total_delta_ms)}
|
</span>
|
||||||
</span>
|
<span class="balance-meta">
|
||||||
<span class="balance-meta">
|
across {summary.closed_week_count} closed {summary.closed_week_count === 1 ? 'week' : 'weeks'}
|
||||||
across {summary.closed_week_count} closed {summary.closed_week_count === 1 ? 'week' : 'weeks'}
|
{#if summary.adjustment_count > 0}
|
||||||
{#if summary.adjustment_count > 0}
|
· {summary.adjustment_count} {summary.adjustment_count === 1 ? 'adjustment' : 'adjustments'}
|
||||||
· {summary.adjustment_count} {summary.adjustment_count === 1 ? 'adjustment' : 'adjustments'}
|
(weeks {formatDelta(summary.weeks_delta_ms)}, adj {formatDelta(summary.adjustments_delta_ms)})
|
||||||
(weeks {formatDelta(summary.weeks_delta_ms)}, adj {formatDelta(summary.adjustments_delta_ms)})
|
{/if}
|
||||||
{/if}
|
</span>
|
||||||
</span>
|
|
||||||
{:else}
|
|
||||||
<span class="balance-value">—</span>
|
|
||||||
{/if}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Closed weeks table -->
|
<!-- Closed weeks table -->
|
||||||
|
|||||||
Reference in New Issue
Block a user