✨ Include the active period in the remaining time display
This commit is contained in:
@@ -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<Entry> | 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<Entry>, 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<string> {
|
||||
await autoStopStale();
|
||||
if (runningEntry()) throw new Error('an entry is already running');
|
||||
|
||||
+31
-1
@@ -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 });
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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}
|
||||
<div class="progress-track" title="{formatDurationShort(workedMs)} / {formatDurationShort(expectedMs)}">
|
||||
<div class="progress-fill" class:over={workedMs > expectedMs} style="width: {progress * 100}%"></div>
|
||||
<div class="progress-fill" class:over={workedMs > expectedMs} style="width: {doneProgress * 100}%"></div>
|
||||
{#if runningMs > 0}
|
||||
<div class="progress-fill running" style="width: {runningProgress * 100}%"></div>
|
||||
{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="progress-track weekend-track"></div>
|
||||
@@ -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;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<script lang="ts">
|
||||
import { onDestroy } from 'svelte';
|
||||
import {
|
||||
closedDay as findClosedDay,
|
||||
closeDay,
|
||||
@@ -8,10 +7,12 @@
|
||||
entriesForDay,
|
||||
markDay,
|
||||
reopenDay,
|
||||
runningDurationMs,
|
||||
startEntry,
|
||||
stopEntry,
|
||||
todayKey,
|
||||
updateEntry
|
||||
updateEntry,
|
||||
workedMsForDay
|
||||
} from '$lib/app.svelte';
|
||||
import type { Doc } from '$lib/db.svelte';
|
||||
import { errorMessage } from '$lib/pb';
|
||||
@@ -31,7 +32,7 @@
|
||||
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 elapsed = $derived(running ? runningDurationMs() : 0);
|
||||
let note = $state('');
|
||||
let error = $state('');
|
||||
|
||||
@@ -49,23 +50,6 @@
|
||||
let editNote = $state('');
|
||||
let editError = $state('');
|
||||
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
// Only tick if this is today and a running entry exists.
|
||||
$effect(() => {
|
||||
const current = running;
|
||||
if (timer) clearInterval(timer);
|
||||
timer = null;
|
||||
if (current && dayKey === todayKey()) {
|
||||
elapsed = Date.now() - current.start_time;
|
||||
timer = setInterval(() => {
|
||||
elapsed = Date.now() - current.start_time;
|
||||
}, 500);
|
||||
} else {
|
||||
elapsed = 0;
|
||||
}
|
||||
});
|
||||
|
||||
// Reset the editing state when dayKey changes.
|
||||
$effect(() => {
|
||||
dayKey; // dependency
|
||||
@@ -74,8 +58,6 @@
|
||||
error = '';
|
||||
});
|
||||
|
||||
onDestroy(() => { if (timer) clearInterval(timer); });
|
||||
|
||||
async function handleStart() {
|
||||
error = '';
|
||||
try {
|
||||
@@ -204,12 +186,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
const totalWorkedMs = $derived(
|
||||
entryList.reduce((sum, e) => {
|
||||
if (e.end_time === null) return sum;
|
||||
return sum + (e.end_time - e.start_time);
|
||||
}, 0)
|
||||
);
|
||||
const totalWorkedMs = $derived(workedMsForDay(dayKey));
|
||||
|
||||
// Close day is disabled when today has a running entry (even if canCloseDay is true).
|
||||
const closeDayDisabled = $derived(!!running && dayKey === todayKey());
|
||||
|
||||
@@ -8,8 +8,11 @@
|
||||
dayExpectedMs,
|
||||
entriesForRange,
|
||||
reopenWeek,
|
||||
runningDurationMs,
|
||||
runningEntry,
|
||||
todayKey,
|
||||
weekExpectedMs
|
||||
weekExpectedMs,
|
||||
workedMsForDay
|
||||
} from '$lib/app.svelte';
|
||||
import type { Doc } from '$lib/db.svelte';
|
||||
import type { ClosedDay } from '$lib/types';
|
||||
@@ -107,12 +110,13 @@
|
||||
|
||||
const DAY_NAMES = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||
|
||||
let running = $derived(runningEntry());
|
||||
let runningMs = $derived(runningDurationMs());
|
||||
|
||||
function dayWorkedMs(dk: string): number {
|
||||
const cd = closedDaysMap[dk];
|
||||
if (cd) return cd.worked_ms;
|
||||
return weekEntries
|
||||
.filter((e) => e.day_key === dk && e.end_time != null)
|
||||
.reduce((sum, e) => sum + (e.end_time! - e.start_time), 0);
|
||||
return workedMsForDay(dk);
|
||||
}
|
||||
|
||||
const totalWorkedMs = $derived(
|
||||
@@ -162,6 +166,7 @@
|
||||
weekdayLabel={DAY_NAMES[i]}
|
||||
workedMs={dayWorkedMs(dk)}
|
||||
expectedMs={dayExpectedMs(dk)}
|
||||
runningMs={running?.day_key === dk ? runningMs : 0}
|
||||
kind={closedDaysMap[dk]?.kind ?? null}
|
||||
closed={!!closedDaysMap[dk]}
|
||||
isToday={dk === todayKey()}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { page } from '$app/state';
|
||||
import { startClock } from '$lib/clock.svelte';
|
||||
import { pendingCount, status, syncNow } from '$lib/db.svelte';
|
||||
import { gateStale, resolveGate, safeNext, session, type Gate } from '$lib/session.svelte';
|
||||
|
||||
@@ -44,7 +45,11 @@
|
||||
void sw.register('/sw.js', { scope: '/' }).catch(() => {});
|
||||
}
|
||||
const tickInterval = setInterval(() => tick++, 15_000);
|
||||
return () => clearInterval(tickInterval);
|
||||
const stopClock = startClock();
|
||||
return () => {
|
||||
clearInterval(tickInterval);
|
||||
stopClock();
|
||||
};
|
||||
});
|
||||
|
||||
const inApp = $derived(gate === 'app' && status.ready);
|
||||
|
||||
Reference in New Issue
Block a user