import { ReactNode, useState } from 'react'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import { useAuth } from '@/hooks/useAuth'; import { useCart } from '@/contexts/CartContext'; import { useBranding } from '@/hooks/useBranding'; import { Button } from '@/components/ui/button'; import { Sheet, SheetContent, SheetTrigger } from '@/components/ui/sheet'; import { cn } from '@/lib/utils'; import { LayoutDashboard, Package, BookOpen, ShoppingCart, Receipt, User, Settings, Users, Calendar, LogOut, Menu, Home, MoreHorizontal, X, Video, } from 'lucide-react'; interface NavItem { label: string; href: string; icon: React.ComponentType<{ className?: string }>; } const userNavItems: NavItem[] = [ { label: 'Dashboard', href: '/dashboard', icon: LayoutDashboard }, { label: 'Produk', href: '/products', icon: Package }, { label: 'Akses', href: '/access', icon: BookOpen }, { label: 'Order', href: '/orders', icon: Receipt }, { label: 'Profil', href: '/profile', icon: User }, ]; const adminNavItems: NavItem[] = [ { label: 'Dashboard', href: '/admin', icon: LayoutDashboard }, { label: 'Produk', href: '/admin/products', icon: Package }, { label: 'Bootcamp', href: '/admin/bootcamp', icon: BookOpen }, { label: 'Konsultasi', href: '/admin/consulting', icon: Video }, { label: 'Order', href: '/admin/orders', icon: Receipt }, { label: 'Member', href: '/admin/members', icon: Users }, { label: 'Kalender', href: '/admin/events', icon: Calendar }, { label: 'Pengaturan', href: '/admin/settings', icon: Settings }, ]; const mobileUserNav: NavItem[] = [ { label: 'Home', href: '/dashboard', icon: Home }, { label: 'Kelas', href: '/access', icon: BookOpen }, { label: 'Pesanan', href: '/orders', icon: Receipt }, { label: 'Profil', href: '/profile', icon: User }, ]; const mobileAdminNav: NavItem[] = [ { label: 'Dashboard', href: '/admin', icon: LayoutDashboard }, { label: 'Produk', href: '/admin/products', icon: Package }, { label: 'Pesanan', href: '/admin/orders', icon: Receipt }, { label: 'Pengguna', href: '/admin/members', icon: Users }, ]; interface AppLayoutProps { children: ReactNode; } export function AppLayout({ children }: AppLayoutProps) { const { user, isAdmin, signOut } = useAuth(); const { items } = useCart(); const branding = useBranding(); const location = useLocation(); const navigate = useNavigate(); const [moreOpen, setMoreOpen] = useState(false); const navItems = isAdmin ? adminNavItems : userNavItems; const mobileNav = isAdmin ? mobileAdminNav : mobileUserNav; const handleSignOut = async () => { await signOut(); navigate('/'); }; const isActive = (href: string) => { if (href === '/dashboard' && location.pathname === '/dashboard') return true; if (href === '/admin' && location.pathname === '/admin') return true; if (href !== '/dashboard' && href !== '/admin') { return location.pathname.startsWith(href); } return false; }; // Get additional items for "More" menu const moreItems = navItems.filter(item => !mobileNav.some(m => m.href === item.href)); const brandName = branding.brand_name || 'LearnHub'; const logoUrl = branding.brand_logo_url; if (!user) { // Public layout for non-authenticated pages return (
{logoUrl ? ( {brandName} ) : ( brandName )}
{children}
); } return (
{/* Desktop Sidebar */} {/* Main content */}
{/* Mobile Header */}
{logoUrl ? ( {brandName} ) : ( brandName )}
{items.length > 0 && ( {items.length} )}
{children}
{/* Mobile Bottom Navigation */}
); }