"use client"; import Link from "next/link"; import { useCallback, useEffect, useMemo, useState } from "react"; import { Bell, MoonStar, RefreshCcw, ShieldCheck, SunMedium } from "lucide-react"; import { useSuperAdminSession } from "@/components/auth/session-context"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Drawer } from "@/components/ui/drawer"; import { useToast } from "@/components/ui/toast"; import { useTheme } from "@/components/theme/theme-provider"; import { listSuperAdminSessions } from "@/lib/api/auth"; import { listPlatformNotifications } from "@/lib/api/notifications"; import { getSuperAdminOverview, getSuperAdminRecentActivity } from "@/lib/api/superadmin"; import { formatDateTime } from "@/lib/format"; import { SUPERADMIN_PERMISSIONS, hasPermission } from "@/lib/permissions"; import type { NotificationItem, NotificationsResponse, SessionItem, SessionsResponse, SuperAdminOverviewResponse, SuperAdminRecentActivityItem, SuperAdminRecentActivityResponse, } from "@/types/api"; const EMPTY_NOTIFICATIONS: NotificationsResponse = { items: [], data: [], unreadCount: 0 }; const EMPTY_SESSIONS: SessionsResponse = { items: [] }; const EMPTY_ACTIVITY: SuperAdminRecentActivityResponse = { items: [] }; export function DashboardTopbar() { const { toast } = useToast(); const { theme, toggle } = useTheme(); const { permissions } = useSuperAdminSession(); const [drawerOpen, setDrawerOpen] = useState(false); const [loading, setLoading] = useState(false); const [overview, setOverview] = useState(null); const [recentActivity, setRecentActivity] = useState([]); const [notifications, setNotifications] = useState([]); const [sessions, setSessions] = useState([]); const canReadOverview = hasPermission(permissions, SUPERADMIN_PERMISSIONS.OVERVIEW_READ); const canReadAnalytics = hasPermission(permissions, SUPERADMIN_PERMISSIONS.ANALYTICS_READ); const canReadNotifications = hasPermission(permissions, SUPERADMIN_PERMISSIONS.NOTIFICATIONS_READ); const canManageSessions = hasPermission(permissions, SUPERADMIN_PERMISSIONS.SESSIONS_MANAGE); const todayLabel = useMemo(() => { try { return new Intl.DateTimeFormat("ar-SA", { weekday: "long", year: "numeric", month: "long", day: "numeric", }).format(new Date()); } catch { return "Today"; } }, []); const unreadCount = overview?.metrics.unreadNotificationsCount ?? notifications.filter((item) => item.read === false).length; const moderationAttentionCount = (overview?.metrics.flaggedPostsCount ?? 0) + (overview?.metrics.flaggedCommentsCount ?? 0); const summaryCards = [ canManageSessions ? { key: "sessions", label: "SuperAdmin sessions", value: String(sessions.length), } : null, canReadNotifications ? { key: "notifications", label: "Unread notifications", value: String(unreadCount), } : null, canReadOverview ? { key: "moderation", label: "Needs review", value: String(moderationAttentionCount), } : null, ].filter(Boolean) as Array<{ key: string; label: string; value: string }>; const summaryGridClass = summaryCards.length >= 3 ? "grid gap-3 sm:grid-cols-3" : summaryCards.length === 2 ? "grid gap-3 sm:grid-cols-2" : "grid gap-3"; const loadOverview = useCallback(async () => { setLoading(true); try { const [overviewResponse, activityResponse, notificationsResponse, sessionsResponse] = await Promise.all([ canReadOverview ? getSuperAdminOverview() : Promise.resolve(null), canReadAnalytics ? getSuperAdminRecentActivity({ limit: 5 }) : Promise.resolve(EMPTY_ACTIVITY), canReadNotifications ? listPlatformNotifications({ limit: 5, sortOrder: "desc" }) : Promise.resolve(EMPTY_NOTIFICATIONS), canManageSessions ? listSuperAdminSessions() : Promise.resolve(EMPTY_SESSIONS), ]); setOverview(overviewResponse); setRecentActivity(activityResponse.items ?? []); setNotifications(notificationsResponse.items ?? notificationsResponse.data ?? []); setSessions(sessionsResponse.items ?? []); } catch (error) { toast({ title: "Failed to load command center", description: String(error), variant: "danger" }); } finally { setLoading(false); } }, [canManageSessions, canReadAnalytics, canReadNotifications, canReadOverview, toast]); useEffect(() => { void loadOverview(); }, [loadOverview]); return ( <>
SuperAdmin

{todayLabel}

setDrawerOpen(false)} title="Operations Center" description="A quick view of the latest notifications, activity, and active sessions." side="right" >
{summaryCards.length ? (
{summaryCards.map((card) => (
{card.label}
{card.value}
))}
) : null} {canReadNotifications ? (

Latest notifications

Open page
{notifications.length ? ( notifications.map((item) => (

{item.title ?? item.type}

{item.read ? "Read" : "New"}

{item.previewText ?? item.deepLink ?? "-"}

)) ) : (

No recent notifications.

)}
) : null} {canReadAnalytics ? (

Recent activity

Dashboard
{recentActivity.length ? ( recentActivity.map((item, index) => (

{item.title}

{item.status}

{item.subtitle}

{formatDateTime(item.createdAt)}

)) ) : (

No recent activity.

)}
) : null} {canManageSessions ? (

Active sessions

{sessions.length ? ( sessions.map((item) => (
{item.id ?? item.jti ?? "session"}
{formatDateTime(item.createdAt)} - {formatDateTime(item.expiresAt)}
)) ) : (

No active sessions to display.

)}
) : null}
); }