import { useState, useEffect } from 'react'; import { useRouter } from 'next/router'; import Link from 'next/link'; import { useTheme } from '../lib/theme-context'; import MobileNavigation from './MobileNavigation'; // User Profile Dropdown Component function UserProfileDropdown({ user, onMobileMenuClose }) { // Hook order is fixed for both branches; do not move this below the // null-user early return — see rules-of-hooks (AGENTS.md Gotcha #11.5). const [isDropdownOpen, setIsDropdownOpen] = useState(false); // Logged-out: replace avatar + email + dropdown with a Sign-in CTA. if (!user) { return (
Sign in
); } const profileMenuItems = [ { name: 'Profile', href: '/profile', icon: 'user' }, { name: 'Settings', href: '/settings', icon: 'settings' }, ...(user?.role === 'admin' ? [ { name: 'Admin Panel', href: '/admin', icon: 'admin' } ] : []), { name: 'Logout', href: '/logout', icon: 'logout', isLogout: true } ]; const getProfileIcon = (iconName) => { const icons = { user: ( ), settings: ( ), admin: ( ), logout: ( ) }; return icons[iconName] || icons.user; }; return (
{/* Dropdown Menu */} {isDropdownOpen && ( <> {/* Backdrop */}
setIsDropdownOpen(false)} /> {/* Menu */}
{profileMenuItems.map((item) => (
{ setIsDropdownOpen(false); onMobileMenuClose(); }} > {item.name}
))}
)} {/* Profile Button */}
); } // Navigation Content Component - Shared between desktop and mobile function NavigationContent({ user, router, onItemClick }) { const [isCommunityExpanded, setIsCommunityExpanded] = useState( router.pathname.startsWith('/community') ); const [lastCommunityPath, setLastCommunityPath] = useState(router.pathname); if ( router.pathname.startsWith('/community') && router.pathname !== lastCommunityPath ) { setLastCommunityPath(router.pathname); if (!isCommunityExpanded) { setIsCommunityExpanded(true); } } else if (router.pathname !== lastCommunityPath) { setLastCommunityPath(router.pathname); } // Navigation structure for authenticated users const authenticatedNavigation = user ? [ { name: 'Activity', href: '/activity', icon: 'activity', active: router.pathname === '/activity', isPlaceholder: true } ] : []; // My Collection section (only for authenticated users) const myCollectionNavigation = user ? { name: 'My Collection', href: '/dashboard', icon: 'collection', active: router.pathname === '/dashboard' || router.pathname === '/collections' || router.pathname === '/my-cards' || router.pathname === '/decks' || router.pathname === '/analytics', expanded: true, items: [ { name: 'Lists', href: '/collections', active: router.pathname === '/collections' }, { name: 'Cards', href: '/my-cards', active: router.pathname === '/my-cards' }, { name: 'Decks', href: '/decks', active: router.pathname === '/decks' }, { name: 'Analytics', href: '/analytics', active: router.pathname === '/analytics', isPlaceholder: true } ] } : null; // Always visible navigation (public + authenticated) const publicNavigation = [ { name: 'Cards', href: '/cards', icon: 'card', active: router.pathname === '/cards' }, { name: 'Scanner', href: '/scanner', icon: 'scanner', active: router.pathname === '/scanner' }, { name: 'Deck Builder', href: '/deck-builder', icon: 'deck', active: router.pathname === '/deck-builder', isPlaceholder: true } ]; // Admin navigation (only for admin users) const adminNavigation = (user?.role === 'admin') ? [ { name: 'Admin Tools', href: '/admin/card-editor', icon: 'admin', active: router.pathname.startsWith('/admin'), badge: 'ADMIN' } ] : []; const communityNavigation = { name: 'Community', icon: 'community', active: router.pathname.startsWith('/community'), expanded: isCommunityExpanded, items: [ { name: 'Lists', href: '/community/collections', active: router.pathname === '/community/collections' }, { name: 'Decks', href: '/community/decks', active: router.pathname === '/community/decks' }, { name: 'Forums', href: '/community/forums', active: router.pathname === '/community/forums' } ] }; const getIcon = (iconName) => { const icons = { grid: ( ), collection: ( ), card: ( ), deck: ( ), analytics: ( ), community: ( ), settings: ( ), logout: ( ), admin: ( ), notifications: ( ), activity: ( ), scanner: ( ) }; return icons[iconName] || icons.grid; }; return ( <> {/* Authenticated User Navigation - Activity */} {authenticatedNavigation.map((item) => (
{item.isPlaceholder ? (
{item.name}
Coming Soon
) : (
{item.name}
)}
))} {/* My Collection Section (only for authenticated users) */} {myCollectionNavigation && (
{/* My Collection Header - Clickable */}
{myCollectionNavigation.name}
{/* My Collection Sub-items */} {myCollectionNavigation.expanded && (
{myCollectionNavigation.items.map((subItem) => (
{subItem.isPlaceholder ? (
{subItem.name} Soon
) : (
{subItem.name}
)}
))}
)}
)} {/* Separator */}
{/* Public Navigation (always visible) */} {publicNavigation.map((item) => (
{item.isPlaceholder ? (
{item.name}
Coming Soon
) : (
{item.name}
)}
))} {/* Community Section with Sub-items */}
setIsCommunityExpanded(!isCommunityExpanded)} role="menuitem" aria-expanded={isCommunityExpanded} aria-haspopup="menu" tabIndex={0} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); setIsCommunityExpanded(!isCommunityExpanded); } }} >
{communityNavigation.name}
{/* Community Sub-items */} {isCommunityExpanded && (
{communityNavigation.items.map((subItem) => (
{ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onItemClick(); } }} > {subItem.name}
))}
)}
{/* Admin Navigation (if admin user) */} {adminNavigation.map((item) => (
{item.name}
{item.badge && ( {item.badge} )}
))} ); } export default function Layout({ children, user = null, showSearch = false }) { const router = useRouter(); const { theme, toggleTheme } = useTheme(); const [searchQuery, setSearchQuery] = useState(''); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); return (
{/* Mobile Navigation - Bottom bar for mobile */} setIsMobileMenuOpen(true)} /> {/* Mobile Overlay */} {isMobileMenuOpen && (
setIsMobileMenuOpen(false)} /> )} {/* Mobile Menu Drawer - Slides in from left when "More" is tapped */}
{/* Mobile Header with Close Button */}
DH

Deck Hearth

{/* Mobile Navigation Content */} {/* Mobile Bottom Section */}
{/* User Profile Dropdown */} setIsMobileMenuOpen(false)} /> {/* Icon Buttons Row - Support and Dark Mode */}
{/* Support Icon Button */} {/* Theme Toggle Icon Button */}
{/* Desktop Sidebar - Hidden on mobile */}
{/* Desktop Header */}
DH

Deck Hearth

{/* Desktop Navigation Content */} {/* Desktop Bottom Section */}
{/* User Profile Dropdown */} {}} /> {/* Icon Buttons Row - Support and Dark Mode */}
{/* Support Icon Button */} {/* Theme Toggle Icon Button */}
{/* Main Content */}
{/* Top Header - Only show search on dashboard */} {showSearch && (
setSearchQuery(e.target.value)} />
⌘F
)} {/* Page Content */}
{children}
); }