♻️ Drive the day detail from SignalDB collections

The day detail panel no longer loads entries and the closed day on
demand and patches its lists after each mutation. Entries, the closed
day, the running entry and the total are derived from the synced
collections, so they update on local changes and on sync. The ticker
restarts whenever the running entry changes. Closing, marking and
reopening a day rely on the sync the domain functions trigger.
This commit is contained in:
2026-11-30 17:43:13 +00:00
parent a47affa765
commit f0cf796ef6
+51 -85
View File
@@ -1,29 +1,39 @@
<script lang="ts"> <script lang="ts">
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
import { entries, days, type Entry, type ClosedDay, ApiError } from '$lib/api/client';
import { triggerSync } from '$lib/stores/sync';
import { import {
todayKey, formatTime, formatDuration, formatDurationShort, closedDay as findClosedDay,
parseTimeInput, toTimeInput, type DayCapabilities closeDay,
createInterval,
deleteEntry,
entriesForDay,
markDay,
reopenDay,
startEntry,
stopEntry,
todayKey,
updateEntry
} from '$lib/app.svelte';
import type { Doc } from '$lib/db.svelte';
import { errorMessage } from '$lib/pb';
import type { Entry } from '$lib/types';
import {
formatTime, formatDuration, formatDurationShort,
parseTimeInput, toTimeInput, dayCapabilities
} from '$lib/utils'; } from '$lib/utils';
interface Props { interface Props {
dayKey: string; dayKey: string;
capabilities: DayCapabilities;
/** Called after any mutation that changes day or entry state.
* closedDay: the new closed-day value (null = reopened, ClosedDay = just closed). */
oninvalidate?: (closedDay?: ClosedDay | null) => void;
} }
let { dayKey, capabilities, oninvalidate }: Props = $props(); let { dayKey }: Props = $props();
let entryList = $state<Entry[]>([]); let entryList = $derived(entriesForDay(dayKey));
let closedDay = $state<ClosedDay | null>(null); let closedDay = $derived(findClosedDay(dayKey));
let running = $state<Entry | null>(null); let running = $derived(entryList.find((e) => e.end_time === null) ?? null);
let capabilities = $derived(dayCapabilities(dayKey, todayKey(), !!closedDay));
let elapsed = $state(0); let elapsed = $state(0);
let note = $state(''); let note = $state('');
let error = $state(''); let error = $state('');
let loading = $state(false);
// Add-interval form // Add-interval form
let showAddForm = $state(false); let showAddForm = $state(false);
@@ -41,44 +51,27 @@
let timer: ReturnType<typeof setInterval> | null = null; let timer: ReturnType<typeof setInterval> | null = null;
async function load() { // Only tick if this is today and a running entry exists.
loading = true; $effect(() => {
error = ''; const current = running;
try {
const [es, ds] = await Promise.all([
entries.list(dayKey, dayKey),
days.list(dayKey, dayKey)
]);
entryList = es ?? [];
closedDay = (ds ?? []).find((d) => d.day_key === dayKey) ?? null;
running = entryList.find((e) => e.end_time === null) ?? null;
startTimer();
} catch (e) {
error = e instanceof ApiError ? e.message : String(e);
} finally {
loading = false;
}
}
function startTimer() {
if (timer) clearInterval(timer); if (timer) clearInterval(timer);
// Only tick if this is today and a running entry exists. timer = null;
if (running && dayKey === todayKey()) { if (current && dayKey === todayKey()) {
elapsed = Date.now() - current.start_time;
timer = setInterval(() => { timer = setInterval(() => {
elapsed = Date.now() - running!.start_time; elapsed = Date.now() - current.start_time;
}, 500); }, 500);
elapsed = Date.now() - running.start_time;
} else { } else {
elapsed = 0; elapsed = 0;
} }
} });
// Reload when dayKey changes. // Reset the editing state when dayKey changes.
$effect(() => { $effect(() => {
dayKey; // dependency dayKey; // dependency
editingId = null; editingId = null;
showAddForm = false; showAddForm = false;
load(); error = '';
}); });
onDestroy(() => { if (timer) clearInterval(timer); }); onDestroy(() => { if (timer) clearInterval(timer); });
@@ -86,14 +79,10 @@
async function handleStart() { async function handleStart() {
error = ''; error = '';
try { try {
const e = await entries.start(note); await startEntry(note);
running = e;
entryList = [...entryList, e];
note = ''; note = '';
startTimer();
oninvalidate?.();
} catch (e) { } catch (e) {
error = e instanceof ApiError ? e.message : String(e); error = errorMessage(e);
} }
} }
@@ -101,61 +90,45 @@
if (!running) return; if (!running) return;
error = ''; error = '';
try { try {
const e = await entries.stop(running.id); await stopEntry(running.id);
entryList = entryList.map((x) => (x.id === e.id ? e : x));
running = null;
if (timer) clearInterval(timer);
elapsed = 0;
oninvalidate?.();
} catch (e) { } catch (e) {
error = e instanceof ApiError ? e.message : String(e); error = errorMessage(e);
} }
} }
async function handleCloseDay() { async function handleCloseDay() {
error = ''; error = '';
try { try {
const cd = await days.close(dayKey); await closeDay(dayKey);
closedDay = cd;
oninvalidate?.(cd);
triggerSync();
} catch (e) { } catch (e) {
error = e instanceof ApiError ? e.message : String(e); error = errorMessage(e);
} }
} }
async function handleMark(kind: 'holiday' | 'vacation' | 'sick') { async function handleMark(kind: 'holiday' | 'vacation' | 'sick') {
error = ''; error = '';
try { try {
const cd = await days.mark(dayKey, kind); await markDay(dayKey, kind);
closedDay = cd;
oninvalidate?.(cd);
triggerSync();
} catch (e) { } catch (e) {
error = e instanceof ApiError ? e.message : String(e); error = errorMessage(e);
} }
} }
async function handleReopenDay() { async function handleReopenDay() {
error = ''; error = '';
try { try {
await days.reopen(dayKey); await reopenDay(dayKey);
closedDay = null;
oninvalidate?.(null);
triggerSync();
} catch (e) { } catch (e) {
error = e instanceof ApiError ? e.message : String(e); error = errorMessage(e);
} }
} }
async function handleDeleteEntry(id: string) { async function handleDeleteEntry(id: string) {
error = ''; error = '';
try { try {
await entries.delete(id); await deleteEntry(id);
entryList = entryList.filter((e) => e.id !== id);
oninvalidate?.();
} catch (e) { } catch (e) {
error = e instanceof ApiError ? e.message : String(e); error = errorMessage(e);
} }
} }
@@ -182,18 +155,16 @@
if (endMs === null) { addError = 'Invalid end time'; return; } if (endMs === null) { addError = 'Invalid end time'; return; }
if (endMs <= startMs) { addError = 'End must be after start'; return; } if (endMs <= startMs) { addError = 'End must be after start'; return; }
try { try {
const e = await entries.createInterval(startMs, endMs, addNote); await createInterval(startMs, endMs, addNote);
entryList = [...entryList, e].sort((a, b) => a.start_time - b.start_time);
closeAddForm(); closeAddForm();
oninvalidate?.();
} catch (e) { } catch (e) {
addError = e instanceof ApiError ? e.message : String(e); addError = errorMessage(e);
} }
} }
// ── Inline edit ───────────────────────────────────────────────────────────── // ── Inline edit ─────────────────────────────────────────────────────────────
function startEdit(entry: Entry) { function startEdit(entry: Doc<Entry>) {
editingId = entry.id; editingId = entry.id;
editStart = toTimeInput(entry.start_time); editStart = toTimeInput(entry.start_time);
editEnd = entry.end_time ? toTimeInput(entry.end_time) : ''; editEnd = entry.end_time ? toTimeInput(entry.end_time) : '';
@@ -206,7 +177,7 @@
editError = ''; editError = '';
} }
async function saveEdit(entry: Entry) { async function saveEdit(entry: Doc<Entry>) {
editError = ''; editError = '';
const startMs = parseTimeInput(dayKey, editStart); const startMs = parseTimeInput(dayKey, editStart);
if (startMs === null) { editError = 'Invalid start time'; return; } if (startMs === null) { editError = 'Invalid start time'; return; }
@@ -222,19 +193,14 @@
} }
try { try {
const updated = await entries.update(entry.id, { await updateEntry(entry.id, {
start_time: startMs, start_time: startMs,
...(endMs !== undefined ? { end_time: endMs } : {}), ...(endMs !== undefined ? { end_time: endMs } : {}),
note: editNote note: editNote
}); });
entryList = entryList
.map((e) => (e.id === updated.id ? updated : e))
.sort((a, b) => a.start_time - b.start_time);
if (running?.id === updated.id) running = updated;
editingId = null; editingId = null;
oninvalidate?.();
} catch (e) { } catch (e) {
editError = e instanceof ApiError ? e.message : String(e); editError = errorMessage(e);
} }
} }