import React, { useState, useEffect } from 'react'; import Navbar from './components/Navbar'; import Tracker from './components/Tracker'; import History from './components/History'; import Stats from './components/Stats'; import AICoach from './components/AICoach'; import Plans from './components/Plans'; import Login from './components/Login'; import Profile from './components/Profile'; import { TabView, WorkoutSession, WorkoutSet, WorkoutPlan, User, Language } from './types'; import { getSessions, saveSession, deleteSession } from './services/storage'; import { getCurrentUserProfile, getMe } from './services/auth'; import { getSystemLanguage } from './services/i18n'; function App() { const [currentUser, setCurrentUser] = useState(null); const [currentTab, setCurrentTab] = useState('TRACK'); const [language, setLanguage] = useState('en'); const [sessions, setSessions] = useState([]); const [activeSession, setActiveSession] = useState(null); const [activePlan, setActivePlan] = useState(null); useEffect(() => { // Set initial language setLanguage(getSystemLanguage()); // Restore session const restoreSession = async () => { const token = localStorage.getItem('token'); if (token) { const res = await getMe(); if (res.success && res.user) { setCurrentUser(res.user); } else { localStorage.removeItem('token'); } } }; restoreSession(); }, []); useEffect(() => { const loadSessions = async () => { if (currentUser) { const s = await getSessions(currentUser.id); setSessions(s); // Profile fetch is skipped for now as it returns undefined } else { setSessions([]); } }; loadSessions(); }, [currentUser]); const handleLogin = (user: User) => { setCurrentUser(user); setCurrentTab('TRACK'); }; const handleLogout = () => { setCurrentUser(null); setActiveSession(null); setActivePlan(null); }; const handleLanguageChange = (lang: Language) => { setLanguage(lang); }; const handleUserUpdate = (updatedUser: User) => { setCurrentUser(updatedUser); }; const handleStartSession = (plan?: WorkoutPlan, startWeight?: number) => { if (!currentUser) return; // Get latest weight from profile or default const profile = getCurrentUserProfile(currentUser.id); // Use provided startWeight, or profile weight, or default 70 const currentWeight = startWeight || profile?.weight || 70; const newSession: WorkoutSession = { id: crypto.randomUUID(), startTime: Date.now(), userBodyWeight: currentWeight, sets: [], planId: plan?.id, planName: plan?.name }; setActivePlan(plan || null); setActiveSession(newSession); setCurrentTab('TRACK'); }; const handleEndSession = () => { if (activeSession && currentUser) { const finishedSession = { ...activeSession, endTime: Date.now() }; saveSession(currentUser.id, finishedSession); setSessions(prev => [finishedSession, ...prev]); setActiveSession(null); setActivePlan(null); } }; const handleAddSet = (set: WorkoutSet) => { if (activeSession) { setActiveSession(prev => { if (!prev) return null; return { ...prev, sets: [...prev.sets, set] }; }); } }; const handleRemoveSetFromActive = (setId: string) => { if (activeSession) { setActiveSession(prev => { if (!prev) return null; return { ...prev, sets: prev.sets.filter(s => s.id !== setId) }; }); } }; const handleUpdateSession = (updatedSession: WorkoutSession) => { if (!currentUser) return; saveSession(currentUser.id, updatedSession); setSessions(prev => prev.map(s => s.id === updatedSession.id ? updatedSession : s)); }; const handleDeleteSession = (sessionId: string) => { if (!currentUser) return; deleteSession(currentUser.id, sessionId); setSessions(prev => prev.filter(s => s.id !== sessionId)); }; if (!currentUser) { return ; } return (
{/* Desktop Navigation Rail (Left) */} {/* Main Content Area */}
{currentTab === 'TRACK' && ( )} {currentTab === 'PLANS' && ( )} {currentTab === 'HISTORY' && ( )} {currentTab === 'STATS' && } {currentTab === 'AI_COACH' && } {currentTab === 'PROFILE' && ( )}
{/* Mobile Navigation (rendered inside Navbar component, fixed to bottom) */}
); } export default App;