import { useQuery } from "@tanstack/react-query"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { useAuth } from "@/hooks/useAuth"; import { useEffect } from "react"; import { useToast } from "@/hooks/use-toast"; import { isUnauthorizedError } from "@/lib/authUtils"; import { useMutation } from "@tanstack/react-query"; import { queryClient } from "@/lib/queryClient"; import { apiRequest } from "@/lib/queryClient"; import type { MediaOutletRequest } from "@shared/schema"; export default function SuperAdminDashboard() { const { user, isLoading } = useAuth(); const { toast } = useToast(); // Redirect if not authenticated or not superadmin useEffect(() => { if (!isLoading && (!user || user.role !== 'superadmin')) { toast({ title: "Unauthorized", description: "You don't have permission to access this page.", variant: "destructive", }); setTimeout(() => { window.location.href = "/"; }, 500); } }, [isLoading, user, toast]); const { data: analytics, isLoading: analyticsLoading, error: analyticsError } = useQuery({ queryKey: ["/api/analytics"], retry: false, }); const { data: requests = [], isLoading: requestsLoading, error: requestsError } = useQuery({ queryKey: ["/api/media-outlet-requests"], retry: false, }); // Handle analytics errors useEffect(() => { if (analyticsError && isUnauthorizedError(analyticsError as Error)) { toast({ title: "Unauthorized", description: "You are logged out. Logging in again...", variant: "destructive", }); setTimeout(() => { window.location.href = "/api/login"; }, 500); } }, [analyticsError, toast]); // Handle requests errors useEffect(() => { if (requestsError && isUnauthorizedError(requestsError as Error)) { toast({ title: "Unauthorized", description: "You are logged out. Logging in again...", variant: "destructive", }); setTimeout(() => { window.location.href = "/api/login"; }, 500); } }, [requestsError, toast]); const updateRequestMutation = useMutation({ mutationFn: async ({ id, status }: { id: string; status: string }) => { await apiRequest("PATCH", `/api/media-outlet-requests/${id}`, { status }); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["/api/media-outlet-requests"] }); toast({ title: "Success", description: "Request status updated successfully.", }); }, onError: (error: Error) => { if (isUnauthorizedError(error)) { toast({ title: "Unauthorized", description: "You are logged out. Logging in again...", variant: "destructive", }); setTimeout(() => { window.location.href = "/api/login"; }, 500); return; } toast({ title: "Error", description: "Failed to update request status.", variant: "destructive" }); } }); const handleLogout = () => { window.location.href = "/api/logout"; }; const handleRequestAction = (id: string, status: string) => { updateRequestMutation.mutate({ id, status }); }; if (isLoading || !user || user.role !== 'superadmin') { return (
); } return (
{/* Header */}
S
SAPIENS • Super Admin Dashboard

Super Admin Dashboard

Comprehensive platform analytics and management

{/* Analytics Cards */}
{analyticsLoading ? ( Array.from({ length: 4 }).map((_, i) => (
)) ) : ( <>

Total Articles

{(analytics as any)?.totalArticles || 0}

Active Predictions

{(analytics as any)?.activePredictions || 0}

Live Auctions

{(analytics as any)?.liveAuctions || 0}

Total Revenue

${((analytics as any)?.totalRevenue || 0).toLocaleString()}

)}
{/* Platform Analytics */}

User Engagement

Daily Active Users 1,247
Avg. Session Duration 12m 34s
Bounce Rate 32%
Page Views 15,632

Market Performance

Total Prediction Volume $892K
Auction Participation 78%
Avg. Bid Amount $2,150
Quality Score Avg. 82/100
{/* Media Outlet Requests */}

Media Outlet Requests

{requestsLoading ? (
{Array.from({ length: 3 }).map((_, i) => (
))}
) : (requests as MediaOutletRequest[]).length === 0 ? (

No pending requests

) : (
{requests.filter(r => r.status === 'pending').map((request) => (

{request.name}

{request.category} {request.status}

{request.description}

Submitted {request.createdAt ? new Date(request.createdAt).toLocaleDateString() : 'Unknown'}

))}
)}
); }