'use client'; import { useEffect, useState } from 'react'; import { useRouter, usePathname } from 'next/navigation'; import Link from 'next/link'; import { useAuthStore } from '@/lib/store'; const menuItems = [ { href: '/admin', label: 'Dashboard', icon: 'πŸ“Š' }, { href: '/admin/visitor-stats', label: 'Visitor Stats', icon: 'πŸ‘οΈ' }, { href: '/admin/cars', label: 'Cars', icon: 'πŸš—' }, { href: '/admin/vehicle-requests', label: 'Vehicle Requests', icon: 'πŸ“‹' }, { href: '/admin/purchased', label: 'Purchased Vehicles', icon: 'πŸ“¦' }, { href: '/admin/dealers', label: 'Dealers', icon: '🀝' }, { href: '/admin/payments', label: 'Payments', icon: 'πŸ’³' }, { href: '/admin/withdrawals', label: 'Withdrawals', icon: 'πŸ’Έ' }, { href: '/admin/notifications', label: 'Notifications', icon: 'πŸ””' }, { href: '/admin/translations', label: 'Translations', icon: '🌐' }, { href: '/admin/dealer-translations', label: 'Dealer Descriptions', icon: 'πŸ“' }, { href: '/admin/users', label: 'Users', icon: 'πŸ‘₯' }, { href: '/admin/inquiries', label: 'Inquiries', icon: 'πŸ’¬' }, { href: '/admin/settings', label: 'Settings', icon: 'βš™οΈ' }, ]; export default function AdminLayout({ children, }: { children: React.ReactNode; }) { const router = useRouter(); const pathname = usePathname(); const { user, token, logout, isLoading: authLoading } = useAuthStore(); const [sidebarOpen, setSidebarOpen] = useState(true); // 디버그 둜그 useEffect(() => { console.log('Admin Layout Debug:', { pathname, token: token ? 'exists' : 'null', user: user ? { id: user.id, email: user.email, is_admin: user.is_admin } : null, authLoading }); }, [pathname, token, user, authLoading]); useEffect(() => { // 둜그인 νŽ˜μ΄μ§€λŠ” 체크 ν•„μš” μ—†μŒ if (pathname === '/admin/login') { return; } // 아직 λ‘œλ”© 쀑이면 λŒ€κΈ° if (authLoading) { return; } // 토큰이 μ—†μœΌλ©΄ 둜그인 νŽ˜μ΄μ§€λ‘œ if (!token) { router.push('/admin/login'); return; } // user 정보가 μ—†μœΌλ©΄ 둜그인 νŽ˜μ΄μ§€λ‘œ if (!user) { router.push('/admin/login'); return; } // κ΄€λ¦¬μžκ°€ μ•„λ‹ˆλ©΄ ν™ˆμœΌλ‘œ if (!user.is_admin) { console.log('User is not admin, redirecting to home'); router.push('/'); return; } }, [pathname, router, token, user, authLoading]); const handleLogout = () => { logout(); router.push('/admin/login'); }; // 둜그인 νŽ˜μ΄μ§€λŠ” λ ˆμ΄μ•„μ›ƒ 없이 λ Œλ”λ§ if (pathname === '/admin/login') { return <>{children}; } // λ‘œλ”© 쀑 if (authLoading) { return (
); } // 토큰 μ—†μŒ λ˜λŠ” μœ μ € μ—†μŒ λ˜λŠ” κ΄€λ¦¬μž μ•„λ‹˜ -> 빈 ν™”λ©΄ (λ¦¬λ‹€μ΄λ ‰νŠΈ 될 μ˜ˆμ •) if (!token || !user || !user.is_admin) { return (
); } return (
{/* Sidebar */} {/* Main Content */}
{/* Top Bar */}
View Site
{user?.name?.charAt(0).toUpperCase() || user?.email?.charAt(0).toUpperCase() || 'A'}
{/* Page Content */}
{children}
); }