import React, { useMemo } from 'react'; import { Trash2 } from 'lucide-react'; import { Habit } from '../types'; import { useThemeContext } from '../contexts/ThemeContext'; import { calculateStreak } from '../utils/streakCalculator'; import { getDayIndex } from '../utils/dateUtils'; interface HabitListProps { habits: Habit[]; currentWeek: string[]; daysOfWeek: string[]; onToggleHabit: (id: number, date: string) => Promise; onUpdateHabit: (id: number, name: string) => Promise; onDeleteHabit: (id: number) => Promise; } export function HabitList({ habits, currentWeek, daysOfWeek, onToggleHabit, onUpdateHabit, onDeleteHabit, }: HabitListProps) { const { theme, habitSort, showStreaks } = useThemeContext(); const sortedHabits = useMemo(() => { if (habitSort === 'alphabetical') { return [...habits].sort((a, b) => a.name.localeCompare(b.name)); } return habits; }, [habits, habitSort]); const getDayName = (dateStr: string) => { const date = new Date(dateStr); return daysOfWeek[getDayIndex(date)]; }; return (
{currentWeek.map((dateStr) => ( ))} {showStreaks && ( )} {sortedHabits.map((habit) => { const { currentStreak } = calculateStreak(habit.completedDates); return ( {currentWeek.map((date) => ( ))} {showStreaks && ( )} ); })}
Habit
{getDayName(dateStr)}
{getDayName(dateStr).slice(0, 1)}
{new Date(dateStr).getDate()}
StreakActions
onUpdateHabit(habit.id, e.target.value)} className="bg-transparent border-none focus:outline-none focus:ring-2 focus:ring-gray-300 rounded px-2 w-full" /> {currentStreak} days
); }