'use client'; import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; import { useSuperAdmin } from './super-admin-context'; import { useAuth } from './auth-context'; const NAV_LINKS = [ { href: '/search', label: 'Search' }, { href: '/groups', label: 'Groups & Routes' }, { href: '/messages/pending', label: 'Pending messages' }, { href: '/settings/rules', label: 'Rules' }, { href: '/settings/bot', label: 'Bot' }, ]; const SUPER_ADMIN_LINKS = [ { href: '/admin', label: 'Dashboard' }, { href: '/admin/tenants', label: 'Tenants' }, { href: '/admin/bots', label: 'Bot Pool' }, ]; const PUBLIC_PATHS = ['/login', '/signup', '/onboard']; const ADMIN_PATHS = ['/admin']; const MEMBER_PATHS = ['/my']; export function Sidebar() { const { admin, loading, logout } = useAuth(); const { admin: superAdmin, logout: superLogout } = useSuperAdmin(); const pathname = usePathname(); const router = useRouter(); const [pendingCount, setPendingCount] = useState(null); useEffect(() => { fetch('/api/messages/pending/count') .then((r) => r.ok ? r.json() : null) .then((data) => setPendingCount(data?.count ?? null)) .catch(() => setPendingCount(null)); }, []); useEffect(() => { if (loading) return; if (PUBLIC_PATHS.some((p) => pathname.startsWith(p))) return; if (ADMIN_PATHS.some((p) => pathname.startsWith(p))) return; if (MEMBER_PATHS.some((p) => pathname.startsWith(p))) return; if (!admin) { router.replace(`/login?next=${encodeURIComponent(pathname)}`); } }, [loading, admin, pathname, router]); if (PUBLIC_PATHS.some((p) => pathname.startsWith(p))) { return ( ); } if (ADMIN_PATHS.some((p) => pathname.startsWith(p))) { return ( ); } if (MEMBER_PATHS.some((p) => pathname.startsWith(p))) { return ( ); } return ( ); }