import React, { useState, useEffect } from 'react'; import { Plus, CalendarIcon, SettingsIcon, Moon, Sun, ChevronLeft, ChevronRight } from 'lucide-react'; import { HabitList } from './components/HabitList'; import { Calendar } from './components/Calendar'; import { Habit } from './types'; export default function HabitTracker() { const [habits, setHabits] = useState([]); const [newHabit, setNewHabit] = useState(''); const [currentWeek, setCurrentWeek] = useState([]); const [activeView, setActiveView] = useState<'habits' | 'calendar' | 'settings'>('habits'); const [darkMode, setDarkMode] = useState(() => localStorage.getItem('darkMode') === 'true'); const [currentMonth, setCurrentMonth] = useState(new Date()); const daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; useEffect(() => { fetchHabits(); setCurrentWeek(getCurrentWeekDates()); }, []); useEffect(() => { if (darkMode) { document.documentElement.classList.add('dark'); } else { document.documentElement.classList.remove('dark'); } localStorage.setItem('darkMode', darkMode.toString()); }, [darkMode]); const fetchHabits = async () => { try { const response = await fetch('http://localhost:5000/api/habits'); const data = await response.json(); setHabits(data); } catch (error) { console.error('Error fetching habits:', error); setHabits([]); // Set empty array on error } }; const addHabit = async (e: React.FormEvent) => { e.preventDefault(); if (newHabit.trim()) { try { const response = await fetch('http://localhost:5000/api/habits', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: newHabit }), }); if (response.ok) { setNewHabit(''); fetchHabits(); } } catch (error) { console.error('Error adding habit:', error); } } }; const toggleHabit = async (id: number, date: string) => { try { await fetch(`http://localhost:5000/api/habits/${id}/toggle`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ date }), }); fetchHabits(); } catch (error) { console.error('Error toggling habit:', error); } }; const updateHabit = async (id: number, name: string) => { try { await fetch(`http://localhost:5000/api/habits/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }), }); fetchHabits(); } catch (error) { console.error('Error updating habit:', error); } }; const deleteHabit = async (id: number) => { try { await fetch(`http://localhost:5000/api/habits/${id}`, { method: 'DELETE', }); fetchHabits(); } catch (error) { console.error('Error deleting habit:', error); } }; const getCurrentWeekDates = () => { const now = new Date(); const dayOfWeek = now.getDay(); const diff = now.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1); const monday = new Date(now.setDate(diff)); return Array.from({ length: 7 }, (_, i) => { const date = new Date(monday); date.setDate(monday.getDate() + i); return date.toISOString().split('T')[0]; }); }; const changeWeek = (direction: 'prev' | 'next') => { const firstDay = new Date(currentWeek[0]); const newFirstDay = new Date(firstDay.setDate(firstDay.getDate() + (direction === 'prev' ? -7 : 7))); setCurrentWeek(Array.from({ length: 7 }, (_, i) => { const date = new Date(newFirstDay); date.setDate(newFirstDay.getDate() + i); return date.toISOString().split('T')[0]; })); }; const changeMonth = (direction: 'prev' | 'next') => { setCurrentMonth(prevMonth => { const newMonth = new Date(prevMonth); newMonth.setMonth(newMonth.getMonth() + (direction === 'prev' ? -1 : 1)); return newMonth; }); }; const getDaysInMonth = (year: number, month: number) => { return new Date(year, month + 1, 0).getDate(); }; const getCompletedHabitsForDate = (date: string) => { return habits.filter(habit => habit.completedDates.includes(date)); }; const handleUpdateStreak = async (id: number, newStreak: number) => { // Prevent negative streaks if (newStreak < 0) return; // Update in database await db.habits.update(id, { manualStreak: newStreak }); // Update state setHabits(habits.map(habit => habit.id === id ? { ...habit, manualStreak: newStreak } : habit )); }; return (
{activeView === 'habits' && (
setNewHabit(e.target.value)} placeholder="Add a new habit" className="flex-grow px-4 py-2 border rounded-lg dark:bg-gray-800 dark:border-gray-700 dark:text-white" />

Weekly Progress

)} {activeView === 'calendar' && ( )} {activeView === 'settings' && (

Settings

Dark Mode
)}
); }