feat(m4): SvelteKit frontend - today, week, history, settings views
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { days, weeks, type ClosedDay, type ClosedWeek, ApiError } from '$lib/api/client';
|
||||
import { formatDurationShort, formatDelta } from '$lib/utils';
|
||||
|
||||
// Show last 12 weeks
|
||||
function pastWeekKeys(n: number): string[] {
|
||||
const result: string[] = [];
|
||||
const now = new Date();
|
||||
for (let i = 0; i < n; i++) {
|
||||
const d = new Date(now);
|
||||
d.setDate(now.getDate() - i * 7);
|
||||
const thu = new Date(d);
|
||||
thu.setDate(d.getDate() + 4 - (d.getDay() || 7));
|
||||
const ys = new Date(thu.getFullYear(), 0, 1);
|
||||
const w = Math.ceil(((thu.getTime() - ys.getTime()) / 86400000 + 1) / 7);
|
||||
result.push(`${thu.getFullYear()}-W${String(w).padStart(2, '0')}`);
|
||||
}
|
||||
return result.reverse();
|
||||
}
|
||||
|
||||
let weekKeys = pastWeekKeys(12);
|
||||
let closedWeeksMap: Record<string, ClosedWeek> = $state({});
|
||||
let closedDaysMap: Record<string, ClosedDay> = $state({});
|
||||
let error = $state('');
|
||||
let loading = $state(false);
|
||||
|
||||
onMount(async () => {
|
||||
loading = true;
|
||||
try {
|
||||
const from = weekKeys[0];
|
||||
const to = weekKeys[weekKeys.length - 1];
|
||||
// Estimate day range
|
||||
const dayFrom = from.replace('-W', '') + '-01-01'; // rough
|
||||
const [ws, ds] = await Promise.all([
|
||||
weeks.list(from, to),
|
||||
days.list('2000-01-01', '2100-01-01')
|
||||
]);
|
||||
closedWeeksMap = Object.fromEntries((ws ?? []).map((w) => [w.week_key, w]));
|
||||
closedDaysMap = Object.fromEntries((ds ?? []).map((d) => [d.day_key, d]));
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="history">
|
||||
<h1>History</h1>
|
||||
{#if error}<p class="error">{error}</p>{/if}
|
||||
{#if loading}<p>Loading…</p>{/if}
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Week</th>
|
||||
<th>Worked</th>
|
||||
<th>Expected</th>
|
||||
<th>Delta</th>
|
||||
<th>Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#each weekKeys as wk (wk)}
|
||||
{@const cw = closedWeeksMap[wk]}
|
||||
<tr class:closed={!!cw}>
|
||||
<td>{wk}</td>
|
||||
<td>{cw ? formatDurationShort(cw.worked_ms) : '—'}</td>
|
||||
<td>{cw ? formatDurationShort(cw.expected_ms) : '—'}</td>
|
||||
<td class:positive={cw && cw.delta_ms >= 0} class:negative={cw && cw.delta_ms < 0}>
|
||||
{cw ? formatDelta(cw.delta_ms) : '—'}
|
||||
</td>
|
||||
<td>{cw ? 'closed' : 'open'}</td>
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>Recent Days</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Day</th>
|
||||
<th>Kind</th>
|
||||
<th>Worked</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#each Object.entries(closedDaysMap).sort((a, b) => b[0].localeCompare(a[0])).slice(0, 30) as [dk, cd]}
|
||||
<tr>
|
||||
<td>{dk}</td>
|
||||
<td><span class="badge" data-kind={cd.kind}>{cd.kind}</span></td>
|
||||
<td>{formatDurationShort(cd.worked_ms)}</td>
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
h1 { margin: 0 0 1rem; }
|
||||
h2 { margin: 2rem 0 0.5rem; }
|
||||
.error { color: #c0392b; background: #fde8e4; padding: 0.5rem; border-radius: 6px; }
|
||||
table { width: 100%; border-collapse: collapse; background: #fff; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.08); margin-bottom: 1rem; }
|
||||
th { background: #f8f9fa; padding: 0.6rem 1rem; text-align: left; font-size: 0.85rem; color: #6c757d; border-bottom: 1px solid #dee2e6; }
|
||||
td { padding: 0.5rem 1rem; border-bottom: 1px solid #f1f3f5; font-size: 0.9rem; }
|
||||
tr.closed td { color: #495057; }
|
||||
.positive { color: #27ae60; font-weight: 600; }
|
||||
.negative { color: #c0392b; font-weight: 600; }
|
||||
.badge { font-size: 0.75rem; padding: 0.15rem 0.4rem; border-radius: 4px; }
|
||||
.badge[data-kind="work"] { background: #d4edda; color: #155724; }
|
||||
.badge[data-kind="holiday"] { background: #fff3cd; color: #856404; }
|
||||
.badge[data-kind="vacation"] { background: #cce5ff; color: #004085; }
|
||||
.badge[data-kind="sick"] { background: #f8d7da; color: #721c24; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user