import { useQuery } from '@tanstack/react-query';
import { adminApi, api } from '../../api';
import { Activity, AlertTriangle, Car, CheckCircle2, RefreshCw, Route, Users } from 'lucide-react';
export default function AdminStatsCards() {
const { data, isLoading, isError, refetch } = useQuery({
queryKey: ['admin-stats'],
queryFn: async () => {
try {
const res = await adminApi.get('/api/admin/stats');
return res.data;
} catch {
const res = await api.get('/api/admin/stats');
return res.data;
}
},
refetchInterval: 10000,
});
if (isLoading) {
return (
{[1, 2, 3, 4, 5].map((i) => (
))}
);
}
if (isError) {
return (
Failed to load platform telemetry stats.
);
}
const stats = data?.stats || {
total_users: 0,
active_drivers: 0,
total_rides: 0,
active_rides: 0,
completed_rides: 0,
};
const cards = [
{
title: 'Total Users',
value: stats.total_users ?? 0,
icon: Users,
color: 'text-slate-900 dark:text-slate-100',
bg: 'bg-slate-100 dark:bg-slate-800',
description: 'Registered passengers & drivers',
},
{
title: 'Online Drivers',
value: stats.active_drivers ?? 0,
icon: Car,
color: 'text-emerald-600 dark:text-emerald-400',
bg: 'bg-emerald-50 dark:bg-emerald-950/40',
description: 'Available for instant dispatch',
},
{
title: 'Total System Rides',
value: stats.total_rides ?? 0,
icon: Route,
color: 'text-sky-600 dark:text-sky-400',
bg: 'bg-sky-50 dark:bg-sky-950/40',
description: 'All-time ride requests',
},
{
title: 'Active Ongoing',
value: stats.active_rides ?? 0,
icon: Activity,
color: 'text-amber-600 dark:text-amber-400',
bg: 'bg-amber-50 dark:bg-amber-950/40',
description: 'Requested, accepted, or picked up',
},
{
title: 'Completed Trips',
value: stats.completed_rides ?? 0,
icon: CheckCircle2,
color: 'text-teal-600 dark:text-teal-400',
bg: 'bg-teal-50 dark:bg-teal-950/40',
description: 'Successfully finished rides',
},
];
return (
{cards.map((card) => {
const Icon = card.icon;
return (
{(card.value ?? 0).toLocaleString()}
{card.description}
// Fixed text formatting
);
})}
);
}