import { useEffect, useState } from 'react'; import { FaCalendarCheck, FaClock, FaDumbbell } from 'react-icons/fa'; import type { RecordRoutine, WorkoutStats } from '../types/models'; import { WorkoutService } from '../services/api'; const HomePage = () => { const [stats, setStats] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchStats = async () => { try { setLoading(true); const data = await WorkoutService.getStats(); setStats(data); setError(null); } catch (err) { console.error('Failed to fetch workout stats:', err); setError('Could not load workout statistics. Please try again later.'); } finally { setLoading(false); } }; fetchStats(); }, []); const formatDate = (dateStr: string) => { const date = new Date(dateStr); return new Intl.DateTimeFormat('en-US', { weekday: 'short', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }).format(date); }; const calculateDuration = (start: string, end: string) => { const startTime = new Date(start).getTime(); const endTime = new Date(end).getTime(); const durationMinutes = Math.round((endTime - startTime) / (1000 * 60)); return `${durationMinutes} min`; }; if (loading) { return (

Home

Loading workout data...
); } if (error) { return (

Home

{error}
); } // Display placeholder if no stats if (!stats) { return (

Workout Overview

Welcome to Go Lift!

Start by adding exercises and creating your first workout routine.

); } return (

Workout Overview

{/* Statistics Cards */}
{stats.totalWorkouts}
Total Workouts
{stats.totalMinutes}
Total Minutes
{stats.totalExercises}
Exercises Done
{/* Favorite Data */} {(stats.mostFrequentExercise || stats.mostFrequentRoutine) && (

Your Favorites

{stats.mostFrequentRoutine && (
Most Used Routine:
{stats.mostFrequentRoutine.name} ({stats.mostFrequentRoutine.count}x)
)} {stats.mostFrequentExercise && (
Most Performed Exercise:
{stats.mostFrequentExercise.name} ({stats.mostFrequentExercise.count}x)
)}
)} {/* Recent Workouts */}

Recent Workouts

{stats.recentWorkouts && stats.recentWorkouts.length > 0 ? ( stats.recentWorkouts.map((workout: RecordRoutine) => (

{workout.routine?.name || 'Workout'}

{formatDate(workout.startedAt)}
{workout.endedAt && (
Duration: {calculateDuration(workout.startedAt, workout.endedAt)}
)}
)) ) : (

No workouts recorded yet. Start your fitness journey today!

Record a Workout
)}
); }; export default HomePage;