'use client';
import { useState, useEffect } from 'react';
import {
Box,
Typography,
Tabs,
Tab,
Select,
MenuItem,
FormControl,
InputLabel,
CircularProgress,
Alert,
Grid,
} from '@mui/material';
import { Timeline, TrendingUp, Assessment } from '@mui/icons-material';
import { childrenApi, Child } from '@/lib/api/children';
import { analyticsApi, PatternInsights, PredictionInsights } from '@/lib/api/analytics';
import { InsightsDashboard } from './InsightsDashboard';
import PredictionsCard from './PredictionsCard';
import GrowthSpurtAlert from './GrowthSpurtAlert';
import { motion } from 'framer-motion';
interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}
function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;
return (
{value === index && {children}}
);
}
export function UnifiedInsightsDashboard() {
const [children, setChildren] = useState([]);
const [selectedChildId, setSelectedChildId] = useState('');
const [tabValue, setTabValue] = useState(0);
const [loading, setLoading] = useState(true);
const [insights, setInsights] = useState(null);
const [predictions, setPredictions] = useState(null);
const [insightsLoading, setInsightsLoading] = useState(false);
const [predictionsLoading, setPredictionsLoading] = useState(false);
const [days, setDays] = useState(7);
useEffect(() => {
loadChildren();
}, []);
useEffect(() => {
if (selectedChildId) {
loadInsights();
loadPredictions();
}
}, [selectedChildId, days]);
const loadChildren = async () => {
try {
const data = await childrenApi.getChildren();
setChildren(data);
if (data.length > 0 && !selectedChildId) {
setSelectedChildId(data[0].id);
}
} catch (error) {
console.error('Failed to load children:', error);
} finally {
setLoading(false);
}
};
const loadInsights = async () => {
if (!selectedChildId) return;
setInsightsLoading(true);
try {
const data = await analyticsApi.getInsights(selectedChildId, days);
setInsights(data);
} catch (error) {
console.error('Failed to load insights:', error);
} finally {
setInsightsLoading(false);
}
};
const loadPredictions = async () => {
if (!selectedChildId) return;
setPredictionsLoading(true);
try {
const data = await analyticsApi.getPredictions(selectedChildId);
setPredictions(data);
} catch (error) {
console.error('Failed to load predictions:', error);
} finally {
setPredictionsLoading(false);
}
};
if (loading) {
return (
);
}
if (children.length === 0) {
return (
Add a child to your family to view insights and predictions.
);
}
return (
{/* Header */}
Insights & Predictions
AI-powered insights, patterns, and predictions for your child
{/* Child Selector */}
{children.length > 1 && (
Child
)}
{/* Growth Spurt Alert */}
{insights?.growthSpurt && }
{/* Tabs */}
setTabValue(newValue)}>
} iconPosition="start" />
} iconPosition="start" />
{/* Tab Panels */}
{/* Insights tab shows the existing InsightsDashboard */}
{/* Predictions tab */}
);
}