'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import api, { Task } from '@/lib/api'; import { PlusIcon, CheckIcon, XIcon, ClockIcon, TrashIcon } from '@/components/ui/Icons'; function formatDateTime(dtStr: string): string { try { const dt = new Date(dtStr); const now = new Date(); const tomorrow = new Date(now); tomorrow.setDate(now.getDate() + 1); const timeStr = dt.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' }); if (dt.toDateString() === now.toDateString()) return `Today at ${timeStr}`; if (dt.toDateString() === tomorrow.toDateString()) return `Tomorrow at ${timeStr}`; return dt.toLocaleDateString([], { weekday: 'short', month: 'short', day: 'numeric' }) + ` at ${timeStr}`; } catch { return dtStr; } } function isPast(dtStr: string): boolean { try { return new Date(dtStr) < new Date(); } catch { return false; } } export default function TasksPage() { const router = useRouter(); const [tasks, setTasks] = useState([]); const [isLoading, setIsLoading] = useState(true); const [showCompleted, setShowCompleted] = useState(false); const loadTasks = async (status: string) => { try { const data = await api.tasks.list(status); setTasks(data); } catch (err) { console.error('Failed to load tasks:', err); } finally { setIsLoading(false); } }; useEffect(() => { loadTasks(showCompleted ? 'all' : 'pending'); }, [showCompleted]); const handleMarkDone = async (task: Task) => { try { await api.tasks.update(task.id, { status: 'completed' }); setTasks(prev => prev.filter(t => t.id !== task.id)); } catch (err) { console.error('Failed to update task:', err); } }; const handleCancel = async (task: Task) => { try { await api.tasks.update(task.id, { status: 'cancelled' }); setTasks(prev => prev.filter(t => t.id !== task.id)); } catch (err) { console.error('Failed to cancel task:', err); } }; const handleDelete = async (task: Task) => { if (!confirm(`Delete "${task.title}"?`)) return; try { await api.tasks.delete(task.id); setTasks(prev => prev.filter(t => t.id !== task.id)); } catch (err) { console.error('Failed to delete task:', err); } }; const activeTasks = tasks.filter(t => t.status === 'pending' || t.status === 'notified'); const doneTasks = tasks.filter(t => t.status === 'completed' || t.status === 'cancelled'); return (

Tasks

Add Task
{isLoading ? (
) : activeTasks.length === 0 ? (

No upcoming tasks

Add your first task
) : (
{activeTasks.map(task => (

{task.title}

{task.description && (

{task.description}

)}
{formatDateTime(task.scheduled_datetime)} {task.status === 'notified' && ( Notified )} {task.reminder_minutes_before > 0 && ( +{task.reminder_minutes_before}m reminder )}
))}
)} {/* Show completed toggle */} {!isLoading && ( )} {/* Completed tasks */} {showCompleted && doneTasks.length > 0 && (

Completed / Cancelled

{doneTasks.map(task => (

{task.title}

{formatDateTime(task.scheduled_datetime)}

))}
)}
); }