import { useEffect, useState } from 'react'; import { ChevronLeft, ChevronRight } from 'lucide-react'; import { clsx } from 'clsx'; import { trpc } from '../lib/trpc'; import { Card } from '../components/ui/Card'; import { Button } from '../components/ui/Button'; import { SkeletonCard } from '../components/ui/SkeletonCard'; import { ErrorState } from '../components/ui/ErrorState'; import { formatDurationCompact } from '../lib/format-duration'; import { TIME_CATEGORIES } from '../lib/time-categories'; const CATEGORY_LABEL: Record = Object.fromEntries( TIME_CATEGORIES.map((c) => [c.value, c.label]), ); const CATEGORY_TINT: Record = { focus: 'bg-[var(--color-ink)] text-[var(--color-brand-fg)]', meeting: 'bg-[var(--color-ampel-amber-fg)] text-white', admin: 'bg-[var(--color-ink-muted)] text-white', learning: 'bg-[var(--color-ampel-green-fg)] text-white', break: 'bg-[var(--color-ink-faint)] text-white', other: 'bg-[var(--color-border-strong)] text-[var(--color-ink)]', }; const BAR_H = 22; // px pro Sub-Spur const GAP = 4; const todayStr = new Intl.DateTimeFormat('en-CA', { timeZone: 'Europe/Berlin' }).format(new Date()); function shiftDay(iso: string, delta: number): string { const d = new Date(`${iso}T12:00:00Z`); d.setUTCDate(d.getUTCDate() + delta); return d.toISOString().slice(0, 10); } function pad(n: number): string { return n.toString().padStart(2, '0'); } function hhmm(ms: number): string { const d = new Date(ms); return `${pad(d.getHours())}:${pad(d.getMinutes())}`; } function floorHourMs(ms: number): number { const d = new Date(ms); d.setMinutes(0, 0, 0); return d.getTime(); } function ceilHourMs(ms: number): number { const d = new Date(ms); if (d.getMinutes() || d.getSeconds() || d.getMilliseconds()) { d.setHours(d.getHours() + 1, 0, 0, 0); } return d.getTime(); } function formatDateLong(iso: string): string { return new Intl.DateTimeFormat('de-DE', { timeZone: 'Europe/Berlin', weekday: 'short', day: 'numeric', month: 'long', year: 'numeric', }).format(new Date(`${iso}T12:00:00Z`)); } type RawEntry = { id: string; start: string; end: string | null; category: string; description: string | null; todoTitle: string | null; }; type Bar = { id: string; startMs: number; endMs: number; running: boolean; category: string; label: string; row: number; }; // Überlappende Blöcke auf parallele Sub-Spuren verteilen. function layout(entries: RawEntry[], nowMs: number): { bars: Bar[]; rows: number } { const sorted = entries .map((e) => { const startMs = new Date(e.start).getTime(); const running = e.end === null; const endMs = running ? nowMs : new Date(e.end!).getTime(); return { id: e.id, startMs, endMs: Math.max(endMs, startMs + 1), running, category: e.category, label: e.todoTitle || e.description || CATEGORY_LABEL[e.category] || 'Eintrag', }; }) .sort((a, b) => a.startMs - b.startMs); const rowEnds: number[] = []; const bars: Bar[] = sorted.map((e) => { let row = rowEnds.findIndex((end) => end <= e.startMs); if (row === -1) { row = rowEnds.length; rowEnds.push(e.endMs); } else { rowEnds[row] = e.endMs; } return { ...e, row }; }); return { bars, rows: Math.max(rowEnds.length, 1) }; } export function TeamViewRoute() { const [date, setDate] = useState(todayStr); const isToday = date === todayStr; const q = trpc.time.teamTimeline.useQuery( { date }, { refetchInterval: isToday ? 30_000 : false }, ); // 1-Sekunden-Tick nur heute (für laufende Balken). const [, setTick] = useState(0); useEffect(() => { if (!isToday) return undefined; const id = window.setInterval(() => setTick((t) => t + 1), 1000); return () => window.clearInterval(id); }, [isToday]); if (q.isPending) return ; if (q.isError || !q.data) return ( void q.refetch()} /> ); const lanes = q.data.lanes; const nowMs = Date.now(); // Achse aus allen Einträgen bestimmen. const allMs: number[] = []; for (const lane of lanes) { for (const e of lane.entries) { allMs.push(new Date(e.start).getTime()); allMs.push(e.end === null ? nowMs : new Date(e.end).getTime()); } } const hasData = allMs.length > 0; const axisStart = hasData ? floorHourMs(Math.min(...allMs)) : floorHourMs(nowMs); let axisEnd = hasData ? ceilHourMs(Math.max(...allMs)) : axisStart + 2 * 3600_000; if (axisEnd - axisStart < 2 * 3600_000) axisEnd = axisStart + 2 * 3600_000; const range = axisEnd - axisStart; const pct = (ms: number) => ((Math.min(Math.max(ms, axisStart), axisEnd) - axisStart) / range) * 100; const hours: number[] = []; for (let t = axisStart; t <= axisEnd; t += 3600_000) hours.push(t); return (

Team-View

Wer hat wann woran gearbeitet — horizontale Tages-Timeline mit parallelen Strängen.

{formatDateLong(date)} setDate(e.target.value || todayStr)} className="ml-2 rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-2 py-1.5 text-sm" /> {!isToday && ( )}
{lanes.length === 0 ? (

Keine Zeiterfassung an diesem Tag.

) : (
{/* Stunden-Achse */}
{hours.map((t) => ( {pad(new Date(t).getHours())} ))}
{/* Spuren */} {lanes.map((lane) => { const { bars, rows } = layout(lane.entries, nowMs); const laneHeight = rows * BAR_H + (rows - 1) * GAP; return (
{lane.name} {bars.length} {bars.length === 1 ? 'Block' : 'Blöcke'}
{/* Stunden-Gitter */} {hours.map((t) => (
))} {/* Blöcke */} {bars.map((b) => { const left = pct(b.startMs); const width = Math.max(pct(b.endMs) - left, 0.7); const dur = formatDurationCompact(Math.round((b.endMs - b.startMs) / 1000)); return (
{b.label}
); })}
); })}
)} {/* Legende */}
{TIME_CATEGORIES.map((c) => ( {c.label} ))}
); }