From aa96c37860cfee9aefcb19b696b35984a80b59aa Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 3 Dec 2026 19:17:46 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Include=20the=20active=20period=20i?= =?UTF-8?q?n=20the=20remaining=20time=20display?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/lib/app.svelte.ts | 18 ++++++++++++++ web/src/lib/app.test.ts | 32 +++++++++++++++++++++++- web/src/lib/clock.svelte.ts | 8 ++++++ web/src/lib/components/DayChip.svelte | 16 ++++++++++-- web/src/lib/components/DayDetail.svelte | 33 ++++--------------------- web/src/lib/components/WeekView.svelte | 13 +++++++--- web/src/routes/+layout.svelte | 7 +++++- 7 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 web/src/lib/clock.svelte.ts diff --git a/web/src/lib/app.svelte.ts b/web/src/lib/app.svelte.ts index dd80e34..d3986dc 100644 --- a/web/src/lib/app.svelte.ts +++ b/web/src/lib/app.svelte.ts @@ -11,6 +11,7 @@ import { type Settings } from '$lib/types'; import { dayKeyForMs, endOfDayMs, isWorkday, weekDayKeys } from '$lib/utils'; +import { clock } from '$lib/clock.svelte'; const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/; const LAST_DAY = '9999-12-31'; @@ -119,6 +120,23 @@ export function runningEntry(): Doc | undefined { return all('entries').find((e) => e.end_time === null); } +/** Duration of an entry; a running one counts until now, capped at the end of its day like a stop would be. */ +export function entryDurationMs(entry: Doc, now: number): number { + const end = entry.end_time ?? Math.min(now, endOfDayMs(entry.day_key, currentSettings(entry.day_key).timezone)); + return Math.max(0, end - entry.start_time); +} + +/** Worked time of a day, with the running entry counted up to the current clock. Reads the clock so derived values re-run on every tick. */ +export function workedMsForDay(dayKey: string): number { + return entriesForDay(dayKey).reduce((sum, e) => sum + entryDurationMs(e, clock.now), 0); +} + +/** Time logged so far on the running entry; 0 when nothing runs. Reads the clock, so it updates on every tick. */ +export function runningDurationMs(): number { + const running = runningEntry(); + return running ? entryDurationMs(running, clock.now) : 0; +} + export async function startEntry(note = ''): Promise { await autoStopStale(); if (runningEntry()) throw new Error('an entry is already running'); diff --git a/web/src/lib/app.test.ts b/web/src/lib/app.test.ts index b1c8bf2..4a68224 100644 --- a/web/src/lib/app.test.ts +++ b/web/src/lib/app.test.ts @@ -9,14 +9,17 @@ import { deleteSettings, dayExpectedMs, entriesForDay, + runningDurationMs, runningEntry, settingsHistory, startEntry, weekExpectedMs, stopEntry, todayKey, - updateEntry + updateEntry, + workedMsForDay } from './app.svelte'; +import { clock } from './clock.svelte'; import { closeDb, get, openDb, remove, save, status } from './db.svelte'; import { DEFAULT_SETTINGS } from './types'; @@ -228,6 +231,33 @@ describe('entries', () => { }); }); +describe('worked time', () => { + it('workedMsForDay counts a completed entry fully and a running entry up to the clock', async () => { + const start = Date.parse('2026-06-16T08:00:00Z'); + await save('entries', null, { start_time: Date.parse('2026-06-16T06:00:00Z'), end_time: Date.parse('2026-06-16T07:00:00Z'), auto_stopped: false, note: '', day_key: '2026-06-16' }); + await save('entries', null, { start_time: start, end_time: null, auto_stopped: false, note: '', day_key: '2026-06-16' }); + clock.now = start + 10 * 60_000; + expect(workedMsForDay('2026-06-16')).toBe(3_600_000 + 600_000); + clock.now = start + 20 * 60_000; + expect(workedMsForDay('2026-06-16')).toBe(3_600_000 + 1_200_000); + }); + + it('a running entry on a past day counts only until the end of that day', async () => { + const start = Date.parse('2026-06-15T17:00:00Z'); + await save('entries', null, { start_time: start, end_time: null, auto_stopped: false, note: '', day_key: '2026-06-15' }); + clock.now = Date.parse('2026-06-17T10:00:00Z'); + expect(workedMsForDay('2026-06-15')).toBe(Date.parse('2026-06-15T23:59:59.999Z') - start); + }); + + it('runningDurationMs is 0 when nothing runs and counts up while an entry runs', async () => { + expect(runningDurationMs()).toBe(0); + const start = Date.parse('2026-06-16T08:00:00Z'); + await save('entries', null, { start_time: start, end_time: null, auto_stopped: false, note: '', day_key: '2026-06-16' }); + clock.now = start + 5 * 60_000; + expect(runningDurationMs()).toBe(300_000); + }); +}); + describe('balance', () => { it('balance sums non-deleted weeks and adjustments', async () => { await save('closed_weeks', null, { week_key: '2026-W23', expected_ms: 1, worked_ms: 2, delta_ms: 3_600_000, closed_at: 1 }); diff --git a/web/src/lib/clock.svelte.ts b/web/src/lib/clock.svelte.ts new file mode 100644 index 0000000..01bd27e --- /dev/null +++ b/web/src/lib/clock.svelte.ts @@ -0,0 +1,8 @@ +export const clock = $state({ now: Date.now() }); + +/** Keeps the clock current while mounted; returns the cleanup. */ +export function startClock(): () => void { + clock.now = Date.now(); + const timer = setInterval(() => (clock.now = Date.now()), 500); + return () => clearInterval(timer); +} diff --git a/web/src/lib/components/DayChip.svelte b/web/src/lib/components/DayChip.svelte index f1bed3d..6b82c5c 100644 --- a/web/src/lib/components/DayChip.svelte +++ b/web/src/lib/components/DayChip.svelte @@ -6,6 +6,7 @@ weekdayLabel: string; // e.g. 'Mon' workedMs: number; expectedMs: number; + runningMs?: number; // duration of the running entry on this day, blue bar segment kind: 'work' | 'holiday' | 'vacation' | 'sick' | null; // null = open/untracked closed: boolean; isToday: boolean; @@ -19,6 +20,7 @@ weekdayLabel, workedMs, expectedMs, + runningMs = 0, kind, closed, isToday, @@ -30,7 +32,9 @@ const dateNum = $derived(dayKey.slice(8)); // DD // Progress bar fill: clamp to [0,1]. Non-workdays have expectedMs=0 → no bar. - const progress = $derived(expectedMs > 0 ? Math.min(workedMs / expectedMs, 1) : 0); + const doneMs = $derived(Math.max(0, workedMs - runningMs)); + const doneProgress = $derived(expectedMs > 0 ? Math.min(doneMs / expectedMs, 1) : 0); + const runningProgress = $derived(expectedMs > 0 ? Math.min(runningMs / expectedMs, 1 - doneProgress) : 0); const workedLabel = $derived(workedMs > 0 ? formatDurationShort(workedMs) : null); @@ -60,7 +64,10 @@ {#if expectedMs > 0}
-
expectedMs} style="width: {progress * 100}%">
+
expectedMs} style="width: {doneProgress * 100}%">
+ {#if runningMs > 0} +
+ {/if}
{:else}
@@ -149,6 +156,7 @@ } .progress-track { + display: flex; width: 100%; height: 4px; background: #e9ecef; @@ -172,6 +180,10 @@ background: #f39c12; } + .progress-fill.running { + background: #2c7be5; + } + /* Worked duration text — hidden by default, shown when chip is wide */ .worked-label { display: none; diff --git a/web/src/lib/components/DayDetail.svelte b/web/src/lib/components/DayDetail.svelte index 63d8afa..284ce4c 100644 --- a/web/src/lib/components/DayDetail.svelte +++ b/web/src/lib/components/DayDetail.svelte @@ -1,5 +1,4 @@