import { useQuery } from "@tanstack/react-query"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent } from "@/components/ui/card"; import { useAuth } from "@/hooks/useAuth"; import { useEffect, useState } from "react"; import { useToast } from "@/hooks/use-toast"; import { isUnauthorizedError } from "@/lib/authUtils"; import { Search, Settings, ArrowUpDown } from "lucide-react"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import type { MediaOutlet } from "@shared/schema"; import MediaOutletManagement from "@/components/MediaOutletManagement"; export default function AdminDashboard() { const { user, isLoading } = useAuth(); const { toast } = useToast(); const [searchTerm, setSearchTerm] = useState(""); const [selectedOutlet, setSelectedOutlet] = useState(null); const [managingOutlet, setManagingOutlet] = useState(null); const [sortBy, setSortBy] = useState<"alphabetical" | "traffic">("alphabetical"); // Redirect if not authenticated or not admin/superadmin useEffect(() => { if (!isLoading && (!user || (user.role !== 'admin' && 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: mediaOutlets = [], isLoading: outletsLoading } = useQuery({ queryKey: ["/api/media-outlets"], queryFn: async () => { const res = await fetch("/api/media-outlets", { credentials: "include", }); if (!res.ok) { throw new Error(`${res.status}: ${res.statusText}`); } return res.json(); }, }); // Filter and sort outlets based on search term and sort option const filteredOutlets = mediaOutlets .filter(outlet => outlet.name.toLowerCase().includes(searchTerm.toLowerCase()) || (outlet.description && outlet.description.toLowerCase().includes(searchTerm.toLowerCase())) ) .sort((a, b) => { if (sortBy === "alphabetical") { return a.name.localeCompare(b.name); } else { // Sort by traffic score (descending - highest traffic first) return (b.trafficScore || 0) - (a.trafficScore || 0); } }); // Group outlets by category and sort const getOutletsByCategory = (category: string) => { const filtered = filteredOutlets.filter(outlet => outlet.category.toLowerCase() === category.toLowerCase() ); return filtered.sort((a, b) => { if (sortBy === "alphabetical") { return a.name.localeCompare(b.name); } else { // Sort by traffic score (descending - highest traffic first) return (b.trafficScore || 0) - (a.trafficScore || 0); } }); }; const handleLogout = () => { window.location.href = "/api/logout"; }; if (isLoading || !user || (user.role !== 'admin' && user.role !== 'superadmin')) { return (
); } // If managing an outlet, show MediaOutletManagement if (managingOutlet) { return ( setManagingOutlet(null)} /> ); } return (
{/* Header */}
setSearchTerm(e.target.value)} data-testid="input-admin-search" />

Admin Dashboard

Search and select media outlets to manage

{outletsLoading ? (
{Array.from({ length: 12 }).map((_, i) => (
))}
) : ( <>
{filteredOutlets.length} media outlets {searchTerm && `(search results for "${searchTerm}")`}
{/* People Section */}

People ({getOutletsByCategory("People").length})

{getOutletsByCategory("People").map((outlet) => ( setSelectedOutlet(outlet)} data-testid={`admin-card-outlet-${outlet.id}`} >
{outlet.imageUrl ? ( {outlet.name} ) : (
{outlet.name.charAt(0)}
)}

{outlet.name}

{outlet.description || "Media Outlet"}

))}
{/* Topics Section */}

Topics ({getOutletsByCategory("Topics").length})

{getOutletsByCategory("Topics").map((outlet) => ( setSelectedOutlet(outlet)} data-testid={`admin-card-outlet-${outlet.id}`} >
{outlet.imageUrl ? ( {outlet.name} ) : (
{outlet.name.charAt(0)}
)}

{outlet.name}

{outlet.description || "Media Outlet"}

))}
{/* Companies Section */}

Companies ({getOutletsByCategory("Companies").length})

{getOutletsByCategory("Companies").map((outlet) => ( setSelectedOutlet(outlet)} data-testid={`admin-card-outlet-${outlet.id}`} >
{outlet.imageUrl ? ( {outlet.name} ) : (
{outlet.name.charAt(0)}
)}

{outlet.name}

{outlet.description || "Media Outlet"}

))}
{filteredOutlets.length === 0 && searchTerm && (
No results found
Try a different search term
)} )} {/* Selected Outlet Modal/Preview */} {selectedOutlet && (
{selectedOutlet.imageUrl ? ( {selectedOutlet.name} ) : (
{selectedOutlet.name.charAt(0)}
)}

{selectedOutlet.name}

{selectedOutlet.description}

{selectedOutlet.category}
)}
); }