import { useState, useEffect } 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, Bot, GitBranch, Database, Wifi, Settings2, Info } 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 { useCallback } from 'react'; import { cn } from '@/lib/utils'; interface SettingsItem { id: string; title: string; description: string; icon: React.ComponentType<{ className?: string }>; href: string; } // Define settings items as a function to get fresh translations const getSettingsItems = (t: (key: string) => string): SettingsItem[] => [ { id: 'preferences', title: t('preferences'), description: t('preferencesDescription'), icon: Settings2, href: '/settings/preferences', }, { id: 'ai', title: t('aiSettings'), description: t('aiSettingsDescription'), icon: Bot, href: '/settings/ai', }, { id: 'git', title: t('gitSettings'), description: t('gitSettingsDescription'), icon: GitBranch, href: '/settings/git', }, { id: 'nostr', title: t('nostrSettings'), description: t('nostrSettingsDescription'), icon: Wifi, href: '/settings/nostr', }, { id: 'data', title: t('dataSettings'), description: t('dataSettingsDescription'), icon: Database, href: '/settings/data', }, { id: 'about', title: t('aboutShakespeare'), description: t('aboutShakespeareDescription'), icon: Info, href: '/settings/about', }, ]; 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 settingsItems = getSettingsItems(t); const currentPath = location.pathname; const currentSettingsId = settingsItems.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 */}
{settingsItems.map((item) => { const Icon = item.icon; const isActive = currentSettingsId === item.id; return ( ); })}
{/* Right Column - Settings Content - Scrollable */}
); }