import { useEffect, useState, useCallback } from 'react' import { CalendarDays } from 'lucide-react' import type { Task } from '../types' import { fetchTasks, updateTask } from '../api' import { useTaskDrawer } from '../components/TaskDrawer' import TaskRow from '../components/TaskRow' function startOfDay(d: Date) { const x = new Date(d); x.setHours(0, 0, 0, 0); return x } export default function TodayView() { const { openTask, changeVersion } = useTaskDrawer() const [tasks, setTasks] = useState([]) const [error, setError] = useState(null) const load = useCallback(() => { fetchTasks({ view: 'today' }).then(setTasks).catch(e => setError(e.message)) }, []) useEffect(() => { load() }, [load, changeVersion]) async function toggle(t: Task) { await updateTask(t.id, { status: t.status === 'done' ? 'todo' : 'done' }) load() } const now = new Date() const todayStart = startOfDay(now) const tomorrowStart = new Date(todayStart); tomorrowStart.setDate(tomorrowStart.getDate() + 1) const overdue = tasks.filter(t => t.due_at && new Date(t.due_at) < todayStart) const today = tasks.filter(t => t.due_at && new Date(t.due_at) >= todayStart && new Date(t.due_at) < tomorrowStart) const upcoming = tasks.filter(t => t.due_at && new Date(t.due_at) >= tomorrowStart) const groups: Array<{ key: string; title: string; items: Task[]; overdue?: boolean }> = [ { key: 'overdue', title: 'Overdue', items: overdue, overdue: true }, { key: 'today', title: 'Today', items: today }, { key: 'upcoming', title: 'Next 7 days', items: upcoming }, ] return (

Today

{error &&
{error}
} {tasks.length === 0 && (
Nothing due in the next 7 days. Enjoy the calm. ☕
)} {groups.map(g => g.items.length > 0 && (
{g.title} · {g.items.length}
{g.items.map(t => ( openTask(t.id)} /> ))}
))}
) }