'use client'; import { useEffect, useState } from 'react'; import { Flame } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Checkbox } from '@/components/ui/checkbox'; interface Habit { id: string; name: string; current_streak: number; logged_today: boolean; } export function HabitChecklistWidget() { const [habits, setHabits] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { fetchHabits(); }, []); async function fetchHabits() { try { const response = await fetch('/api/habits'); if (response.ok) { const data = await response.json(); setHabits(data.items || []); } } catch (error) { console.error('Failed to fetch habits:', error); } finally { setLoading(false); } } async function toggleHabit(id: string) { try { await fetch(`/api/habits/${id}/logs`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}), }); fetchHabits(); } catch (error) { console.error('Failed to log habit:', error); } } const completedCount = habits.filter((h) => h.logged_today).length; return (
{completedCount}/{habits.length} done
{loading ? (

Loading...

) : habits.length === 0 ? (

No habits tracked

) : (
{habits.slice(0, 5).map((habit) => (
toggleHabit(habit.id)} aria-label={`Mark "${habit.name}" as ${habit.logged_today ? 'incomplete' : 'complete'}`} /> {habit.current_streak > 0 && ( 🔥 {habit.current_streak} )}
))}
)}
); }