'use client'; import { useEffect, useState } from 'react'; import { useTranslations } from 'next-intl'; import { useAuth } from '@/lib/context/auth-context'; import { Link, usePathname, useRouter } from '@/i18n/navigation'; import LanguageSwitcher from '@/components/LanguageSwitcher'; import NotificationDropdown from '@/components/NotificationDropdown'; import Image from 'next/image'; import { Users, Building2, Package, FileText, BarChart3, Newspaper, ScrollText, ArrowLeft, LogOut, ShieldCheck, type LucideIcon, } from 'lucide-react'; interface AdminNavItem { key: string; href: string; icon: LucideIcon; } const adminNavItems: AdminNavItem[] = [ { key: 'users', href: '/admin/users', icon: Users }, { key: 'organizations', href: '/admin/organizations', icon: Building2 }, { key: 'bookings', href: '/admin/bookings', icon: Package }, { key: 'documents', href: '/admin/documents', icon: FileText }, { key: 'csvRates', href: '/admin/csv-rates', icon: BarChart3 }, { key: 'blog', href: '/admin/blog', icon: Newspaper }, { key: 'logs', href: '/admin/logs', icon: ScrollText }, ]; export default function AdminLayout({ children }: { children: React.ReactNode }) { const { user, logout, loading, isAuthenticated } = useAuth(); const pathname = usePathname(); const router = useRouter(); const tItems = useTranslations('components.adminPanelDropdown'); const tAdmin = useTranslations('admin'); const [sidebarOpen, setSidebarOpen] = useState(false); // The /admin/login page lives under this segment but must render without the // admin chrome and without triggering the role guard. const isLoginRoute = pathname === '/admin/login'; useEffect(() => { if (isLoginRoute || loading) return; if (!isAuthenticated) { router.replace('/admin/login'); return; } if (user && user.role !== 'ADMIN') { router.replace('/dashboard'); } }, [isLoginRoute, loading, isAuthenticated, user, router]); if (isLoginRoute) { return <>{children}>; } const authorized = isAuthenticated && user?.role === 'ADMIN'; if (loading || !authorized) { return (
{user?.firstName} {user?.lastName}
{user?.email}