mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor(frontend): restructure sidebar navigation and add repo quick switch
- Restructure DesktopSidebar with separate primary and navigation sections - Add RepoQuickSwitchSheet integration for quick repo switching - Add New Session primary CTA to repo and session detail pages - Update sidebar collapse toggle icons and styling - Wrap Tooltip content in Portal for proper rendering - Integrate useSidebarAction hook for new-session keyboard shortcut
This commit is contained in:
1 parent
4f43bf9876
commit
a78e54a2c5
9 files changed
+125
-97
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { MemoryRouter, useLocation } from 'react-router-dom'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { DesktopSidebar } from './DesktopSidebar'
|
||||
import * as useDesktopModule from '@/hooks/useDesktop'
|
||||
import * as useSidebarCollapsedModule from '@/hooks/useSidebarCollapsed'
|
||||
@@ -18,6 +19,23 @@ function LocationDisplay() {
|
||||
return <div data-testid="location">{location.pathname}{location.search}</div>
|
||||
}
|
||||
|
||||
function createWrapper(initialEntries?: string[]) {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
retry: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MemoryRouter initialEntries={initialEntries}>
|
||||
{children}
|
||||
</MemoryRouter>
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('DesktopSidebar', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
@@ -90,11 +108,7 @@ describe('DesktopSidebar', () => {
|
||||
logout: vi.fn(),
|
||||
} as any)
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<DesktopSidebar />
|
||||
</MemoryRouter>
|
||||
)
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
expect(screen.getByText('New Repo')).toBeInTheDocument()
|
||||
expect(screen.getByText('Assistant')).toBeInTheDocument()
|
||||
@@ -110,17 +124,13 @@ describe('DesktopSidebar', () => {
|
||||
logout: vi.fn(),
|
||||
} as any)
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/repos/5']}>
|
||||
<DesktopSidebar />
|
||||
</MemoryRouter>
|
||||
)
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper(['/repos/5']) })
|
||||
|
||||
expect(screen.getByText('New Session')).toBeInTheDocument()
|
||||
expect(screen.getByText('Assistant')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders primary CTA for session detail', () => {
|
||||
it('renders primary CTAs for session detail', () => {
|
||||
vi.spyOn(useDesktopModule, 'useDesktop').mockReturnValue(true)
|
||||
vi.spyOn(useSidebarCollapsedModule, 'useSidebarCollapsed').mockReturnValue([false, vi.fn()])
|
||||
vi.spyOn(useMemoryPluginStatusModule, 'useMemoryPluginStatus').mockReturnValue({ memoryPluginEnabled: false })
|
||||
@@ -130,12 +140,9 @@ describe('DesktopSidebar', () => {
|
||||
logout: vi.fn(),
|
||||
} as any)
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/repos/5/sessions/abc']}>
|
||||
<DesktopSidebar />
|
||||
</MemoryRouter>
|
||||
)
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper(['/repos/5/sessions/abc']) })
|
||||
|
||||
expect(screen.getByText('New Session')).toBeInTheDocument()
|
||||
expect(screen.getByText('Assistant')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
@@ -149,11 +156,7 @@ describe('DesktopSidebar', () => {
|
||||
logout: vi.fn(),
|
||||
} as any)
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/schedules']}>
|
||||
<DesktopSidebar />
|
||||
</MemoryRouter>
|
||||
)
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper(['/schedules']) })
|
||||
|
||||
expect(screen.getByText('New Schedule')).toBeInTheDocument()
|
||||
expect(screen.getByText('Assistant')).toBeInTheDocument()
|
||||
@@ -170,11 +173,7 @@ describe('DesktopSidebar', () => {
|
||||
logout: vi.fn(),
|
||||
} as any)
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/repos/5']}>
|
||||
<DesktopSidebar />
|
||||
</MemoryRouter>
|
||||
)
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper(['/repos/5']) })
|
||||
|
||||
fireEvent.click(screen.getByText('New Session'))
|
||||
|
||||
@@ -197,10 +196,11 @@ describe('DesktopSidebar', () => {
|
||||
} as any)
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/repos/5/sessions/abc?assistant=1']}>
|
||||
<>
|
||||
<DesktopSidebar />
|
||||
<LocationDisplay />
|
||||
</MemoryRouter>
|
||||
</>,
|
||||
{ wrapper: createWrapper(['/repos/5/sessions/abc?assistant=1']) }
|
||||
)
|
||||
|
||||
fireEvent.click(screen.getByText('Files'))
|
||||
@@ -219,10 +219,11 @@ describe('DesktopSidebar', () => {
|
||||
} as any)
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/?dialog=files']}>
|
||||
<>
|
||||
<DesktopSidebar />
|
||||
<LocationDisplay />
|
||||
</MemoryRouter>
|
||||
</>,
|
||||
{ wrapper: createWrapper(['/?dialog=files']) }
|
||||
)
|
||||
|
||||
fireEvent.click(screen.getByText('Settings'))
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
import { useState } from 'react'
|
||||
import { useLocation, useNavigate } from 'react-router-dom'
|
||||
import { useDesktop } from '@/hooks/useDesktop'
|
||||
import { useSidebarCollapsed } from '@/hooks/useSidebarCollapsed'
|
||||
import { useMemoryPluginStatus } from '@/hooks/useMemoryPluginStatus'
|
||||
import { useAuth } from '@/hooks/useAuth'
|
||||
import { buildNavModel } from '@/components/navigation/moreDrawerItems'
|
||||
import { buildNavModel, type MoreDrawerItem, type NavPrimaryCta } from '@/components/navigation/moreDrawerItems'
|
||||
import { RepoQuickSwitchSheet } from '@/components/navigation/RepoQuickSwitchSheet'
|
||||
import {
|
||||
Sidebar,
|
||||
SidebarSection,
|
||||
SidebarItem,
|
||||
SidebarCollapseToggle,
|
||||
} from '@/components/ui/sidebar'
|
||||
import { LayoutGrid } from 'lucide-react'
|
||||
import { FolderGit2 } from 'lucide-react'
|
||||
|
||||
export function DesktopSidebar() {
|
||||
const location = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const [collapsed, toggle] = useSidebarCollapsed()
|
||||
const [repoSwitcherOpen, setRepoSwitcherOpen] = useState(false)
|
||||
const { memoryPluginEnabled } = useMemoryPluginStatus()
|
||||
const { isAuthenticated, isLoading, logout } = useAuth()
|
||||
|
||||
@@ -31,7 +34,7 @@ export function DesktopSidebar() {
|
||||
|
||||
const { primary, items } = buildNavModel(location.pathname, { memoryPluginEnabled })
|
||||
|
||||
const handlePrimaryClick = (item: (typeof primary)[number]) => {
|
||||
const handlePrimaryClick = (item: NavPrimaryCta) => {
|
||||
if (item.to) {
|
||||
navigate(item.to)
|
||||
} else if (item.onSelect) {
|
||||
@@ -43,7 +46,7 @@ export function DesktopSidebar() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleItemClick = (item: (typeof items)[number]) => {
|
||||
const handleItemClick = (item: MoreDrawerItem) => {
|
||||
if (item.to) {
|
||||
navigate(item.to)
|
||||
} else if (item.dialog) {
|
||||
@@ -58,54 +61,63 @@ export function DesktopSidebar() {
|
||||
params.set('settings', 'open')
|
||||
params.set('tab', 'account')
|
||||
navigate({ search: params.toString() }, { replace: true })
|
||||
} else if (item.key === 'repos') {
|
||||
setRepoSwitcherOpen(true)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Sidebar collapsed={collapsed} onToggle={toggle}>
|
||||
<div className="flex items-center justify-between p-2 border-b border-border">
|
||||
{!collapsed && (
|
||||
<span className="text-sm font-semibold px-2">OpenCode</span>
|
||||
)}
|
||||
{collapsed && (
|
||||
<div className="w-full flex justify-center">
|
||||
<LayoutGrid className="h-5 w-5" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
const navItems: MoreDrawerItem[] = [
|
||||
{ key: 'repos', label: 'Repos', icon: FolderGit2 },
|
||||
...items,
|
||||
]
|
||||
|
||||
{primary.length > 0 && (
|
||||
<SidebarSection collapsed={collapsed}>
|
||||
{primary.map((item) => (
|
||||
return (
|
||||
<>
|
||||
<Sidebar collapsed={collapsed} onToggle={toggle} className='mt-2'>
|
||||
|
||||
{primary.length > 0 && (
|
||||
<SidebarSection collapsed={collapsed}>
|
||||
{primary.map((item: NavPrimaryCta) => (
|
||||
<SidebarItem
|
||||
key={item.key}
|
||||
icon={item.icon}
|
||||
label={item.label}
|
||||
collapsed={collapsed}
|
||||
onClick={() => handlePrimaryClick(item)}
|
||||
asPrimary
|
||||
variant={item.variant}
|
||||
/>
|
||||
))}
|
||||
</SidebarSection>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-between px-2 py-1.5">
|
||||
{!collapsed && (
|
||||
<div className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
|
||||
Navigation
|
||||
</div>
|
||||
)}
|
||||
<SidebarCollapseToggle collapsed={collapsed} onToggle={toggle} />
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1 p-2 pt-0">
|
||||
{navItems.map((item: MoreDrawerItem) => (
|
||||
<SidebarItem
|
||||
key={item.key}
|
||||
icon={item.icon}
|
||||
label={item.label}
|
||||
collapsed={collapsed}
|
||||
onClick={() => handlePrimaryClick(item)}
|
||||
asPrimary
|
||||
variant={item.variant}
|
||||
onClick={() => handleItemClick(item)}
|
||||
danger={item.danger}
|
||||
/>
|
||||
))}
|
||||
</SidebarSection>
|
||||
)}
|
||||
</div>
|
||||
</Sidebar>
|
||||
|
||||
<SidebarSection label="Navigation" collapsed={collapsed}>
|
||||
{items.map((item) => (
|
||||
<SidebarItem
|
||||
key={item.key}
|
||||
icon={item.icon}
|
||||
label={item.label}
|
||||
collapsed={collapsed}
|
||||
onClick={() => handleItemClick(item)}
|
||||
danger={item.danger}
|
||||
/>
|
||||
))}
|
||||
</SidebarSection>
|
||||
|
||||
<div className="mt-auto">
|
||||
<SidebarCollapseToggle collapsed={collapsed} onToggle={toggle} />
|
||||
</div>
|
||||
</Sidebar>
|
||||
<RepoQuickSwitchSheet
|
||||
isOpen={repoSwitcherOpen}
|
||||
onClose={() => setRepoSwitcherOpen(false)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -98,7 +98,7 @@ describe('RepoQuickSwitchSheet', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('navigates on repo click without calling onClose', async () => {
|
||||
it('navigates on repo click and closes sheet', async () => {
|
||||
vi.mocked(listRepos).mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
@@ -120,7 +120,7 @@ describe('RepoQuickSwitchSheet', () => {
|
||||
expect(screen.getByText('repo1')).toBeInTheDocument()
|
||||
})
|
||||
fireEvent.click(screen.getByText('repo1'))
|
||||
expect(handleClose).not.toHaveBeenCalled()
|
||||
expect(handleClose).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('navigates directly to assistant when mobileTabAction is assistant', async () => {
|
||||
@@ -162,6 +162,6 @@ describe('RepoQuickSwitchSheet', () => {
|
||||
fireEvent.click(screen.getByText('repo1'))
|
||||
|
||||
expect(screen.getByTestId('location')).toHaveTextContent('/repos/1/assistant')
|
||||
expect(handleClose).not.toHaveBeenCalled()
|
||||
expect(handleClose).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -47,10 +47,12 @@ export function RepoQuickSwitchSheet({ isOpen, onClose }: RepoQuickSwitchSheetPr
|
||||
const pendingAction = new URLSearchParams(location.search).get('mobileTabAction')
|
||||
if (pendingAction === 'assistant') {
|
||||
navigate(`/repos/${id}/assistant`)
|
||||
onClose()
|
||||
return
|
||||
}
|
||||
|
||||
navigate(`/repos/${id}`, { replace: true })
|
||||
onClose()
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -128,12 +128,15 @@ describe('buildNavModel', () => {
|
||||
expect(model.primary[1].to).toBe('/repos/5/assistant')
|
||||
})
|
||||
|
||||
it('returns assistant-only primary CTA for session detail', () => {
|
||||
it('returns new-session and assistant primary CTAs for session detail', () => {
|
||||
const model = buildNavModel('/repos/5/sessions/abc')
|
||||
expect(model.primary).toHaveLength(1)
|
||||
expect(model.primary[0].key).toBe('assistant')
|
||||
expect(model.primary[0].to).toBe('/repos/5/assistant')
|
||||
expect(model.primary).toHaveLength(2)
|
||||
expect(model.primary[0].key).toBe('new-session')
|
||||
expect(model.primary[0].onSelect).toBe('new-session')
|
||||
expect(model.primary[0].variant).toBe('primary')
|
||||
expect(model.primary[1].key).toBe('assistant')
|
||||
expect(model.primary[1].to).toBe('/repos/5/assistant')
|
||||
expect(model.primary[1].variant).toBe('secondary')
|
||||
})
|
||||
|
||||
it('returns assistant primary CTA for assistant workspace', () => {
|
||||
|
||||
@@ -96,7 +96,8 @@ export function buildNavModel(pathname: string, options: BuildNavOptions = {}):
|
||||
|
||||
return {
|
||||
primary: [
|
||||
getAssistantNavItem(pathname, 'primary'),
|
||||
{ key: 'new-session', label: 'New Session', icon: SquarePlus, onSelect: 'new-session', variant: 'primary' },
|
||||
getAssistantNavItem(pathname),
|
||||
],
|
||||
items,
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-react'
|
||||
import { PanelLeft, PanelLeftClose } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
|
||||
|
||||
export interface SidebarProps {
|
||||
@@ -122,15 +121,18 @@ export interface SidebarCollapseToggleProps {
|
||||
|
||||
export function SidebarCollapseToggle({ collapsed, onToggle }: SidebarCollapseToggleProps) {
|
||||
return (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
<button
|
||||
type="button"
|
||||
onClick={onToggle}
|
||||
className="w-full rounded-none border-t border-border"
|
||||
className={cn(
|
||||
'rounded-md p-2.5',
|
||||
'hover:bg-accent hover:text-accent-foreground',
|
||||
'transition-colors duration-150'
|
||||
)}
|
||||
title={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
|
||||
aria-label={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
|
||||
>
|
||||
{collapsed ? <ChevronRight className="h-4 w-4" /> : <ChevronLeft className="h-4 w-4" />}
|
||||
<span className="sr-only">{collapsed ? 'Expand' : 'Collapse'}</span>
|
||||
</Button>
|
||||
{collapsed ? <PanelLeft className="h-5 w-5" /> : <PanelLeftClose className="h-5 w-5" />}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
@@ -12,15 +12,17 @@ const TooltipContent = React.forwardRef<
|
||||
React.ElementRef<typeof TooltipPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
|
||||
>(({ className, sideOffset = 4, ...props }, ref) => (
|
||||
<TooltipPrimitive.Content
|
||||
ref={ref}
|
||||
sideOffset={sideOffset}
|
||||
className={cn(
|
||||
'z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
<TooltipPrimitive.Portal>
|
||||
<TooltipPrimitive.Content
|
||||
ref={ref}
|
||||
sideOffset={sideOffset}
|
||||
className={cn(
|
||||
'z-[100] overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
</TooltipPrimitive.Portal>
|
||||
))
|
||||
TooltipContent.displayName = TooltipPrimitive.Content.displayName
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ import { PendingActionsGroup } from "@/components/notifications/PendingActionsGr
|
||||
import { SourceControlPanel } from "@/components/source-control";
|
||||
import { SessionTodoDisplay } from "@/components/message/SessionTodoDisplay";
|
||||
import { useDialogParam } from "@/hooks/useDialogParam";
|
||||
import { useSidebarAction } from "@/hooks/useSidebarAction";
|
||||
import { SessionMoreButton } from "@/components/navigation/SessionMoreButton";
|
||||
|
||||
const compareMessageIds = (id1: string, id2: string): number => {
|
||||
@@ -227,6 +228,10 @@ export function SessionDetail() {
|
||||
}
|
||||
}, [createSession, navigate, repoId, sessionRouteSuffix]);
|
||||
|
||||
useSidebarAction('new-session', () => {
|
||||
handleNewSession();
|
||||
});
|
||||
|
||||
const handleCompact = useCallback(async () => {
|
||||
if (!opcodeUrl || !sessionId) return;
|
||||
if (!model?.providerID || !model?.modelID) {
|
||||
|
||||
Reference in new issue
Block a user