'use client';
import { useState, useEffect } from 'react';
import {
Box,
Typography,
Paper,
Grid,
Card,
CardContent,
Button,
CircularProgress,
Tabs,
Tab,
Alert,
} from '@mui/material';
import { AppShell } from '@/components/layouts/AppShell/AppShell';
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
import { ErrorBoundary } from '@/components/common/ErrorBoundary';
import { ChartErrorFallback } from '@/components/common/ErrorFallbacks';
import { StatGridSkeleton, ChartSkeleton } from '@/components/common/LoadingSkeletons';
import {
TrendingUp,
Hotel,
Restaurant,
BabyChangingStation,
Download,
} from '@mui/icons-material';
import { motion } from 'framer-motion';
import apiClient from '@/lib/api/client';
import WeeklySleepChart from '@/components/analytics/WeeklySleepChart';
import FeedingFrequencyGraph from '@/components/analytics/FeedingFrequencyGraph';
import GrowthCurve from '@/components/analytics/GrowthCurve';
import PatternInsights from '@/components/analytics/PatternInsights';
interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}
function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;
return (
{value === index && {children}}
);
}
export default function AnalyticsPage() {
const [tabValue, setTabValue] = useState(0);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
const [insights, setInsights] = useState(null);
useEffect(() => {
fetchAnalytics();
}, []);
const fetchAnalytics = async () => {
try {
setIsLoading(true);
const response = await apiClient.get('/api/v1/analytics/insights');
setInsights(response.data.data);
} catch (err: any) {
console.error('Failed to fetch analytics:', err);
setError(err.response?.data?.message || 'Failed to load analytics');
} finally {
setIsLoading(false);
}
};
const handleExportReport = async () => {
try {
const response = await apiClient.get('/api/v1/analytics/reports/weekly', {
responseType: 'blob',
});
const blob = new Blob([response.data], { type: 'application/pdf' });
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `weekly-report-${new Date().toISOString().split('T')[0]}.pdf`;
link.click();
window.URL.revokeObjectURL(url);
} catch (err) {
console.error('Failed to export report:', err);
}
};
const handleTabChange = (event: React.SyntheticEvent, newValue: number) => {
setTabValue(newValue);
};
if (isLoading) {
return (
Analytics & Insights
);
}
return (
{/* Header */}
Analytics & Insights 📊
Track patterns and get personalized insights
}
onClick={handleExportReport}
sx={{ borderRadius: 3 }}
>
Export Report
{error && (
{error}
)}
{/* Summary Cards */}
{insights?.sleep?.averageHours || '0'}h
Avg Sleep (7 days)
{insights?.feeding?.averagePerDay || '0'}
Avg Feedings (7 days)
{insights?.diaper?.averagePerDay || '0'}
Avg Diapers (7 days)
{/* Tabs */}
}>
}>
}>
);
}