244 lines
7.8 KiB
Svelte
244 lines
7.8 KiB
Svelte
<script lang="ts">
|
||
import { errorMessage } from '$lib/pb';
|
||
import {
|
||
closedDaysInRange,
|
||
closedWeek as findClosedWeek,
|
||
closeWeek,
|
||
currentSettings as getSettings,
|
||
dayExpectedMs,
|
||
entriesForRange,
|
||
reopenWeek,
|
||
runningDurationMs,
|
||
runningEntry,
|
||
todayKey,
|
||
weekExpectedMs,
|
||
workedMsForDay
|
||
} from '$lib/app.svelte';
|
||
import type { Doc } from '$lib/db.svelte';
|
||
import type { ClosedDay } from '$lib/types';
|
||
import {
|
||
weekDayKeys, formatDurationShort, formatDelta,
|
||
isWorkday, defaultDayForWeek
|
||
} from '$lib/utils';
|
||
import DayChip from '$lib/components/DayChip.svelte';
|
||
import DayDetail from '$lib/components/DayDetail.svelte';
|
||
|
||
interface Props {
|
||
weekKey: string;
|
||
selectedDay: string;
|
||
onweekchange: (weekKey: string, dayKey: string) => void;
|
||
ondaychange: (dayKey: string) => void;
|
||
}
|
||
|
||
let { weekKey, selectedDay, onweekchange, ondaychange }: Props = $props();
|
||
|
||
function offsetWeek(wk: string, offset: number): string {
|
||
const [y, w] = wk.split('-W').map(Number);
|
||
const date = new Date(y, 0, 4);
|
||
date.setDate(date.getDate() + (w - 1) * 7 + offset * 7);
|
||
const thu = new Date(date);
|
||
thu.setDate(date.getDate() + 4 - (date.getDay() || 7));
|
||
const ys = new Date(thu.getFullYear(), 0, 1);
|
||
const weekNum = Math.ceil(((thu.getTime() - ys.getTime()) / 86400000 + 1) / 7);
|
||
return `${thu.getFullYear()}-W${String(weekNum).padStart(2, '0')}`;
|
||
}
|
||
|
||
function navigateWeek(offset: number) {
|
||
const wk = offsetWeek(weekKey, offset);
|
||
onweekchange(wk, defaultDayForWeek(wk, todayKey()));
|
||
}
|
||
|
||
// ── Data ────────────────────────────────────────────────────────────────────
|
||
|
||
let dayKeys = $derived(weekDayKeys(weekKey));
|
||
let weekEntries = $derived(entriesForRange(dayKeys[0], dayKeys[6]));
|
||
let closedDaysMap = $derived.by(() => {
|
||
const map: Record<string, Doc<ClosedDay>> = {};
|
||
for (const d of closedDaysInRange(dayKeys[0], dayKeys[6])) map[d.day_key] = d;
|
||
return map;
|
||
});
|
||
let closedWeek = $derived(findClosedWeek(weekKey) ?? null);
|
||
let currentSettings = $derived(getSettings());
|
||
let error = $state('');
|
||
let busy = $state(false);
|
||
|
||
$effect(() => {
|
||
weekKey;
|
||
error = '';
|
||
});
|
||
|
||
// ── Keyboard navigation ──────────────────────────────────────────────────────
|
||
|
||
function handleStripKeydown(e: KeyboardEvent) {
|
||
const idx = dayKeys.indexOf(selectedDay);
|
||
if (e.key === 'ArrowRight' && idx < 6) {
|
||
ondaychange(dayKeys[idx + 1]);
|
||
e.preventDefault();
|
||
} else if (e.key === 'ArrowLeft' && idx > 0) {
|
||
ondaychange(dayKeys[idx - 1]);
|
||
e.preventDefault();
|
||
}
|
||
}
|
||
|
||
// ── Week actions ─────────────────────────────────────────────────────────────
|
||
|
||
async function handleCloseWeek() {
|
||
error = '';
|
||
busy = true;
|
||
try {
|
||
await closeWeek(weekKey);
|
||
} catch (e) {
|
||
error = errorMessage(e);
|
||
} finally {
|
||
busy = false;
|
||
}
|
||
}
|
||
|
||
async function handleReopenWeek() {
|
||
error = '';
|
||
busy = true;
|
||
try {
|
||
await reopenWeek(weekKey);
|
||
} catch (e) {
|
||
error = errorMessage(e);
|
||
} finally {
|
||
busy = false;
|
||
}
|
||
}
|
||
|
||
// ── Derived display values ───────────────────────────────────────────────────
|
||
|
||
const DAY_NAMES = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||
|
||
let running = $derived(runningEntry());
|
||
let runningMs = $derived(runningDurationMs());
|
||
|
||
function dayWorkedMs(dk: string): number {
|
||
const cd = closedDaysMap[dk];
|
||
if (cd) return cd.worked_ms;
|
||
return workedMsForDay(dk);
|
||
}
|
||
|
||
const totalWorkedMs = $derived(
|
||
dayKeys.reduce((sum, dk) => sum + dayWorkedMs(dk), 0)
|
||
);
|
||
const expectedMs = $derived(weekExpectedMs(weekKey));
|
||
|
||
const daysWithEntries = $derived(new Set(weekEntries.map((e) => e.day_key)));
|
||
|
||
const canCloseWeek = $derived(
|
||
!closedWeek &&
|
||
dayKeys[0] <= todayKey() &&
|
||
dayKeys.every((dk) => {
|
||
if (dk > todayKey()) return true;
|
||
if (!daysWithEntries.has(dk)) return true;
|
||
return !!closedDaysMap[dk];
|
||
})
|
||
);
|
||
|
||
let chipRefs: Record<string, HTMLElement> = {};
|
||
$effect(() => {
|
||
const el = chipRefs[selectedDay];
|
||
el?.scrollIntoView({ inline: 'nearest', block: 'nearest', behavior: 'smooth' });
|
||
});
|
||
</script>
|
||
|
||
<div class="week-view">
|
||
<div class="header">
|
||
<button onclick={() => navigateWeek(-1)} aria-label="Previous week">‹</button>
|
||
<h1>Week {weekKey}</h1>
|
||
<button onclick={() => navigateWeek(1)} aria-label="Next week">›</button>
|
||
</div>
|
||
|
||
{#if error}<p class="error">{error}</p>{/if}
|
||
|
||
<!-- svelte-ignore a11y_interactive_supports_focus -->
|
||
<div
|
||
class="day-strip"
|
||
role="tablist"
|
||
aria-label="Days of the week"
|
||
onkeydown={handleStripKeydown}
|
||
>
|
||
{#each dayKeys as dk, i (dk)}
|
||
<div class="chip-slot" bind:this={chipRefs[dk]}>
|
||
<DayChip
|
||
dayKey={dk}
|
||
weekdayLabel={DAY_NAMES[i]}
|
||
workedMs={dayWorkedMs(dk)}
|
||
expectedMs={dayExpectedMs(dk)}
|
||
runningMs={running?.day_key === dk ? runningMs : 0}
|
||
kind={closedDaysMap[dk]?.kind ?? null}
|
||
closed={!!closedDaysMap[dk]}
|
||
isToday={dk === todayKey()}
|
||
selected={dk === selectedDay}
|
||
isWorkday={isWorkday(dk, getSettings(dk).workdays_mask)}
|
||
onclick={() => ondaychange(dk)}
|
||
/>
|
||
</div>
|
||
{/each}
|
||
</div>
|
||
|
||
<div class="summary">
|
||
<div class="row">
|
||
<span>Worked</span>
|
||
<strong>{formatDurationShort(totalWorkedMs)}</strong>
|
||
</div>
|
||
<div class="row">
|
||
<span>Expected</span>
|
||
<strong>{formatDurationShort(expectedMs)}</strong>
|
||
</div>
|
||
<div class="row delta" class:positive={totalWorkedMs >= expectedMs}>
|
||
<span>Delta</span>
|
||
<strong>{formatDelta(totalWorkedMs - expectedMs)}</strong>
|
||
</div>
|
||
</div>
|
||
|
||
{#if closedWeek}
|
||
<div class="week-closed">
|
||
<p>Week closed — overtime: <strong>{formatDelta(closedWeek.delta_ms)}</strong></p>
|
||
<button onclick={handleReopenWeek} disabled={busy}>Reopen week</button>
|
||
</div>
|
||
{:else if canCloseWeek}
|
||
<button class="btn close-week" onclick={handleCloseWeek} disabled={busy}>Close week</button>
|
||
{/if}
|
||
|
||
<div class="day-detail-panel" role="tabpanel">
|
||
<h2 class="detail-heading">{selectedDay}</h2>
|
||
<DayDetail dayKey={selectedDay} />
|
||
</div>
|
||
</div>
|
||
|
||
<style>
|
||
.week-view h1 { margin: 0; font-size: 1.2rem; }
|
||
.header { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; }
|
||
.header button { background: none; border: 1px solid #dee2e6; border-radius: 6px; padding: 0.25rem 0.6rem; font-size: 1.1rem; }
|
||
.error { color: #c0392b; background: #fde8e4; padding: 0.5rem; border-radius: 6px; margin-bottom: 1rem; }
|
||
|
||
.day-strip {
|
||
display: flex;
|
||
gap: 0.4rem;
|
||
margin-bottom: 1.25rem;
|
||
overflow-x: auto;
|
||
scroll-snap-type: x mandatory;
|
||
-webkit-overflow-scrolling: touch;
|
||
padding-bottom: 0.25rem;
|
||
align-items: stretch;
|
||
}
|
||
|
||
.chip-slot {
|
||
flex: 1;
|
||
min-width: 2.8rem;
|
||
display: flex;
|
||
}
|
||
|
||
.summary { background: #fff; border: 1px solid #dee2e6; border-radius: 8px; padding: 1rem; margin-bottom: 1rem; }
|
||
.row { display: flex; justify-content: space-between; padding: 0.25rem 0; }
|
||
.delta strong { color: #c0392b; }
|
||
.delta.positive strong { color: #27ae60; }
|
||
.btn.close-week { background: #343a40; color: #fff; border: none; padding: 0.55rem 1.4rem; border-radius: 6px; font-size: 1rem; font-weight: 600; }
|
||
.week-closed { background: #d4edda; border-radius: 8px; padding: 0.75rem 1rem; display: flex; align-items: center; justify-content: space-between; }
|
||
.week-closed button { background: none; border: 1px solid #155724; color: #155724; border-radius: 6px; padding: 0.3rem 0.6rem; font-size: 0.875rem; }
|
||
.day-detail-panel { border-top: 2px solid #dee2e6; margin-top: 1.25rem; padding-top: 0.75rem; }
|
||
.detail-heading { font-size: 1rem; font-weight: 600; color: #495057; margin: 0 0 0.5rem; }
|
||
</style>
|