import { useQuery } from '@tanstack/react-query' import { Users, FileText, Package, DollarSign, TrendingUp, Clock } from 'lucide-react' import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, BarChart, Bar } from 'recharts' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { analyticsApi, type DashboardData, type ChartDataPoint } from '@/api/analytics' import { useAuthStore } from '@/stores/authStore' export default function DashboardPage() { const { isAuthenticated, token } = useAuthStore() // Only make requests if authenticated and token exists const isReady = isAuthenticated && !!token && !!localStorage.getItem('admin_token') const { data: dashboardData, isLoading: dashboardLoading } = useQuery({ queryKey: ['dashboard'], queryFn: analyticsApi.getDashboard, enabled: isReady, }) const { data: usersChartData } = useQuery<{ data: ChartDataPoint[] }>({ queryKey: ['users-chart'], queryFn: analyticsApi.getUsersChart, enabled: isReady, }) const { data: revenueChartData } = useQuery<{ data: ChartDataPoint[] }>({ queryKey: ['revenue-chart'], queryFn: analyticsApi.getRevenueChart, enabled: isReady, }) if (dashboardLoading) { return (

Dashboard

Welcome to Sto k Odnomu Admin Panel

{[...Array(4)].map((_, i) => (
))}
) } return (

Dashboard

Welcome to Sto k Odnomu Admin Panel

{/* Stats Cards */}
Total Users
{dashboardData?.stats.users || 0}

Registered users

Total Cards
{dashboardData?.stats.cards || 0}

Game cards created

Active Packs
{dashboardData?.stats.enabledPacks || 0}

of {dashboardData?.stats.packs || 0} total packs

Total Payments
{dashboardData?.stats.payments || 0}

Successful transactions

{/* Charts */}
User Registrations Daily user registrations for the last 30 days {usersChartData?.data && ( new Date(value).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })} /> new Date(value).toLocaleDateString()} formatter={(value: number) => [value, 'Registrations']} /> )} Revenue Trend Daily revenue for the last 30 days {revenueChartData?.data && ( new Date(value).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })} /> new Date(value).toLocaleDateString()} formatter={(value: number) => [`$${value}`, 'Revenue']} /> )}
{/* Recent Users and Top Packs */}
Recent Users Latest user registrations
{dashboardData?.recentUsers.slice(0, 5).map((user) => (

{user.name || 'No name'}

{user.email || 'No email'}

{user.createdAt ? new Date(user.createdAt).toLocaleDateString() : 'N/A'}
)) || (

No recent users

)}
Popular Packs Most popular card packs
{dashboardData?.topPacks.map((pack) => (

{pack.title}

{pack.cards} cards

{pack.enabled ? 'Active' : 'Disabled'}
)) || (

No packs available

)}
) }