'use client'; import { useEffect, useState } from 'react'; import { Calendar, ListTodo } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { useRouter } from 'next/navigation'; interface UpcomingItem { id: string; title: string; due_date: string; priority?: string; status?: string; name?: string; target_date?: string; color?: string; } export function UpcomingCalendarWidget() { const [tasks, setTasks] = useState([]); const [projects, setProjects] = useState([]); const [loading, setLoading] = useState(true); const router = useRouter(); useEffect(() => { fetchUpcoming(); }, []); async function fetchUpcoming() { try { const res = await fetch('/api/tasks?perPage=10&sort=due_date'); if (res.ok) { const data = await res.json(); const now = new Date(); const nextWeek = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000); const upcoming = (data.items || []).filter((t: any) => { if (!t.due_date) return false; const d = new Date(t.due_date); return d >= now && d <= nextWeek; }); setTasks(upcoming); } } catch {} finally { setLoading(false); } } function formatDate(dateStr: string) { const d = new Date(dateStr); const today = new Date(); const tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); if (d.toDateString() === today.toDateString()) return 'Today'; if (d.toDateString() === tomorrow.toDateString()) return 'Tomorrow'; return d.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' }); } return (
{tasks.length} due
{loading ? (

Loading...

) : tasks.length === 0 ? (

No upcoming due dates

) : (
{tasks.slice(0, 7).map((task) => (
router.push('/tasks')} > {task.title} {formatDate(task.due_date!)}
))}
)}
); }