import { useEffect, useState } from 'react'; import GarminSync from '../components/garmin/GarminSync'; import WorkoutChart from '../components/analysis/WorkoutCharts'; import PlanTimeline from '../components/plans/PlanTimeline'; import { useAuth } from '../context/AuthContext'; import LoadingSpinner from '../components/LoadingSpinner'; const Dashboard = () => { const { apiKey, loading: apiLoading } = useAuth(); const isBuildTime = typeof window === 'undefined'; const [recentWorkouts, setRecentWorkouts] = useState([]); const [currentPlan, setCurrentPlan] = useState(null); const [stats, setStats] = useState({ totalWorkouts: 0, totalDistance: 0 }); const [healthStatus, setHealthStatus] = useState(null); const [localLoading, setLocalLoading] = useState(true); const [error, setError] = useState(''); useEffect(() => { const fetchDashboardData = async () => { try { const [workoutsRes, planRes, statsRes, healthRes] = await Promise.all([ fetch(`${process.env.REACT_APP_API_URL}/api/workouts?limit=3`, { headers: { 'X-API-Key': apiKey } }), fetch(`${process.env.REACT_APP_API_URL}/api/plans/active`, { headers: { 'X-API-Key': apiKey } }), fetch(`${process.env.REACT_APP_API_URL}/api/stats`, { headers: { 'X-API-Key': apiKey } }), fetch(`${process.env.REACT_APP_API_URL}/api/health`, { headers: { 'X-API-Key': apiKey } }) ]); const errors = []; if (!workoutsRes.ok) errors.push('Failed to fetch workouts'); if (!planRes.ok) errors.push('Failed to fetch plan'); if (!statsRes.ok) errors.push('Failed to fetch stats'); if (!healthRes.ok) errors.push('Failed to fetch health status'); if (errors.length > 0) throw new Error(errors.join(', ')); const [workoutsData, planData, statsData, healthData] = await Promise.all([ workoutsRes.json(), planRes.json(), statsRes.json(), healthRes.json() ]); setRecentWorkouts(workoutsData.workouts || []); setCurrentPlan(planData); setStats(statsData.workouts || { totalWorkouts: 0, totalDistance: 0 }); setHealthStatus(healthData); } catch (err) { setError(err.message); } finally { setLocalLoading(false); } }; fetchDashboardData(); }, [apiKey]); if (isBuildTime) { return (
Loading dashboard data...
{stats.totalWorkouts}
{totalDistanceKm} km
{currentPlan ? `v${currentPlan.version}` : 'None'}
{workout.activity_type}
{(workout.distance_m / 1000).toFixed(1)} km
{Math.round(workout.duration_seconds / 60)} mins
{workout.description}