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})`;
+}