♻️ Read the week view reactively from SignalDB
The week view no longer loads its data on demand and patches it after each mutation. Entries, closed days, the closed week and the settings are derived from the synced collections, so the view follows local changes and sync results. Closing and reopening a week report errors from the server in place.
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
<script lang="ts">
|
||||
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 { errorMessage } from '$lib/pb';
|
||||
import {
|
||||
closedDaysInRange,
|
||||
closedWeek as findClosedWeek,
|
||||
closeWeek,
|
||||
currentSettings as getSettings,
|
||||
entriesForRange,
|
||||
reopenWeek,
|
||||
todayKey
|
||||
} from '$lib/app.svelte';
|
||||
import type { Doc } from '$lib/db.svelte';
|
||||
import type { ClosedDay } from '$lib/types';
|
||||
import {
|
||||
weekDayKeys, formatDurationShort, formatDelta,
|
||||
todayKey, isWorkday, dayCapabilities, defaultDayForWeek
|
||||
isWorkday, defaultDayForWeek
|
||||
} from '$lib/utils';
|
||||
import DayChip from '$lib/components/DayChip.svelte';
|
||||
import DayDetail from '$lib/components/DayDetail.svelte';
|
||||
@@ -30,44 +40,27 @@
|
||||
|
||||
function navigateWeek(offset: number) {
|
||||
const wk = offsetWeek(weekKey, offset);
|
||||
onweekchange(wk, defaultDayForWeek(wk));
|
||||
onweekchange(wk, defaultDayForWeek(wk, todayKey()));
|
||||
}
|
||||
|
||||
// ── Data ────────────────────────────────────────────────────────────────────
|
||||
|
||||
let dayKeys = $derived(weekDayKeys(weekKey));
|
||||
let closedDaysMap = $state<Record<string, ClosedDay>>({});
|
||||
let weekEntries = $state<Entry[]>([]);
|
||||
let closedWeek = $state<ClosedWeek | null>(null);
|
||||
let currentSettings = $state<Settings | null>(null);
|
||||
let weekEntries = $derived(entriesForRange(dayKeys[0], dayKeys[6]));
|
||||
let closedDaysMap = $derived.by(() => {
|
||||
const map: Record<string, Doc<ClosedDay>> = {};
|
||||
for (const d of closedDaysInRange(dayKeys[0], dayKeys[6])) map[d.day_key] = d;
|
||||
return map;
|
||||
});
|
||||
let closedWeek = $derived(findClosedWeek(weekKey) ?? null);
|
||||
let currentSettings = $derived(getSettings());
|
||||
let error = $state('');
|
||||
let busy = $state(false);
|
||||
|
||||
async function load() {
|
||||
$effect(() => {
|
||||
weekKey;
|
||||
error = '';
|
||||
const from = dayKeys[0];
|
||||
const to = dayKeys[6];
|
||||
try {
|
||||
const [ds, ws, es] = await Promise.all([
|
||||
days.list(from, to),
|
||||
weeks.list(weekKey, weekKey),
|
||||
entries.list(from, to)
|
||||
]);
|
||||
closedDaysMap = Object.fromEntries((ds ?? []).map((d) => [d.day_key, d]));
|
||||
closedWeek = (ws ?? []).find((w) => w.week_key === weekKey) ?? null;
|
||||
weekEntries = es ?? [];
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
}
|
||||
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(); });
|
||||
});
|
||||
|
||||
// ── Keyboard navigation ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -86,23 +79,25 @@
|
||||
|
||||
async function handleCloseWeek() {
|
||||
error = '';
|
||||
busy = true;
|
||||
try {
|
||||
const cw = await weeks.close(weekKey);
|
||||
closedWeek = cw;
|
||||
triggerSync();
|
||||
await closeWeek(weekKey);
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleReopenWeek() {
|
||||
error = '';
|
||||
busy = true;
|
||||
try {
|
||||
await weeks.reopen(weekKey);
|
||||
closedWeek = null;
|
||||
triggerSync();
|
||||
await reopenWeek(weekKey);
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +114,6 @@
|
||||
}
|
||||
|
||||
function dailyExpectedMs(dk: string): number {
|
||||
if (!currentSettings) return 0;
|
||||
const mask = currentSettings.workdays_mask;
|
||||
if (!isWorkday(dk, mask)) return 0;
|
||||
const workdayCount = [1, 2, 4, 8, 16, 32, 64].filter((b) => mask & b).length;
|
||||
@@ -129,9 +123,7 @@
|
||||
const totalWorkedMs = $derived(
|
||||
dayKeys.reduce((sum, dk) => sum + dayWorkedMs(dk), 0)
|
||||
);
|
||||
const expectedMs = $derived(
|
||||
currentSettings ? currentSettings.hours_per_week * 3_600_000 : 0
|
||||
);
|
||||
const expectedMs = $derived(currentSettings.hours_per_week * 3_600_000);
|
||||
|
||||
const daysWithEntries = $derived(new Set(weekEntries.map((e) => e.day_key)));
|
||||
|
||||
@@ -145,10 +137,6 @@
|
||||
})
|
||||
);
|
||||
|
||||
const detailCaps = $derived(
|
||||
dayCapabilities(selectedDay, todayKey(), !!closedDaysMap[selectedDay])
|
||||
);
|
||||
|
||||
let chipRefs: Record<string, HTMLElement> = {};
|
||||
$effect(() => {
|
||||
const el = chipRefs[selectedDay];
|
||||
@@ -183,7 +171,7 @@
|
||||
closed={!!closedDaysMap[dk]}
|
||||
isToday={dk === todayKey()}
|
||||
selected={dk === selectedDay}
|
||||
isWorkday={currentSettings ? isWorkday(dk, currentSettings.workdays_mask) : i < 5}
|
||||
isWorkday={isWorkday(dk, currentSettings.workdays_mask)}
|
||||
onclick={() => ondaychange(dk)}
|
||||
/>
|
||||
</div>
|
||||
@@ -208,30 +196,15 @@
|
||||
{#if closedWeek}
|
||||
<div class="week-closed">
|
||||
<p>Week closed — overtime: <strong>{formatDelta(closedWeek.delta_ms)}</strong></p>
|
||||
<button onclick={handleReopenWeek}>Reopen week</button>
|
||||
<button onclick={handleReopenWeek} disabled={busy}>Reopen week</button>
|
||||
</div>
|
||||
{:else if canCloseWeek}
|
||||
<button class="btn close-week" onclick={handleCloseWeek}>Close week</button>
|
||||
<button class="btn close-week" onclick={handleCloseWeek} disabled={busy}>Close week</button>
|
||||
{/if}
|
||||
|
||||
<div class="day-detail-panel" role="tabpanel">
|
||||
<h2 class="detail-heading">{selectedDay}</h2>
|
||||
<DayDetail
|
||||
dayKey={selectedDay}
|
||||
capabilities={detailCaps}
|
||||
oninvalidate={(cd) => {
|
||||
if (cd !== undefined) {
|
||||
if (cd === null) {
|
||||
const { [selectedDay]: _, ...rest } = closedDaysMap;
|
||||
closedDaysMap = rest;
|
||||
} else {
|
||||
closedDaysMap = { ...closedDaysMap, [selectedDay]: cd };
|
||||
}
|
||||
} else {
|
||||
load();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<DayDetail dayKey={selectedDay} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user