import { useEffect, useRef, useState } from 'react'; import { Play, Square, Plus } from 'lucide-react'; import { clsx } from 'clsx'; import { toast } from 'sonner'; import { trpc } from '../../lib/trpc'; import { formatDuration } 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_DOT: Record = { focus: 'bg-[var(--color-ink)]', meeting: 'bg-[var(--color-ampel-amber-fg)]', admin: 'bg-[var(--color-ink-muted)]', learning: 'bg-[var(--color-ampel-green-fg)]', break: 'bg-[var(--color-ampel-amber-fg)]', other: 'bg-[var(--color-border-strong)]', }; type Active = { id: string; startedAt: string | Date; category: string; description: string | null; todoId: string | null; todoTitle: string | null; }; function labelOf(a: Active): string { return a.description || a.todoTitle || CATEGORY_LABEL[a.category] || 'Timer'; } function elapsedOf(a: Active): number { return Math.max(0, Math.round((Date.now() - new Date(a.startedAt).getTime()) / 1000)); } export function TimerPill() { const utils = trpc.useUtils(); const nowQuery = trpc.time.now.useQuery(undefined, { refetchInterval: 30_000 }); const todayQuery = trpc.dayEntry.today.useQuery(); const [pickerOpen, setPickerOpen] = useState(false); const [panelOpen, setPanelOpen] = useState(false); const [, setTick] = useState(0); const pickerRef = useRef(null); const active: Active[] = nowQuery.data ?? []; // Sekunden-Tick nur wenn etwas läuft. useEffect(() => { if (active.length === 0) return undefined; const id = window.setInterval(() => setTick((t) => t + 1), 1000); return () => window.clearInterval(id); }, [active.length]); useEffect(() => { if (!pickerOpen && !panelOpen) return undefined; function onClick(e: MouseEvent) { if (pickerRef.current && !pickerRef.current.contains(e.target as Node)) { setPickerOpen(false); setPanelOpen(false); } } function onKey(e: KeyboardEvent) { if (e.key === 'Escape') { setPickerOpen(false); setPanelOpen(false); } } document.addEventListener('mousedown', onClick); document.addEventListener('keydown', onKey); return () => { document.removeEventListener('mousedown', onClick); document.removeEventListener('keydown', onKey); }; }, [pickerOpen, panelOpen]); const invalidate = async () => { await utils.time.now.invalidate(); await utils.time.listToday.invalidate(); }; const startMutation = trpc.time.start.useMutation({ onSuccess: async () => { await invalidate(); setPickerOpen(false); toast('Timer läuft.'); }, }); const count = active.length; const longest = count > 0 ? Math.max(...active.map(elapsedOf)) : 0; return (
{count > 0 ? ( ) : ( )} {pickerOpen && ( t.status === 'open')} onClose={() => setPickerOpen(false)} onStart={(payload) => startMutation.mutate(payload)} pending={startMutation.isPending} /> )} {panelOpen && count > 0 && ( { setPanelOpen(false); setPickerOpen(true); }} onChanged={invalidate} /> )}
); } function TimersPanel({ active, onAdd, onChanged, }: { active: Active[]; onAdd: () => void; onChanged: () => Promise; }) { const stopMutation = trpc.time.stop.useMutation({ onSuccess: onChanged }); return (

Läuft gerade ({active.length})

{active.map((a) => (
{labelOf(a)} {formatDuration(elapsedOf(a))}
))}
); } function QuickPicker({ todos, onClose, onStart, pending, }: { todos: Array<{ id: string; title: string }>; onClose: () => void; onStart: (input: { todoId?: string; category: 'focus' | 'meeting' | 'admin' | 'learning' | 'break' | 'other'; description?: string; }) => void; pending: boolean; }) { const [description, setDescription] = useState(''); return (

Was machst du jetzt?

{todos.length > 0 && (

Deine ToDos heute

{todos.map((t) => ( ))}
)}

Andere Tätigkeit

{TIME_CATEGORIES.filter((c) => c.value !== 'focus').map((c) => ( ))}
setDescription(e.target.value)} placeholder="z. B. Kunde Schmidt — Call" maxLength={200} className="flex-1 rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-2 py-1.5 text-sm" />
); }