'use client' import Link from 'next/link' import { usePathname } from 'next/navigation' import { useEffect, useState } from 'react' import { AdminLanguageSwitcher, AdminThemeSwitcher, useAdminI18n, } from '@/components/I18nProvider' import { resolveBrowserAppUrl } from '@/lib/appUrls' import { ADMIN_API_BASE } from '@/lib/api' import { AdminSessionProvider, type AdminSessionUser } from './AdminSessionContext' function buildUnifiedLoginUrl(nextPath: string) { const dashboardUrl = resolveBrowserAppUrl(process.env.NEXT_PUBLIC_DASHBOARD_URL ?? 'http://localhost:3000/dashboard') const params = new URLSearchParams({ portal: 'admin', next: nextPath || '/dashboard', }) return `${dashboardUrl}/sign-in?${params.toString()}` } const navLinks = [ { href: '/dashboard', key: 'overview', icon: 'M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6' }, { href: '/dashboard/companies', key: 'companies', icon: 'M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4' }, { href: '/dashboard/site-config', key: 'siteConfig', icon: 'M4.5 12a7.5 7.5 0 1015 0 7.5 7.5 0 00-15 0zm7.5-4.5v4.5l3 3' }, { href: '/dashboard/renters', key: 'renters', icon: 'M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z' }, { href: '/dashboard/audit-logs', key: 'auditLogs', icon: 'M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z' }, { href: '/dashboard/notifications', key: 'notifications', icon: 'M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9' }, { href: '/dashboard/menu-management', key: 'menuManagement', icon: 'M4 6h16M4 12h16M4 18h10' }, { href: '/dashboard/admin-users', key: 'adminUsers', icon: 'M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z' }, { href: '/dashboard/billing', key: 'billing', icon: 'M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z' }, { href: '/dashboard/pricing', key: 'pricing', icon: 'M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z' }, ] export default function AdminDashboardLayout({ children }: { children: React.ReactNode }) { const { dict } = useAdminI18n() const pathname = usePathname() const [ready, setReady] = useState(false) const [admin, setAdmin] = useState(null) useEffect(() => { let cancelled = false fetch(`${ADMIN_API_BASE}/admin/auth/me`, { credentials: 'include', }) .then(async (response) => { if (cancelled) return if (response.ok) { const json = await response.json().catch(() => null) const resolvedAdmin = (json?.data ?? json) as AdminSessionUser | null if (!resolvedAdmin?.id || !resolvedAdmin?.email || !resolvedAdmin?.role) { window.location.replace(buildUnifiedLoginUrl(pathname)) return } setAdmin(resolvedAdmin) setReady(true) } else { window.location.replace(buildUnifiedLoginUrl(pathname)) } }) .catch(() => { if (!cancelled) window.location.replace(buildUnifiedLoginUrl(pathname)) }) return () => { cancelled = true } }, [pathname]) function handleLogout() { void fetch('/admin/api/v1/admin/auth/logout', { method: 'POST', credentials: 'include' }) window.location.href = buildUnifiedLoginUrl('/dashboard') } if (!ready) { return (
) } return (
{children}
) }