✨ Visualize the weekly progress

This commit is contained in:
2026-12-03 19:24:36 +01:00
parent aa96c37860
commit c06f0e70b1
3 changed files with 50 additions and 3 deletions
+2 -2
View File
@@ -18,7 +18,7 @@
import type { ClosedDay } from '$lib/types'; import type { ClosedDay } from '$lib/types';
import { import {
weekDayKeys, formatDurationShort, formatDelta, weekDayKeys, formatDurationShort, formatDelta,
isWorkday, defaultDayForWeek isWorkday, defaultDayForWeek, weekProgressBackground
} from '$lib/utils'; } from '$lib/utils';
import DayChip from '$lib/components/DayChip.svelte'; import DayChip from '$lib/components/DayChip.svelte';
import DayDetail from '$lib/components/DayDetail.svelte'; import DayDetail from '$lib/components/DayDetail.svelte';
@@ -178,7 +178,7 @@
{/each} {/each}
</div> </div>
<div class="summary"> <div class="summary" style="background-image: {weekProgressBackground(totalWorkedMs, expectedMs)}">
<div class="row"> <div class="row">
<span>Worked</span> <span>Worked</span>
<strong>{formatDurationShort(totalWorkedMs)}</strong> <strong>{formatDurationShort(totalWorkedMs)}</strong>
+29 -1
View File
@@ -8,7 +8,8 @@ import {
dayKeyForMs, dayKeyForMs,
defaultDayForWeek, defaultDayForWeek,
endOfDayMs, endOfDayMs,
newId newId,
weekProgressBackground
} from './utils'; } from './utils';
describe('dayCapabilities', () => { describe('dayCapabilities', () => {
@@ -171,3 +172,30 @@ describe('newId', () => {
expect(ids.size).toBe(1000); 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);
});
});
+19
View File
@@ -254,3 +254,22 @@ export function endOfDayMs(dayKey: string, tz: string): number {
} }
return midnight - 1; 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})`;
}