From c06f0e70b1e08c398a0fff64402d9a965a1507f0 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 3 Dec 2026 19:24:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Visualize=20the=20weekly=20progress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/lib/components/WeekView.svelte | 4 ++-- web/src/lib/utils.test.ts | 30 +++++++++++++++++++++++++- web/src/lib/utils.ts | 19 ++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/web/src/lib/components/WeekView.svelte b/web/src/lib/components/WeekView.svelte index d2081c4..6fedb00 100644 --- a/web/src/lib/components/WeekView.svelte +++ b/web/src/lib/components/WeekView.svelte @@ -18,7 +18,7 @@ import type { ClosedDay } from '$lib/types'; import { weekDayKeys, formatDurationShort, formatDelta, - isWorkday, defaultDayForWeek + isWorkday, defaultDayForWeek, weekProgressBackground } from '$lib/utils'; import DayChip from '$lib/components/DayChip.svelte'; import DayDetail from '$lib/components/DayDetail.svelte'; @@ -178,7 +178,7 @@ {/each} -
+
Worked {formatDurationShort(totalWorkedMs)} diff --git a/web/src/lib/utils.test.ts b/web/src/lib/utils.test.ts index 1a0f10c..a786f32 100644 --- a/web/src/lib/utils.test.ts +++ b/web/src/lib/utils.test.ts @@ -8,7 +8,8 @@ import { dayKeyForMs, defaultDayForWeek, endOfDayMs, - newId + newId, + weekProgressBackground } from './utils'; describe('dayCapabilities', () => { @@ -171,3 +172,30 @@ describe('newId', () => { expect(ids.size).toBe(1000); }); }); + +describe('weekProgressBackground', () => { + const h = 3_600_000; + const start = (worked: number, expected: number) => weekProgressBackground(worked, expected).match(/transparent ([\d.]+)%/)![1]; + + it('fills the background in proportion to the work done and caps at the full width', () => { + expect(weekProgressBackground(0, 40 * h)).toBe(''); + expect(weekProgressBackground(5 * h, 0)).toBe(''); + expect(start(10 * h, 40 * h)).toBe('25'); + expect(start(40 * h, 40 * h)).toBe('100'); + expect(start(60 * h, 40 * h)).toBe('100'); + }); + + it('stays blue within the expected hours and turns red over the first two hours of overtime', () => { + const blue = weekProgressBackground(40 * h, 40 * h); + const half = weekProgressBackground(41 * h, 40 * h); + const full = weekProgressBackground(42 * h, 40 * h); + const beyond = weekProgressBackground(50 * h, 40 * h); + expect(blue).toContain('#e8f2ff'); + expect(blue).toContain('#c5ddf9'); + expect(half).not.toBe(blue); + expect(full).not.toBe(half); + expect(full).toContain('#fde4e0'); + expect(full).toContain('#f6b8af'); + expect(beyond).toBe(full); + }); +}); diff --git a/web/src/lib/utils.ts b/web/src/lib/utils.ts index dba056a..d26c26d 100644 --- a/web/src/lib/utils.ts +++ b/web/src/lib/utils.ts @@ -254,3 +254,22 @@ export function endOfDayMs(dayKey: string, tz: string): number { } return midnight - 1; } + +function mixHex(from: string, to: string, t: number): string { + const a = from.match(/\w\w/g)!.map((h) => parseInt(h, 16)); + const b = to.match(/\w\w/g)!.map((h) => parseInt(h, 16)); + return `#${a.map((part, i) => Math.round(part + (b[i] - part) * t).toString(16).padStart(2, '0')).join('')}`; +} + +const OVERTIME_FULL_MS = 2 * 3_600_000; + +/** CSS background for the week summary: a blue gradient as wide as the share done, blending towards red over the first two hours of overtime. */ +export function weekProgressBackground(workedMs: number, expectedMs: number): string { + if (expectedMs <= 0 || workedMs <= 0) return ''; + const share = Math.min(workedMs / expectedMs, 1); + const overtime = Math.min(Math.max((workedMs - expectedMs) / OVERTIME_FULL_MS, 0), 1); + const from = mixHex('#e8f2ff', '#fde4e0', overtime); + const to = mixHex('#c5ddf9', '#f6b8af', overtime); + const softEnd = Math.min(share * 100 + 3, 100); + return `linear-gradient(90deg, transparent ${share * 100}%, #fff ${softEnd}%), linear-gradient(90deg, ${from}, ${to})`; +}