"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { BarChart3, CalendarDays, CheckCircle2, CreditCard, LayoutDashboard, Mail, QrCode, Settings, Users, Workflow, MessageCircle, Handshake, Contact, Plug } from "lucide-react"; import { clsx } from "clsx"; import { useEffect, useState } from "react"; import { apiFetch, clearAccessToken } from "@/lib/api"; import Image from "next/image"; const nav = [ ["Dashboard", "/dashboard", LayoutDashboard, "dashboard"], ["Events", "/events", CalendarDays, "events"], ["Attendees", "/attendees", Users, "attendees"], ["Invitees", "/invitees", Mail, "invitees"], ["RSVPs", "/rsvps", CheckCircle2, "rsvps"], ["Registrations", "/registrations", Users, "registrations"], ["Check-in (Live)", "/check-in", QrCode, "checkins"], ["QR Codes", "/qr-codes", QrCode, "qrcodes"], ["Forms & Workflows", "/forms-workflows", Workflow, "workflows"], ["Calendar", "/calendar", CalendarDays, "calendar"], ["Communications", "/communications", MessageCircle, "communications"], ["Email Campaigns", "/email-campaigns", Mail, "communications"], ["WhatsApp Campaigns", "/whatsapp-campaigns", MessageCircle, "communications"], ["Payments", "/payments", CreditCard, "payments"], ["CRM Pipeline", "/crm-pipeline", Handshake, "crm"], ["Contacts / Leads", "/contacts-leads", Contact, "crm"], ["Reports", "/reports", BarChart3, "reports"], ["Settings", "/settings", Settings, "settings"], ["Integrations", "/integrations", Plug, "integrations"] ] as const; export function AdminShell({ title, children }: { title: string; children: React.ReactNode }) { const pathname = usePathname(); const [appName, setAppName] = useState("EventSphere"); const [logoUrl, setLogoUrl] = useState(null); const [modules, setModules] = useState | null>(null); const [me, setMe] = useState<{ fullName: string; email: string; avatarUrl?: string | null; mustChangePassword?: boolean } | null>(null); useEffect(() => { const load = async () => { try { const settings = await apiFetch<{ appName: string | null; logoUrl: string | null; modules: Record | null }>("/settings"); setAppName(settings.appName ?? "EventSphere"); setLogoUrl(settings.logoUrl ?? null); setModules(settings.modules ?? null); } catch { setAppName("EventSphere"); setLogoUrl(null); setModules(null); } }; void load(); }, []); useEffect(() => { const load = async () => { try { const payload = await apiFetch<{ user: any }>("/auth/me"); const u = payload.user ?? null; if (!u) return; setMe({ fullName: String(u.fullName ?? "User"), email: String(u.email ?? ""), avatarUrl: u.avatarUrl ?? null, mustChangePassword: Boolean(u.mustChangePassword) }); } catch { setMe(null); } }; void load(); }, []); useEffect(() => { if (me?.mustChangePassword && typeof window !== "undefined" && window.location.pathname !== "/change-password") { window.location.href = "/change-password"; } }, [me]); useEffect(() => { if (!modules) return; const entry = nav.find((n) => n[1] === pathname); if (!entry) return; const key = entry[3]; if (key && modules[key] === false) { window.location.href = "/dashboard"; } }, [modules, pathname]); return
{title}
{me?.avatarUrl ? ( {me.fullName} ) : (
SA
)}
{me?.fullName ?? "Super Admin"}
{me?.email ?? "Administrator"}
{children}
; }