import { useEffect, useState, useCallback } from 'react' import { useLocation, useNavigate, Link } from 'react-router-dom' import { Menu, X, LayoutGrid, Clock, AlertTriangle, GitBranch, Code2, Wand2, BarChart3, Settings, LogOut, Shield, Layers } from 'lucide-react' import { useAuthStore } from '@/store/authStore' import { usePermissions } from '@/hooks/usePermissions' import { useUserPreferencesStore } from '@/store/userPreferencesStore' import { BrandLogo } from '@/components/common/BrandLogo' import { TopBar } from './TopBar' import { Sidebar } from './Sidebar' import { EmailVerificationBanner } from './EmailVerificationBanner' import { ViewTransitionOutlet } from './ViewTransitionOutlet' import { cn } from '@/lib/utils' export function AppLayout() { const location = useLocation() const navigate = useNavigate() const { user, logout } = useAuthStore() const { effectiveRole } = usePermissions() const sidebarPinned = useUserPreferencesStore(s => s.sidebarPinned) const [mobileMenuOpen, setMobileMenuOpen] = useState(false) // Close mobile menu on route change const [prevPath, setPrevPath] = useState(location.pathname) if (prevPath !== location.pathname) { setPrevPath(location.pathname) if (mobileMenuOpen) setMobileMenuOpen(false) } // Close on Escape const handleKeyDown = useCallback((e: KeyboardEvent) => { if (e.key === 'Escape') setMobileMenuOpen(false) }, []) useEffect(() => { if (mobileMenuOpen) { document.addEventListener('keydown', handleKeyDown) document.body.style.overflow = 'hidden' } else { document.body.style.overflow = '' } return () => { document.removeEventListener('keydown', handleKeyDown) document.body.style.overflow = '' } }, [mobileMenuOpen, handleKeyDown]) const handleLogout = async () => { setMobileMenuOpen(false) await logout() navigate('/login') } const mobileNavItems = [ { path: '/', label: 'Dashboard', icon: LayoutGrid }, { path: '/sessions', label: 'Active Sessions', icon: Clock }, { path: '/escalations', label: 'Escalations', icon: AlertTriangle }, { path: '/trees', label: 'Flows', icon: GitBranch }, { path: '/step-library', label: 'Step Library', icon: Layers }, { path: '/scripts', label: 'Scripts', icon: Code2 }, { path: '/script-builder', label: 'Script Builder', icon: Wand2 }, { path: '/analytics', label: 'Analytics', icon: BarChart3 }, { path: '/account', label: 'Account', icon: Settings }, ] return ( <>