import { useState, useEffect, useCallback } from 'react'; import { useNavigate, useLocation, Outlet } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Menu } from 'lucide-react'; import { ProjectSidebar } from '@/components/ProjectSidebar'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { useIsMobile } from '@/hooks/useIsMobile'; import { type Project } from '@/lib/ProjectsManager'; import { getSettingsCategories } from '@/lib/settingsItems'; import { SettingsItem } from '@/components/SettingsItem'; export function SettingsLayout() { const { t } = useTranslation(); const navigate = useNavigate(); const location = useLocation(); const isMobile = useIsMobile(); const projectsManager = useProjectsManager(); const [_projects, setProjects] = useState([]); const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile); 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(true); 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, hasSetInitialState, isMobile]); useEffect(() => { loadProjects(); }, [loadProjects]); const handleProjectSelect = (project: Project | null) => { if (project) { setIsSidebarVisible(false); // Collapse sidebar on mobile navigation navigate(`/project/${project.id}`); } else { navigate('/'); } }; // Get current settings page const settingsCategories = getSettingsCategories(t); const currentPath = location.pathname; const currentSettingsId = settingsCategories .flatMap(category => category.items) .find(item => item.href === currentPath)?.id; if (isMobile) { return (
{/* Mobile Header */}

{t('settings')}

{/* Mobile Sidebar Overlay */} {isSidebarVisible && (
setIsSidebarVisible(false)} />
{ setIsSidebarVisible(false); handleProjectSelect(project); }} onClose={() => setIsSidebarVisible(false)} />
)}
); } // Desktop 3-column layout return (
{/* Left Column - Projects Sidebar (optionally hidden) */} {isSidebarVisible && (
setIsSidebarVisible(false)} />
)} {/* Middle Column - Settings Navigation */}
{/* Header with toggle button */}
{!isSidebarVisible && ( )}

{t('settings')}

{/* Settings Navigation - Scrollable */}
{settingsCategories.map((category) => (

{category.title}

{category.items.map((item) => { const isActive = currentSettingsId === item.id; return ( navigate(item.href)} isActive={isActive} /> ); })}
))}
{/* Right Column - Settings Content - Scrollable */}
); }