'use client'; import { useState, useEffect } from 'react'; import { Box, Typography, Button, Paper, Grid, CircularProgress } from '@mui/material'; import { AppShell } from '@/components/layouts/AppShell/AppShell'; import { ProtectedRoute } from '@/components/common/ProtectedRoute'; import { EmailVerificationBanner } from '@/components/common/EmailVerificationBanner'; import { ErrorBoundary } from '@/components/common/ErrorBoundary'; import { DataErrorFallback } from '@/components/common/ErrorFallbacks'; import { NetworkStatusIndicator } from '@/components/common/NetworkStatusIndicator'; import { StatGridSkeleton } from '@/components/common/LoadingSkeletons'; import { Restaurant, Hotel, BabyChangingStation, Insights, SmartToy, Analytics, } from '@mui/icons-material'; import { motion } from 'framer-motion'; import { useAuth } from '@/lib/auth/AuthContext'; import { useRouter } from 'next/navigation'; import { trackingApi, DailySummary } from '@/lib/api/tracking'; import { childrenApi, Child } from '@/lib/api/children'; import { format } from 'date-fns'; export default function HomePage() { const { user } = useAuth(); const router = useRouter(); const [children, setChildren] = useState([]); const [selectedChild, setSelectedChild] = useState(null); const [dailySummary, setDailySummary] = useState(null); const [loading, setLoading] = useState(true); const familyId = user?.families?.[0]?.familyId; // Load children and daily summary useEffect(() => { const loadData = async () => { if (!familyId) { setLoading(false); return; } try { // Load children const childrenData = await childrenApi.getChildren(familyId); setChildren(childrenData); if (childrenData.length > 0) { const firstChild = childrenData[0]; setSelectedChild(firstChild); // Load today's summary for first child const today = format(new Date(), 'yyyy-MM-dd'); const summary = await trackingApi.getDailySummary(firstChild.id, today); setDailySummary(summary); } } catch (error) { console.error('Failed to load data:', error); } finally { setLoading(false); } }; loadData(); }, [familyId]); const quickActions = [ { icon: , label: 'Feeding', color: '#FFB6C1', path: '/track/feeding' }, { icon: , label: 'Sleep', color: '#B6D7FF', path: '/track/sleep' }, { icon: , label: 'Diaper', color: '#FFE4B5', path: '/track/diaper' }, { icon: , label: 'AI Assistant', color: '#FFD3B6', path: '/ai-assistant' }, { icon: , label: 'Analytics', color: '#D4B5FF', path: '/analytics' }, ]; const formatSleepHours = (minutes: number) => { const hours = Math.floor(minutes / 60); const mins = minutes % 60; if (hours > 0 && mins > 0) { return `${hours}h ${mins}m`; } else if (hours > 0) { return `${hours}h`; } else { return `${mins}m`; } }; return ( Welcome Back{user?.name ? `, ${user.name}` : ''}! 👋 Track your child's activities and get AI-powered insights {/* Quick Actions */} Quick Actions {quickActions.map((action, index) => ( router.push(action.path)} sx={{ p: 3, textAlign: 'center', cursor: 'pointer', bgcolor: action.color, color: 'white', transition: 'transform 0.2s', '&:hover': { transform: 'scale(1.05)', }, }} > {action.icon} {action.label} ))} {/* Today's Summary */} Today's Summary{selectedChild ? ` - ${selectedChild.name}` : ''} } > {loading ? ( ) : ( {!dailySummary ? ( {children.length === 0 ? 'Add a child to start tracking' : 'No activities tracked today'} ) : ( {dailySummary.feedingCount || 0} Feedings {dailySummary.sleepTotalMinutes ? formatSleepHours(dailySummary.sleepTotalMinutes) : '0m'} Sleep {dailySummary.diaperCount || 0} Diapers )} )} {/* Next Predicted Activity */} Next Predicted Activity Nap time in 45 minutes Based on your child's sleep patterns ); }