import { useState } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import { useDesktop } from '@/hooks/useDesktop' import { useSidebarCollapsed, useSidebarSections } from '@/hooks/useSidebarCollapsed' import { emitSidebarAction } from '@/hooks/useSidebarAction' import { useAuth } from '@/hooks/useAuth' import { useUrlParams } from '@/hooks/useUrlParams' import { buildNavModel, type MoreDrawerItem, type NavPrimaryCta } from '@/components/navigation/moreDrawerItems' import { getPathWithReturnTo } from '@/lib/navigation' import { RepoQuickSwitchSheet } from '@/components/navigation/RepoQuickSwitchSheet' import { DesktopSessionTree } from '@/components/navigation/DesktopSessionTree' import { Sidebar, SidebarCollapsibleSection, SidebarSection, SidebarItem, } from '@/components/ui/sidebar' import { FolderGit2 } from 'lucide-react' const ACCOUNT_ITEM_KEYS = new Set(['settings', 'logout']) export function DesktopSidebar() { const location = useLocation() const navigate = useNavigate() const { updateParams } = useUrlParams() const [collapsed, toggle] = useSidebarCollapsed() const { isSectionOpen, toggleSection } = useSidebarSections(['sessions', 'menu'] as const) const [repoSwitcherOpen, setRepoSwitcherOpen] = useState(false) const { isAuthenticated, isLoading, logout } = useAuth() const isDesktop = useDesktop() if (isLoading || !isAuthenticated) { return null } if (!isDesktop) { return null } const { primary, items } = buildNavModel(location.pathname) const desktopPrimary = primary.filter((item) => item.key !== 'new-repo') const handlePrimaryClick = (item: NavPrimaryCta) => { if (item.to) { navigate(item.to) } else if (item.onSelect) { emitSidebarAction(item.onSelect) } } const handleItemClick = (item: MoreDrawerItem) => { if (item.to) { const to = item.key === 'schedules' ? getPathWithReturnTo(item.to, `${location.pathname}${location.search}`) : item.to navigate(to) } else if (item.dialog) { updateParams((p) => { p.set('dialog', item.dialog!) p.delete('mobileTab') }, 'push') } else if (item.key === 'logout') { logout() } else if (item.key === 'settings') { updateParams((p) => { p.set('settings', 'open') p.set('settingsTab', 'account') p.delete('mobileTab') }, 'push') } else if (item.key === 'repos') { setRepoSwitcherOpen(true) } } const [homeItem, ...routeItems] = items const navItems: MoreDrawerItem[] = [ homeItem, { key: 'repos', label: 'Repos', icon: FolderGit2 }, ...routeItems, ] const toolItems = navItems.filter((item) => !ACCOUNT_ITEM_KEYS.has(item.key)) const accountItems = navItems.filter((item) => ACCOUNT_ITEM_KEYS.has(item.key)) return ( <> {desktopPrimary.length > 0 && ( {desktopPrimary.map((item: NavPrimaryCta) => ( handlePrimaryClick(item)} asPrimary variant={item.variant} /> ))} )} {collapsed ? (
{navItems.map((item: MoreDrawerItem) => ( handleItemClick(item)} danger={item.danger} /> ))}
) : ( <> toggleSection('sessions')} className="border-t border-border" > toggleSection('menu')} className="flex-1 min-h-fit border-t border-border" contentClassName="overflow-y-auto" >
{toolItems.map((item: MoreDrawerItem) => ( handleItemClick(item)} danger={item.danger} /> ))}
{accountItems.map((item: MoreDrawerItem) => ( handleItemClick(item)} danger={item.danger} /> ))}
)}
setRepoSwitcherOpen(false)} /> ) }