diff --git a/src/components/AppLayout.tsx b/src/components/AppLayout.tsx index 9868a6ea..2444e291 100644 --- a/src/components/AppLayout.tsx +++ b/src/components/AppLayout.tsx @@ -6,7 +6,6 @@ import { ProjectSidebar } from '@/components/ProjectSidebar'; import { Button } from '@/components/ui/button'; import { Menu, Columns2 } from 'lucide-react'; import { useIsMobile } from '@/hooks/useIsMobile'; -import { cn } from '@/lib/utils'; interface AppLayoutProps { children: React.ReactNode; @@ -16,35 +15,53 @@ interface AppLayoutProps { headerContent?: React.ReactNode; } -export function AppLayout({ - children, - selectedProject = null, +export function AppLayout({ + children, + selectedProject = null, title = "Shakespeare", showSidebar = true, - headerContent + headerContent }: AppLayoutProps) { - const [projects, setProjects] = useState([]); + const [_projects, setProjects] = useState([]); const projectsManager = useProjectsManager(); const navigate = useNavigate(); const isMobile = useIsMobile(); - + const [isSidebarVisible, setIsSidebarVisible] = useState(() => { - // Check if we're on mobile immediately to set correct initial state - if (typeof window !== 'undefined') { - return window.innerWidth >= 768; // Desktop starts open, mobile starts closed + // On mobile, always start hidden + if (typeof window !== 'undefined' && window.innerWidth < 768) { + return false; } - return false; // Default to closed for SSR + // On desktop, we'll set the initial state after projects load + return false; // Default to closed initially }); + // Track if we've set the initial state based on projects + const [hasSetInitialState, setHasSetInitialState] = useState(false); + const loadProjects = useCallback(async () => { try { await projectsManager.init(); const projectList = await projectsManager.getProjects(); setProjects(projectList); + + // Set initial sidebar state based on projects and device type + if (!hasSetInitialState && !isMobile) { + // On desktop, show sidebar if there are projects + setIsSidebarVisible(projectList.length > 0); + setHasSetInitialState(true); + } else if (!hasSetInitialState && isMobile) { + // On mobile, always start hidden + setIsSidebarVisible(false); + setHasSetInitialState(true); + } } catch (error) { console.error('Failed to load projects:', error); + if (!hasSetInitialState) { + setHasSetInitialState(true); + } } - }, [projectsManager]); + }, [projectsManager, hasSetInitialState, isMobile]); useEffect(() => { loadProjects(); @@ -65,11 +82,12 @@ export function AppLayout({ {/* Mobile Header */}
- {showSidebar && projects.length > 0 && ( + {showSidebar && ( @@ -81,8 +99,8 @@ export function AppLayout({ {headerContent}
- {/* Mobile Sidebar Overlay - only show if user has local projects and showSidebar is true */} - {showSidebar && projects.length > 0 && isSidebarVisible && ( + {/* Mobile Sidebar Overlay - show if showSidebar is true and sidebar is visible */} + {showSidebar && isSidebarVisible && (
{/* Backdrop - only covers the area not occupied by the sidebar */}
- {/* Fixed Sidebar - only show if user has created local projects and showSidebar is true */} - {showSidebar && projects.length > 0 && ( -
+ {/* Fixed Sidebar - show if showSidebar is true and sidebar is visible */} + {showSidebar && isSidebarVisible && ( +
-
- {/* Header - only show toggle button if user has local projects and showSidebar is true */} - {!isSidebarVisible && showSidebar && projects.length > 0 && ( -
- -

- {title} -

-
- {headerContent} -
-
+
+ {/* Toggle Button - positioned absolutely in top-left corner */} + {showSidebar && ( + )}