✨ Include the active period in the remaining time display
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
|||||||
type Settings
|
type Settings
|
||||||
} from '$lib/types';
|
} from '$lib/types';
|
||||||
import { dayKeyForMs, endOfDayMs, isWorkday, weekDayKeys } from '$lib/utils';
|
import { dayKeyForMs, endOfDayMs, isWorkday, weekDayKeys } from '$lib/utils';
|
||||||
|
import { clock } from '$lib/clock.svelte';
|
||||||
|
|
||||||
const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
||||||
const LAST_DAY = '9999-12-31';
|
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);
|
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> {
|
export async function startEntry(note = ''): Promise<string> {
|
||||||
await autoStopStale();
|
await autoStopStale();
|
||||||
if (runningEntry()) throw new Error('an entry is already running');
|
if (runningEntry()) throw new Error('an entry is already running');
|
||||||
|
|||||||
+31
-1
@@ -9,14 +9,17 @@ import {
|
|||||||
deleteSettings,
|
deleteSettings,
|
||||||
dayExpectedMs,
|
dayExpectedMs,
|
||||||
entriesForDay,
|
entriesForDay,
|
||||||
|
runningDurationMs,
|
||||||
runningEntry,
|
runningEntry,
|
||||||
settingsHistory,
|
settingsHistory,
|
||||||
startEntry,
|
startEntry,
|
||||||
weekExpectedMs,
|
weekExpectedMs,
|
||||||
stopEntry,
|
stopEntry,
|
||||||
todayKey,
|
todayKey,
|
||||||
updateEntry
|
updateEntry,
|
||||||
|
workedMsForDay
|
||||||
} from './app.svelte';
|
} from './app.svelte';
|
||||||
|
import { clock } from './clock.svelte';
|
||||||
import { closeDb, get, openDb, remove, save, status } from './db.svelte';
|
import { closeDb, get, openDb, remove, save, status } from './db.svelte';
|
||||||
import { DEFAULT_SETTINGS } from './types';
|
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', () => {
|
describe('balance', () => {
|
||||||
it('balance sums non-deleted weeks and adjustments', async () => {
|
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 });
|
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'
|
weekdayLabel: string; // e.g. 'Mon'
|
||||||
workedMs: number;
|
workedMs: number;
|
||||||
expectedMs: 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
|
kind: 'work' | 'holiday' | 'vacation' | 'sick' | null; // null = open/untracked
|
||||||
closed: boolean;
|
closed: boolean;
|
||||||
isToday: boolean;
|
isToday: boolean;
|
||||||
@@ -19,6 +20,7 @@
|
|||||||
weekdayLabel,
|
weekdayLabel,
|
||||||
workedMs,
|
workedMs,
|
||||||
expectedMs,
|
expectedMs,
|
||||||
|
runningMs = 0,
|
||||||
kind,
|
kind,
|
||||||
closed,
|
closed,
|
||||||
isToday,
|
isToday,
|
||||||
@@ -30,7 +32,9 @@
|
|||||||
const dateNum = $derived(dayKey.slice(8)); // DD
|
const dateNum = $derived(dayKey.slice(8)); // DD
|
||||||
|
|
||||||
// Progress bar fill: clamp to [0,1]. Non-workdays have expectedMs=0 → no bar.
|
// 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);
|
const workedLabel = $derived(workedMs > 0 ? formatDurationShort(workedMs) : null);
|
||||||
|
|
||||||
@@ -60,7 +64,10 @@
|
|||||||
|
|
||||||
{#if expectedMs > 0}
|
{#if expectedMs > 0}
|
||||||
<div class="progress-track" title="{formatDurationShort(workedMs)} / {formatDurationShort(expectedMs)}">
|
<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>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
<div class="progress-track weekend-track"></div>
|
<div class="progress-track weekend-track"></div>
|
||||||
@@ -149,6 +156,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.progress-track {
|
.progress-track {
|
||||||
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 4px;
|
height: 4px;
|
||||||
background: #e9ecef;
|
background: #e9ecef;
|
||||||
@@ -172,6 +180,10 @@
|
|||||||
background: #f39c12;
|
background: #f39c12;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.progress-fill.running {
|
||||||
|
background: #2c7be5;
|
||||||
|
}
|
||||||
|
|
||||||
/* Worked duration text — hidden by default, shown when chip is wide */
|
/* Worked duration text — hidden by default, shown when chip is wide */
|
||||||
.worked-label {
|
.worked-label {
|
||||||
display: none;
|
display: none;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onDestroy } from 'svelte';
|
|
||||||
import {
|
import {
|
||||||
closedDay as findClosedDay,
|
closedDay as findClosedDay,
|
||||||
closeDay,
|
closeDay,
|
||||||
@@ -8,10 +7,12 @@
|
|||||||
entriesForDay,
|
entriesForDay,
|
||||||
markDay,
|
markDay,
|
||||||
reopenDay,
|
reopenDay,
|
||||||
|
runningDurationMs,
|
||||||
startEntry,
|
startEntry,
|
||||||
stopEntry,
|
stopEntry,
|
||||||
todayKey,
|
todayKey,
|
||||||
updateEntry
|
updateEntry,
|
||||||
|
workedMsForDay
|
||||||
} from '$lib/app.svelte';
|
} from '$lib/app.svelte';
|
||||||
import type { Doc } from '$lib/db.svelte';
|
import type { Doc } from '$lib/db.svelte';
|
||||||
import { errorMessage } from '$lib/pb';
|
import { errorMessage } from '$lib/pb';
|
||||||
@@ -31,7 +32,7 @@
|
|||||||
let closedDay = $derived(findClosedDay(dayKey));
|
let closedDay = $derived(findClosedDay(dayKey));
|
||||||
let running = $derived(entryList.find((e) => e.end_time === null) ?? null);
|
let running = $derived(entryList.find((e) => e.end_time === null) ?? null);
|
||||||
let capabilities = $derived(dayCapabilities(dayKey, todayKey(), !!closedDay));
|
let capabilities = $derived(dayCapabilities(dayKey, todayKey(), !!closedDay));
|
||||||
let elapsed = $state(0);
|
let elapsed = $derived(running ? runningDurationMs() : 0);
|
||||||
let note = $state('');
|
let note = $state('');
|
||||||
let error = $state('');
|
let error = $state('');
|
||||||
|
|
||||||
@@ -49,23 +50,6 @@
|
|||||||
let editNote = $state('');
|
let editNote = $state('');
|
||||||
let editError = $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.
|
// Reset the editing state when dayKey changes.
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
dayKey; // dependency
|
dayKey; // dependency
|
||||||
@@ -74,8 +58,6 @@
|
|||||||
error = '';
|
error = '';
|
||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(() => { if (timer) clearInterval(timer); });
|
|
||||||
|
|
||||||
async function handleStart() {
|
async function handleStart() {
|
||||||
error = '';
|
error = '';
|
||||||
try {
|
try {
|
||||||
@@ -204,12 +186,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalWorkedMs = $derived(
|
const totalWorkedMs = $derived(workedMsForDay(dayKey));
|
||||||
entryList.reduce((sum, e) => {
|
|
||||||
if (e.end_time === null) return sum;
|
|
||||||
return sum + (e.end_time - e.start_time);
|
|
||||||
}, 0)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Close day is disabled when today has a running entry (even if canCloseDay is true).
|
// Close day is disabled when today has a running entry (even if canCloseDay is true).
|
||||||
const closeDayDisabled = $derived(!!running && dayKey === todayKey());
|
const closeDayDisabled = $derived(!!running && dayKey === todayKey());
|
||||||
|
|||||||
@@ -8,8 +8,11 @@
|
|||||||
dayExpectedMs,
|
dayExpectedMs,
|
||||||
entriesForRange,
|
entriesForRange,
|
||||||
reopenWeek,
|
reopenWeek,
|
||||||
|
runningDurationMs,
|
||||||
|
runningEntry,
|
||||||
todayKey,
|
todayKey,
|
||||||
weekExpectedMs
|
weekExpectedMs,
|
||||||
|
workedMsForDay
|
||||||
} from '$lib/app.svelte';
|
} from '$lib/app.svelte';
|
||||||
import type { Doc } from '$lib/db.svelte';
|
import type { Doc } from '$lib/db.svelte';
|
||||||
import type { ClosedDay } from '$lib/types';
|
import type { ClosedDay } from '$lib/types';
|
||||||
@@ -107,12 +110,13 @@
|
|||||||
|
|
||||||
const DAY_NAMES = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
const DAY_NAMES = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||||
|
|
||||||
|
let running = $derived(runningEntry());
|
||||||
|
let runningMs = $derived(runningDurationMs());
|
||||||
|
|
||||||
function dayWorkedMs(dk: string): number {
|
function dayWorkedMs(dk: string): number {
|
||||||
const cd = closedDaysMap[dk];
|
const cd = closedDaysMap[dk];
|
||||||
if (cd) return cd.worked_ms;
|
if (cd) return cd.worked_ms;
|
||||||
return weekEntries
|
return workedMsForDay(dk);
|
||||||
.filter((e) => e.day_key === dk && e.end_time != null)
|
|
||||||
.reduce((sum, e) => sum + (e.end_time! - e.start_time), 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalWorkedMs = $derived(
|
const totalWorkedMs = $derived(
|
||||||
@@ -162,6 +166,7 @@
|
|||||||
weekdayLabel={DAY_NAMES[i]}
|
weekdayLabel={DAY_NAMES[i]}
|
||||||
workedMs={dayWorkedMs(dk)}
|
workedMs={dayWorkedMs(dk)}
|
||||||
expectedMs={dayExpectedMs(dk)}
|
expectedMs={dayExpectedMs(dk)}
|
||||||
|
runningMs={running?.day_key === dk ? runningMs : 0}
|
||||||
kind={closedDaysMap[dk]?.kind ?? null}
|
kind={closedDaysMap[dk]?.kind ?? null}
|
||||||
closed={!!closedDaysMap[dk]}
|
closed={!!closedDaysMap[dk]}
|
||||||
isToday={dk === todayKey()}
|
isToday={dk === todayKey()}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import { page } from '$app/state';
|
import { page } from '$app/state';
|
||||||
|
import { startClock } from '$lib/clock.svelte';
|
||||||
import { pendingCount, status, syncNow } from '$lib/db.svelte';
|
import { pendingCount, status, syncNow } from '$lib/db.svelte';
|
||||||
import { gateStale, resolveGate, safeNext, session, type Gate } from '$lib/session.svelte';
|
import { gateStale, resolveGate, safeNext, session, type Gate } from '$lib/session.svelte';
|
||||||
|
|
||||||
@@ -44,7 +45,11 @@
|
|||||||
void sw.register('/sw.js', { scope: '/' }).catch(() => {});
|
void sw.register('/sw.js', { scope: '/' }).catch(() => {});
|
||||||
}
|
}
|
||||||
const tickInterval = setInterval(() => tick++, 15_000);
|
const tickInterval = setInterval(() => tick++, 15_000);
|
||||||
return () => clearInterval(tickInterval);
|
const stopClock = startClock();
|
||||||
|
return () => {
|
||||||
|
clearInterval(tickInterval);
|
||||||
|
stopClock();
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const inApp = $derived(gate === 'app' && status.ready);
|
const inApp = $derived(gate === 'app' && status.ready);
|
||||||
|
|||||||
Reference in New Issue
Block a user