import { useState } from 'react'; import { useNavigate } from 'react-router'; import { trpc } from '../lib/trpc'; import { voice } from '../lib/voice'; import { Card } from '../components/ui/Card'; import { Button } from '../components/ui/Button'; import { SkeletonCard } from '../components/ui/SkeletonCard'; import { toast } from 'sonner'; import { X, Plus, RotateCw, BellRing, Play, Clock } from 'lucide-react'; import { TimelineSection, ManualDialog } from '../components/time/TimelineSection'; /** Aktuelle Stunde in Europe/Berlin (0–23). */ function berlinHourNow(): number { const h = new Intl.DateTimeFormat('en-GB', { timeZone: 'Europe/Berlin', hour: '2-digit', hour12: false, }).format(new Date()); return parseInt(h, 10); } export function HeuteRoute() { const navigate = useNavigate(); const today = trpc.dayEntry.today.useQuery(); if (today.isPending) return ; if (today.data?.entry) { return ; } return navigate('/puls')} />; } function CheckInForm({ onDone }: { onDone: () => void }) { const carryQuery = trpc.dayEntry.pendingCarryOver.useQuery(); const [todos, setTodos] = useState<{ title: string; carried?: boolean }[]>([{ title: '' }]); const [prefilledFromCarry, setPrefilledFromCarry] = useState(false); // Wenn Carryover-ToDos geladen sind und das Formular noch leer ist, vorbefüllen. if ( !prefilledFromCarry && carryQuery.data && carryQuery.data.length > 0 && todos.length === 1 && !todos[0]!.title ) { setPrefilledFromCarry(true); setTodos([...carryQuery.data.map((c) => ({ title: c.title, carried: true })), { title: '' }]); } const utils = trpc.useUtils(); const mutation = trpc.dayEntry.checkIn.useMutation({ onSuccess: async (result) => { const msg = result.carriedOver > 0 ? `Locked in. ${result.carriedOver} offene ToDos vom Vortag übernommen.` : voice.toast.checkedIn; toast(msg); await utils.dayEntry.today.invalidate(); await utils.dayEntry.pendingCarryOver.invalidate(); onDone(); }, }); function updateAt(i: number, value: string) { setTodos((arr) => arr.map((t, idx) => (idx === i ? { ...t, title: value } : t))); } function addRow() { setTodos((arr) => [...arr, { title: '' }]); } function removeAt(i: number) { setTodos((arr) => arr.filter((_, idx) => idx !== i)); } function handleKey(e: React.KeyboardEvent, i: number) { if (e.key === 'Enter') { e.preventDefault(); if (i === todos.length - 1 && todos[i]?.title.trim()) { addRow(); // Fokus auf neue Zeile in nächstem Tick setTimeout(() => { const inputs = document.querySelectorAll('input[data-todo-input]'); inputs[i + 1]?.focus(); }, 0); } } } function handleSubmit(e: React.FormEvent) { e.preventDefault(); const filtered = todos.filter((t) => t.title.trim().length > 0); if (filtered.length === 0) { toast('Mindestens eine ToDo brauchst du.'); return; } mutation.mutate({ todos: filtered.map((t) => ({ title: t.title.trim() })) }); } return ( Heute {voice.hint.howIsItGoing} Was nimmst du dir heute vor? {carryQuery.data && carryQuery.data.length > 0 && ( {carryQuery.data.length} offene ToDos vom Vortag wurden für dich vorgemerkt. Behalte, ändere oder lösche sie. )} {todos.map((t, i) => ( {t.carried && ( )} updateAt(i, e.target.value)} onKeyDown={(e) => handleKey(e, i)} placeholder="ToDo …" maxLength={200} data-todo-input ref={(el) => { if (i === 0 && el && el.value === '') el.focus(); }} className="flex-1 rounded-[var(--radius-md)] border border-[var(--color-border)] bg-white px-3 py-2 text-sm focus:border-[var(--color-ink)] focus:outline-none" /> {todos.length > 1 && ( removeAt(i)} aria-label="ToDo entfernen" className="rounded-[var(--radius-md)] p-2 text-[var(--color-ink-muted)] hover:bg-black/5" > )} ))} Weitere history.back()}> Abbrechen {voice.cta.checkIn} ); } function TodoRow({ todo, allTodos, }: { todo: { id: string; title: string; status: 'open' | 'done' | 'dropped' }; allTodos: Array<{ id: string; title: string }>; }) { const utils = trpc.useUtils(); const now = trpc.time.now.useQuery(); const [retroOpen, setRetroOpen] = useState(false); const startMutation = trpc.time.start.useMutation({ onSuccess: async () => { await utils.time.now.invalidate(); await utils.time.listToday.invalidate(); }, }); const stopMutation = trpc.time.stop.useMutation({ onSuccess: async () => { await utils.time.now.invalidate(); await utils.time.listToday.invalidate(); }, }); const isRunningHere = now.data?.todoId === todo.id; const dotClass = todo.status === 'done' ? 'h-2 w-2 rounded-full bg-[var(--color-ampel-green-fg)]' : todo.status === 'dropped' ? 'h-2 w-2 rounded-full bg-[var(--color-ink-faint)]' : 'h-2 w-2 rounded-full bg-[var(--color-border-strong)]'; return ( {todo.title} {todo.status !== 'done' && todo.status !== 'dropped' && ( isRunningHere ? stopMutation.mutate() : startMutation.mutate({ todoId: todo.id, category: 'focus' }) } aria-label={isRunningHere ? 'Timer stoppen' : 'Timer für diesen ToDo starten'} className={ isRunningHere ? 'inline-flex h-7 items-center gap-1 rounded-full bg-[var(--color-ampel-red-bg)] px-2 text-xs font-medium text-[var(--color-ampel-red-fg)]' : 'inline-flex h-7 w-7 items-center justify-center rounded-full text-[var(--color-ink-muted)] opacity-0 hover:bg-black/5 hover:text-[var(--color-ink)] group-hover:opacity-100' } > {isRunningHere ? ( <> läuft > ) : ( )} )} {todo.status !== 'done' && todo.status !== 'dropped' && !isRunningHere && ( setRetroOpen(true)} aria-label="Zeit nachtragen" title="Zeit nachtragen" className="inline-flex h-7 w-7 items-center justify-center rounded-full text-[var(--color-ink-muted)] opacity-0 hover:bg-black/5 hover:text-[var(--color-ink)] group-hover:opacity-100" > )} {retroOpen && ( setRetroOpen(false)} /> )} ); } function EinstempeltView({ todos, entry, }: { todos: Array<{ id: string; title: string; status: 'open' | 'done' | 'dropped' }>; entry: { status: 'begun' | 'full' | 'absent' }; }) { // Reflexion ist „fällig", wenn nach 18:00 Berlin und noch nicht abgeschlossen. const reflectionDue = entry.status !== 'full' && berlinHourNow() >= 18; // CTA bleibt den ganzen Tag sichtbar — der Eintrag ist bis 00:00 (Berlin) änderbar. const ctaLabel = entry.status === 'full' ? 'Reflexion aktualisieren' : voice.cta.reflect; const subtitle = entry.status === 'full' ? 'Tag abgeschlossen. Bis Mitternacht (Berlin) noch änderbar.' : 'Locked in. Heute Abend reflektieren.'; return ( Heute {subtitle} {reflectionDue && ( Reflexion fällig Es ist nach 18:00 — schließ den Tag noch ab. (location.href = '/reflexion')}>Jetzt reflektieren )} {todos.map((t) => ( ))} (location.href = '/reflexion')}>{ctaLabel} ); }
{voice.hint.howIsItGoing}
Was nimmst du dir heute vor?
{carryQuery.data.length} offene ToDos vom Vortag wurden für dich vorgemerkt. Behalte, ändere oder lösche sie.
{subtitle}