"use client"; import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { useRouter } from "next/navigation"; import { useStableState } from "@/hooks/useStableState"; import { useVisiblePolling } from "@/hooks/useVisiblePolling"; import { Calendar, Banknote, Gift, Users, Ticket, QrCode, DoorOpen, Wallet, UserPlus, FileText, MessageCircle, BarChart2, Mail, TicketPercent, ShieldCheck, ListChecks, WifiOff, BedDouble, Filter, ClipboardCheck, Baby, HandHeart, Layers, } from "lucide-react"; import { StatCard, StatCardRow } from "@/components/shared/StatCard"; import { QuickActionTile, QuickActionGrid } from "@/components/shared/QuickActionTile"; import { AreaTrendChart } from "@/components/charts/AreaTrendChart"; import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "@/components/ui/table"; import type { DashboardStats, PaymentStats, RecentScan, ScanStats } from "@/types"; import { CampusFilter, CampusScopeNote } from "@/components/shared/CampusFilter"; import { TodayPanel } from "@/components/dashboard/TodayPanel"; import { CommandPaletteButton } from "@/components/shared/CommandPalette"; import { apiV2Data } from "@/lib/apiV2"; const formatRand = (n: number) => `R ${(n || 0).toFixed(2)}`; const formatRandAxis = (n: number) => `R${new Intl.NumberFormat(undefined, { notation: "compact", maximumFractionDigits: 1 }).format(n)}`; const formatCount = (n: number) => (n || 0).toLocaleString(); const REPORTS_URL = "/dashboard/supervisor/reports"; const QUICK_ACTIONS = [ { href: "/dashboard/supervisor/manual", label: "Manual registration", description: "Register a guest and issue tickets", icon: UserPlus }, { href: "/dashboard/supervisor/events", label: "Manage events", description: "Create, edit, and update ticket types", icon: Calendar }, { href: "/dashboard/supervisor/series", label: "Event series", description: "Group events like every year's camp on one page", icon: Layers }, { href: "/dashboard/supervisor/payments", label: "Payments & donations", description: "Manual payments and assignment", icon: Wallet }, { href: "/dashboard/supervisor/allocations", label: "Rooms & groups", description: "Put attendees into rooms, groups or buses", icon: BedDouble }, { href: "/dashboard/staff/kids", label: "Kids check-in", description: "Check children in and out with pickup codes", icon: Baby }, { href: "/dashboard/supervisor/kids", label: "Kids check-in setup", description: "Switch it on for an event and set up the rooms", icon: Baby }, { href: "/dashboard/supervisor/volunteers", label: "Volunteer rosters", description: "Serving roles, who serves where, check-ins and hours", icon: HandHeart }, { href: "/dashboard/supervisor/offline-scans", label: "Offline scanning", description: "Devices scanning offline and their synced scans", icon: WifiOff }, { href: "/dashboard/supervisor/attendance", label: "Attendance", description: "Who came to each session, and certificates", icon: ClipboardCheck }, { href: "/dashboard/supervisor/waitlist", label: "Waitlist", description: "People waiting for sold-out tickets", icon: ListChecks }, { href: "/dashboard/supervisor/consent", label: "Parental consent", description: "Youth events: consent status and medical list", icon: ShieldCheck }, { href: "/dashboard/supervisor/coupons", label: "Coupon codes", description: "Discounts for staff, leaders and promotions", icon: TicketPercent }, { href: "/dashboard/supervisor/bundles", label: "Bundle deals", description: "Tickets sold together for one price", icon: Gift }, { href: "/dashboard/staff/ticket-scanning", label: "Open scanner", description: "Use your device camera to validate tickets", icon: QrCode }, { href: "/dashboard/supervisor/at-the-door", label: "At the door", description: "Walk-ins, payments, ticket printing", icon: DoorOpen }, { href: "/dashboard/supervisor/reports", label: "Reports", description: "View, export, and email reports", icon: BarChart2 }, { href: "/dashboard/supervisor/forms", label: "Attendee forms", description: "View submitted attendee forms", icon: FileText }, { href: "/dashboard/supervisor/email-attendees", label: "Email attendees", description: "Send a message to attendees of an event", icon: Mail }, { href: "/dashboard/supervisor/whatsapp-attendees", label: "WhatsApp attendees", description: "Send a WhatsApp message to event attendees", icon: MessageCircle }, ] as const; type OverviewMetric = { thisMonth: number; lastMonth: number; pctChange: number | null }; type Overview = { funnel?: { views: number; paid: number; conversion: number | null } | null; activeEvents: number; registrations: OverviewMetric; ticketsSold: OverviewMetric; revenue: OverviewMetric; donations: OverviewMetric; trend: { date: string; revenue: number }[]; topEvents: { eventId: string; title: string; revenue: number; registrations: number; ticketsSold: number }[]; }; export default function SupervisorDashboardPage() { const { user, loading, token } = useAuth(); const router = useRouter(); const canView = useMemo(() => { const role = user?.role; return role === "admin" || role === "supervisor"; }, [user]); useEffect(() => { if (loading) return; if (!user) router.replace("/login"); }, [user, loading, router]); // /api/stats/supervisor covers scan activity + today's payments (unchanged from before); // /api/stats/overview is the month-over-month KPI/trend/top-events endpoint, shared // with the admin dashboard since supervisors already have full revenue/donation visibility. const [scanStats, setScanStats] = useStableState(null); const [paymentStats, setPaymentStats] = useStableState(null); const [recentScans, setRecentScans] = useStableState([]); const [overview, setOverview] = useStableState(null); const [loadingStats, setLoadingStats] = useState(false); const hasLoadedOnce = useRef(false); const [campus, setCampus] = useState(""); const loadStats = useCallback(async () => { if (!token) return; const isFirstLoad = !hasLoadedOnce.current; try { if (isFirstLoad) setLoadingStats(true); const [data, overviewData] = await Promise.all([ (apiV2Data("get", "/stats/supervisor", { authToken: token }) as Promise), // The month-over-month tiles follow the campus filter; today's totals stay church-wide. (apiV2Data("get", "/stats/overview", { query: { campus: campus || undefined }, authToken: token }) as Promise), ]); setScanStats(data.scanStats); setPaymentStats(data.paymentStats); setRecentScans(Array.isArray(data.recentScans) ? data.recentScans : []); setOverview(overviewData); } catch { // ignore } finally { hasLoadedOnce.current = true; if (isFirstLoad) setLoadingStats(false); } }, [token, campus, setScanStats, setPaymentStats, setRecentScans, setOverview]); useEffect(() => { if (!token) return; loadStats(); }, [token, loadStats]); // Poll every 15s while the tab is visible; pause in the background and refetch // immediately on return instead of leaving stale numbers up. useVisiblePolling(() => { if (!token) return; loadStats(); }, 15000, !!token); return (

Welcome back{user ? `, ${user.name}` : ""} đź‘‹

Here's what's happening with your events today.

{!canView && (
You need supervisor or admin access to use these tools.
)} {overview?.funnel && ( )}
Quick actions
{QUICK_ACTIONS.map(a => ( ))}

Revenue trend — past month

{overview && overview.trend.length > 0 ? ( ({ label: t.date.slice(5), value: t.revenue }))} valueFormatter={formatRand} axisFormatter={formatRandAxis} /> ) : (
No revenue recorded in the past month.
)}

Top performing events

{overview && overview.topEvents.length > 0 ? ( Event Registrations Revenue Tickets sold {overview.topEvents.map(e => ( {e.title} {formatCount(e.registrations)} {formatRand(e.revenue)} {formatCount(e.ticketsSold)} ))}
) : (
No event revenue recorded yet.
)}

Recent scans

{loadingStats && Refreshing…}
    {recentScans.map((u) => (
  • {u.ticket?.event?.title || u.ticket?.eventId || 'Event'}
    {new Date(u.scannedAt).toLocaleString()}
    {u.ticket?.registrationOption?.eventOption?.name || 'Ticket'} — #{String(u.ticket?.id || '').slice(0, 8)}
    Scanned by: {u.scannedBy?.name || u.scannedById}
  • ))} {recentScans.length === 0 &&
  • No scans yet.
  • }

Scanner activity

{loadingStats && Refreshing…}
{scanStats ? (
Today
{scanStats.totalToday}
My scans
{scanStats.myToday}
Last hour
{scanStats.lastHour}
) : (
No scanner data yet.
)}

Payments today

{paymentStats ? (
Today
{formatRand(paymentStats.totalToday)}
) : (
No payment data yet.
)} {scanStats && scanStats.byStaff?.length > 0 && (
Today by staff
    {scanStats.byStaff.map((s) => (
  • {s.name || 'Staff'} {s.count}
  • ))}
)}
); }