import React, { useState, useEffect, useMemo } from 'react'; import { initializeApp } from 'firebase/app'; import { getAuth, onAuthStateChanged, signInAnonymously, signInWithCustomToken } from 'firebase/auth'; import { getFirestore, doc, setDoc, onSnapshot } from 'firebase/firestore'; import { Moon, Sun, ChevronLeft, ChevronRight, RotateCcw, CheckCircle2 } from 'lucide-react'; // --- Configuração Firebase --- const firebaseConfig = JSON.parse(__firebase_config); const app = initializeApp(firebaseConfig); const auth = getAuth(app); const db = getFirestore(app); const appId = typeof __app_id !== 'undefined' ? __app_id : 'habit-tracker-adhd-contrast'; export default function App() { const [user, setUser] = useState(null); const [darkMode, setDarkMode] = useState(true); const [selectedDate, setSelectedDate] = useState(new Date(2026, 0, 1)); const [habitSettings, setHabitSettings] = useState({ 1: { title: 'Manhã', task: 'Tarefa 1' }, 2: { title: 'Tarde', task: 'Tarefa 2' }, 3: { title: 'Noite', task: 'Tarefa 3' } }); const [habitLogs, setHabitLogs] = useState({}); const currentYear = selectedDate.getFullYear(); const currentMonth = selectedDate.getMonth(); useEffect(() => { const initAuth = async () => { if (typeof __initial_auth_token !== 'undefined' && __initial_auth_token) { await signInWithCustomToken(auth, __initial_auth_token); } else { await signInAnonymously(auth); } }; initAuth(); const unsubscribe = onAuthStateChanged(auth, setUser); return () => unsubscribe(); }, []); useEffect(() => { if (!user) return; const settingsDoc = doc(db, 'artifacts', appId, 'users', user.uid, 'settings', 'habits'); const unsubSettings = onSnapshot(settingsDoc, (snapshot) => { if (snapshot.exists()) setHabitSettings(snapshot.data()); }); const monthId = `${currentYear}-${currentMonth + 1}`; const logsDoc = doc(db, 'artifacts', appId, 'users', user.uid, 'logs', monthId); const unsubLogs = onSnapshot(logsDoc, (snapshot) => { if (snapshot.exists()) { setHabitLogs(snapshot.data()); } else { setHabitLogs({}); } }); return () => { unsubSettings(); unsubLogs(); }; }, [user, currentYear, currentMonth]); const saveSettings = async (newSettings) => { if (!user) return; await setDoc(doc(db, 'artifacts', appId, 'users', user.uid, 'settings', 'habits'), newSettings); }; const toggleDay = async (habitId, day) => { if (!user) return; const monthId = `${currentYear}-${currentMonth + 1}`; const currentList = habitLogs[habitId] || []; const newList = currentList.includes(day) ? currentList.filter(d => d !== day) : [...currentList, day]; await setDoc(doc(db, 'artifacts', appId, 'users', user.uid, 'logs', monthId), { ...habitLogs, [habitId]: newList }); }; const resetHabitMonth = async (habitId) => { if (!user || !window.confirm("Zerar este mês?")) return; const monthId = `${currentYear}-${currentMonth + 1}`; await setDoc(doc(db, 'artifacts', appId, 'users', user.uid, 'logs', monthId), { ...habitLogs, [habitId]: [] }); }; const daysInMonth = useMemo(() => new Date(currentYear, currentMonth + 1, 0).getDate(), [currentYear, currentMonth]); const firstDayOfWeek = useMemo(() => new Date(currentYear, currentMonth, 1).getDay(), [currentYear, currentMonth]); const monthNames = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"]; const weekDays = ["D", "S", "T", "Q", "Q", "S", "S"]; if (!user) return (
Sincronizando...
); return (

Foco Total

{currentYear} {monthNames[currentMonth]}
{monthNames.map((m, idx) => ( ))}
{[1, 2, 3].map((id) => { const completedDays = habitLogs[id] || []; const completionRate = Math.round((completedDays.length / daysInMonth) * 100); return (
{ const updated = { ...habitSettings, [id]: { ...habitSettings[id], title: e.target.value } }; setHabitSettings(updated); saveSettings(updated); }} className={`w-full bg-transparent border-none text-[11px] font-black uppercase tracking-[0.3em] focus:ring-0 p-0 mb-2 ${darkMode ? 'text-emerald-400' : 'text-slate-400'}`} /> { const updated = { ...habitSettings, [id]: { ...habitSettings[id], task: e.target.value } }; setHabitSettings(updated); saveSettings(updated); }} className={`w-full bg-transparent border-none text-2xl font-black focus:ring-0 p-0 ${darkMode ? 'text-white' : 'text-blue-600'}`} />
{weekDays.map((wd, idx) => (
{wd}
))}
{/* Espaços vazios para alinhar o primeiro dia do mês */} {Array.from({ length: firstDayOfWeek }).map((_, i) => (
))} {/* Dias do mês */} {Array.from({ length: daysInMonth }, (_, i) => i + 1).map((day) => { const isDone = completedDays.includes(day); return ( ); })}
Taxa de Sucesso
{completionRate}%
); })}

2026 Habit Tracking Systems