♻️ 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:
@@ -1,29 +1,39 @@
|
||||
<script lang="ts">
|
||||
import { onDestroy } from 'svelte';
|
||||
import { entries, days, type Entry, type ClosedDay, ApiError } from '$lib/api/client';
|
||||
import { triggerSync } from '$lib/stores/sync';
|
||||
import {
|
||||
todayKey, formatTime, formatDuration, formatDurationShort,
|
||||
parseTimeInput, toTimeInput, type DayCapabilities
|
||||
closedDay as findClosedDay,
|
||||
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';
|
||||
|
||||
interface Props {
|
||||
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 closedDay = $state<ClosedDay | null>(null);
|
||||
let running = $state<Entry | null>(null);
|
||||
let entryList = $derived(entriesForDay(dayKey));
|
||||
let closedDay = $derived(findClosedDay(dayKey));
|
||||
let running = $derived(entryList.find((e) => e.end_time === null) ?? null);
|
||||
let capabilities = $derived(dayCapabilities(dayKey, todayKey(), !!closedDay));
|
||||
let elapsed = $state(0);
|
||||
let note = $state('');
|
||||
let error = $state('');
|
||||
let loading = $state(false);
|
||||
|
||||
// Add-interval form
|
||||
let showAddForm = $state(false);
|
||||
@@ -41,44 +51,27 @@
|
||||
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
async function load() {
|
||||
loading = true;
|
||||
error = '';
|
||||
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() {
|
||||
// Only tick if this is today and a running entry exists.
|
||||
$effect(() => {
|
||||
const current = running;
|
||||
if (timer) clearInterval(timer);
|
||||
// Only tick if this is today and a running entry exists.
|
||||
if (running && dayKey === todayKey()) {
|
||||
timer = null;
|
||||
if (current && dayKey === todayKey()) {
|
||||
elapsed = Date.now() - current.start_time;
|
||||
timer = setInterval(() => {
|
||||
elapsed = Date.now() - running!.start_time;
|
||||
elapsed = Date.now() - current.start_time;
|
||||
}, 500);
|
||||
elapsed = Date.now() - running.start_time;
|
||||
} else {
|
||||
elapsed = 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Reload when dayKey changes.
|
||||
// Reset the editing state when dayKey changes.
|
||||
$effect(() => {
|
||||
dayKey; // dependency
|
||||
editingId = null;
|
||||
showAddForm = false;
|
||||
load();
|
||||
error = '';
|
||||
});
|
||||
|
||||
onDestroy(() => { if (timer) clearInterval(timer); });
|
||||
@@ -86,14 +79,10 @@
|
||||
async function handleStart() {
|
||||
error = '';
|
||||
try {
|
||||
const e = await entries.start(note);
|
||||
running = e;
|
||||
entryList = [...entryList, e];
|
||||
await startEntry(note);
|
||||
note = '';
|
||||
startTimer();
|
||||
oninvalidate?.();
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,61 +90,45 @@
|
||||
if (!running) return;
|
||||
error = '';
|
||||
try {
|
||||
const e = await entries.stop(running.id);
|
||||
entryList = entryList.map((x) => (x.id === e.id ? e : x));
|
||||
running = null;
|
||||
if (timer) clearInterval(timer);
|
||||
elapsed = 0;
|
||||
oninvalidate?.();
|
||||
await stopEntry(running.id);
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCloseDay() {
|
||||
error = '';
|
||||
try {
|
||||
const cd = await days.close(dayKey);
|
||||
closedDay = cd;
|
||||
oninvalidate?.(cd);
|
||||
triggerSync();
|
||||
await closeDay(dayKey);
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleMark(kind: 'holiday' | 'vacation' | 'sick') {
|
||||
error = '';
|
||||
try {
|
||||
const cd = await days.mark(dayKey, kind);
|
||||
closedDay = cd;
|
||||
oninvalidate?.(cd);
|
||||
triggerSync();
|
||||
await markDay(dayKey, kind);
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleReopenDay() {
|
||||
error = '';
|
||||
try {
|
||||
await days.reopen(dayKey);
|
||||
closedDay = null;
|
||||
oninvalidate?.(null);
|
||||
triggerSync();
|
||||
await reopenDay(dayKey);
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDeleteEntry(id: string) {
|
||||
error = '';
|
||||
try {
|
||||
await entries.delete(id);
|
||||
entryList = entryList.filter((e) => e.id !== id);
|
||||
oninvalidate?.();
|
||||
await deleteEntry(id);
|
||||
} 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 <= startMs) { addError = 'End must be after start'; return; }
|
||||
try {
|
||||
const e = await entries.createInterval(startMs, endMs, addNote);
|
||||
entryList = [...entryList, e].sort((a, b) => a.start_time - b.start_time);
|
||||
await createInterval(startMs, endMs, addNote);
|
||||
closeAddForm();
|
||||
oninvalidate?.();
|
||||
} catch (e) {
|
||||
addError = e instanceof ApiError ? e.message : String(e);
|
||||
addError = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Inline edit ─────────────────────────────────────────────────────────────
|
||||
|
||||
function startEdit(entry: Entry) {
|
||||
function startEdit(entry: Doc<Entry>) {
|
||||
editingId = entry.id;
|
||||
editStart = toTimeInput(entry.start_time);
|
||||
editEnd = entry.end_time ? toTimeInput(entry.end_time) : '';
|
||||
@@ -206,7 +177,7 @@
|
||||
editError = '';
|
||||
}
|
||||
|
||||
async function saveEdit(entry: Entry) {
|
||||
async function saveEdit(entry: Doc<Entry>) {
|
||||
editError = '';
|
||||
const startMs = parseTimeInput(dayKey, editStart);
|
||||
if (startMs === null) { editError = 'Invalid start time'; return; }
|
||||
@@ -222,19 +193,14 @@
|
||||
}
|
||||
|
||||
try {
|
||||
const updated = await entries.update(entry.id, {
|
||||
await updateEntry(entry.id, {
|
||||
start_time: startMs,
|
||||
...(endMs !== undefined ? { end_time: endMs } : {}),
|
||||
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;
|
||||
oninvalidate?.();
|
||||
} catch (e) {
|
||||
editError = e instanceof ApiError ? e.message : String(e);
|
||||
editError = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user