'use client'; import { useState, useEffect } from 'react'; import { Typography, Box, Breadcrumbs, Link, Card, CardContent, Grid, FormControl, InputLabel, Select, MenuItem, CircularProgress, Alert, Chip, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper } from '@mui/material'; import { Home, Analytics, TrendingUp, People, Chat, FavoriteBorder, Bookmarks } from '@mui/icons-material'; import { LineChart, Line, AreaChart, Area, BarChart, Bar, PieChart, Pie, Cell, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'; interface AnalyticsData { period: number; overview: { users: { total: number; new: number; active: number }; prayerRequests: { total: number; active: number; new: number }; prayers: { total: number; new: number }; conversations: { total: number; active: number; new: number }; messages: { total: number; new: number }; bookmarks: { total: number; new: number }; }; distributions: { usersByRole: Array<{ role: string; _count: { role: number } }>; prayersByCategory: Array<{ category: string; _count: { category: number } }>; }; topContent: { prayerRequests: Array<{ id: string; title: string; category: string; prayerCount: number; author: string; }>; }; activity: { daily: Array<{ date: string; newUsers: number; newPrayers: number; newConversations: number; newBookmarks: number; }>; }; } interface MetricCardProps { title: string; value: number; change: number; icon: React.ReactNode; color: string; } function MetricCard({ title, value, change, icon, color }: MetricCardProps) { return ( {title} {value.toLocaleString()} = 0 ? 'success.main' : 'error.main' }} /> = 0 ? 'success.main' : 'error.main' }} > {change >= 0 ? '+' : ''}{change} this period {icon} ); } const COLORS = ['#8884d8', '#82ca9d', '#ffc658', '#ff7300', '#0088fe', '#00c49f']; export default function AdminAnalyticsPage() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); const [period, setPeriod] = useState('30'); useEffect(() => { const fetchAnalytics = async () => { setLoading(true); try { const response = await fetch(`/api/admin/analytics/overview?period=${period}`, { credentials: 'include' }); if (response.ok) { const analyticsData = await response.json(); setData(analyticsData); } else { setError('Failed to load analytics data'); } } catch (error) { setError('Network error loading analytics'); } finally { setLoading(false); } }; fetchAnalytics(); }, [period]); if (loading) { return ( ); } if (error) { return ( {error} ); } if (!data) return null; return ( {/* Breadcrumbs */} Admin Analytics {/* Page Header */} Analytics Dashboard Comprehensive insights into user behavior and content engagement Time Period {/* Metric Cards */} window.location.href = '/admin/analytics/users'}> } color="#1976d2" /> } color="#d32f2f" /> } color="#ed6c02" /> } color="#2e7d32" /> } color="#9c27b0" /> } color="#0288d1" /> {/* Daily Activity Chart */} Daily Activity Trends {/* User Roles Distribution */} User Roles Distribution ({ name: item.role, value: item._count.role }))} cx="50%" cy="50%" outerRadius={80} fill="#8884d8" dataKey="value" label > {data.distributions.usersByRole.map((entry, index) => ( ))} {/* Prayer Categories Chart */} Prayer Requests by Category ({ category: item.category, count: item._count.category }))}> {/* Top Prayer Requests */} Most Prayed For Requests Title Category Prayers {data.topContent.prayerRequests.map((request) => ( {request.title} by {request.author} {request.prayerCount} ))}
); }