import {
Users,
UserCheck,
UserX,
Activity,
Smartphone,
LogIn,
ShieldAlert,
} from 'lucide-react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Skeleton } from '@/components/ui/skeleton';
import { useAdminDashboard, useAuditLog } from '@/hooks/useAdmin';
import { getRelativeTime } from '@/lib/date-utils';
import { cn } from '@/lib/utils';
interface StatCardProps {
icon: React.ReactNode;
label: string;
value: string | number;
iconBg?: string;
}
function StatCard({ icon, label, value, iconBg = 'bg-accent/10' }: StatCardProps) {
return (
);
}
function actionColor(action: string): string {
if (action.includes('failed') || action.includes('locked') || action.includes('disabled')) {
return 'bg-red-500/15 text-red-400';
}
if (action.includes('login') || action.includes('create') || action.includes('enabled')) {
return 'bg-green-500/15 text-green-400';
}
if (action.includes('config') || action.includes('role') || action.includes('password')) {
return 'bg-orange-500/15 text-orange-400';
}
return 'bg-blue-500/15 text-blue-400';
}
export default function AdminDashboardPage() {
const { data: dashboard, isLoading } = useAdminDashboard();
const { data: auditData } = useAuditLog(1, 10);
const mfaPct = dashboard ? Math.round(dashboard.mfa_adoption_rate * 100) : null;
const disabledUsers =
dashboard ? dashboard.total_users - dashboard.active_users : null;
return (
{/* Stats grid */}
{isLoading ? (
Array.from({ length: 5 }).map((_, i) => (
))
) : (
<>
}
label="Total Users"
value={dashboard?.total_users ?? '—'}
/>
}
label="Active Users"
value={dashboard?.active_users ?? '—'}
iconBg="bg-green-500/10"
/>
}
label="Disabled Users"
value={disabledUsers ?? '—'}
iconBg="bg-red-500/10"
/>
}
label="Active Sessions"
value={dashboard?.active_sessions ?? '—'}
iconBg="bg-blue-500/10"
/>
}
label="MFA Adoption"
value={mfaPct !== null ? `${mfaPct}%` : '—'}
iconBg="bg-purple-500/10"
/>
>
)}
{/* Recent logins */}
{isLoading ? (
{Array.from({ length: 5 }).map((_, i) => (
))}
) : !dashboard?.recent_logins?.length ? (
No recent logins.
) : (
|
Username
|
When
|
IP
|
{dashboard.recent_logins.map((entry, idx) => (
| {entry.username} |
{getRelativeTime(entry.last_login_at)}
|
{entry.ip_address ?? '—'}
|
))}
)}
{/* Recent admin actions */}
{!auditData?.entries?.length ? (
No recent actions.
) : (
|
Action
|
Actor
|
Target
|
When
|
{auditData.entries.slice(0, 10).map((entry, idx) => (
|
{entry.action}
|
{entry.actor_username ?? (
system
)}
|
{entry.target_username ?? '—'}
|
{getRelativeTime(entry.created_at)}
|
))}
)}
);
}