mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor(frontend): drop sidebar action bus and primary CTAs
This commit is contained in:
1 parent
5a8a81419c
commit
4cf69e9023
19 files changed
+51
-323
No files matched your search
@@ -149,7 +149,11 @@ describe('DesktopSidebar', () => {
|
||||
expect(screen.getByText('Assistant')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders primary CTA for schedules routes', () => {
|
||||
it.each([
|
||||
['/schedules', 'New Schedule'],
|
||||
['/repos/5/schedules', 'New Schedule'],
|
||||
['/assistant', 'New Session'],
|
||||
])('does not duplicate the page header create action on %s', (path, headerAction) => {
|
||||
vi.spyOn(useDesktopModule, 'useDesktop').mockReturnValue(true)
|
||||
vi.spyOn(useSidebarCollapsedModule, 'useSidebarCollapsed').mockReturnValue([false, vi.fn()])
|
||||
vi.spyOn(useAuthModule, 'useAuth').mockReturnValue({
|
||||
@@ -158,34 +162,12 @@ describe('DesktopSidebar', () => {
|
||||
logout: vi.fn(),
|
||||
} as any)
|
||||
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper(['/schedules']) })
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper([path]) })
|
||||
|
||||
expect(screen.getByText('New Schedule')).toBeInTheDocument()
|
||||
expect(screen.queryByText(headerAction)).toBeNull()
|
||||
expect(screen.getByText('Assistant')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('dispatches oc:sidebar:action event when primary CTA is clicked', () => {
|
||||
const dispatchEventSpy = vi.spyOn(window, 'dispatchEvent')
|
||||
vi.spyOn(useDesktopModule, 'useDesktop').mockReturnValue(true)
|
||||
vi.spyOn(useSidebarCollapsedModule, 'useSidebarCollapsed').mockReturnValue([false, vi.fn()])
|
||||
vi.spyOn(useAuthModule, 'useAuth').mockReturnValue({
|
||||
isAuthenticated: true,
|
||||
isLoading: false,
|
||||
logout: vi.fn(),
|
||||
} as any)
|
||||
|
||||
render(<DesktopSidebar />, { wrapper: createWrapper(['/schedules']) })
|
||||
|
||||
fireEvent.click(screen.getByText('New Schedule'))
|
||||
|
||||
expect(dispatchEventSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'oc:sidebar:action',
|
||||
detail: { action: 'new-schedule' },
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('opens dialog items by updating the dialog query param (push) and closes on back', () => {
|
||||
vi.spyOn(useDesktopModule, 'useDesktop').mockReturnValue(true)
|
||||
vi.spyOn(useSidebarCollapsedModule, 'useSidebarCollapsed').mockReturnValue([false, vi.fn()])
|
||||
|
||||
@@ -2,7 +2,6 @@ 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'
|
||||
@@ -39,15 +38,6 @@ export function DesktopSidebar() {
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -85,15 +75,15 @@ export function DesktopSidebar() {
|
||||
return (
|
||||
<>
|
||||
<Sidebar collapsed={collapsed} onToggle={toggle} widthClass='w-72' className='mt-2'>
|
||||
{desktopPrimary.length > 0 && (
|
||||
{primary.length > 0 && (
|
||||
<SidebarSection collapsed={collapsed}>
|
||||
{desktopPrimary.map((item: NavPrimaryCta) => (
|
||||
{primary.map((item: NavPrimaryCta) => (
|
||||
<SidebarItem
|
||||
key={item.key}
|
||||
icon={item.icon}
|
||||
label={item.label}
|
||||
collapsed={collapsed}
|
||||
onClick={() => handlePrimaryClick(item)}
|
||||
onClick={() => navigate(item.to)}
|
||||
asPrimary
|
||||
variant={item.variant}
|
||||
/>
|
||||
|
||||
@@ -109,75 +109,23 @@ describe('buildMoreItems', () => {
|
||||
})
|
||||
|
||||
describe('buildNavModel', () => {
|
||||
it('returns new-repo primary CTA for root path', () => {
|
||||
const model = buildNavModel('/')
|
||||
expect(model.primary).toHaveLength(2)
|
||||
expect(model.primary[0].key).toBe('new-repo')
|
||||
expect(model.primary[0].onSelect).toBe('new-repo')
|
||||
expect(model.primary[1].key).toBe('assistant')
|
||||
expect(model.primary[1].to).toBe('/assistant')
|
||||
})
|
||||
|
||||
it('returns only the assistant primary CTA for repo detail', () => {
|
||||
const model = buildNavModel('/repos/5')
|
||||
expect(model.primary).toHaveLength(1)
|
||||
expect(model.primary[0].key).toBe('assistant')
|
||||
expect(model.primary[0].to).toBe('/assistant')
|
||||
})
|
||||
|
||||
it('returns only the assistant primary CTA for session detail', () => {
|
||||
const model = buildNavModel('/repos/5/sessions/abc')
|
||||
it.each([
|
||||
'/',
|
||||
'/repos/5',
|
||||
'/repos/5/sessions/abc',
|
||||
'/repos/5/assistant',
|
||||
'/assistant',
|
||||
'/schedules',
|
||||
'/repos/5/schedules',
|
||||
'/unknown/path',
|
||||
])('returns only the assistant primary CTA for %s', (path) => {
|
||||
const model = buildNavModel(path)
|
||||
expect(model.primary).toHaveLength(1)
|
||||
expect(model.primary[0].key).toBe('assistant')
|
||||
expect(model.primary[0].to).toBe('/assistant')
|
||||
expect(model.primary[0].variant).toBe('secondary')
|
||||
})
|
||||
|
||||
it('returns new-session and assistant primary CTAs for assistant workspace', () => {
|
||||
const model = buildNavModel('/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('/assistant')
|
||||
expect(model.primary[1].variant).toBe('secondary')
|
||||
})
|
||||
|
||||
it('returns new-session and assistant primary CTAs for canonical /assistant', () => {
|
||||
const model = buildNavModel('/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('/assistant')
|
||||
expect(model.primary[1].variant).toBe('secondary')
|
||||
})
|
||||
|
||||
it('returns new-schedule primary CTA for schedules routes', () => {
|
||||
const model1 = buildNavModel('/schedules')
|
||||
expect(model1.primary).toHaveLength(2)
|
||||
expect(model1.primary[0].key).toBe('new-schedule')
|
||||
expect(model1.primary[0].onSelect).toBe('new-schedule')
|
||||
expect(model1.primary[1].key).toBe('assistant')
|
||||
expect(model1.primary[1].to).toBe('/assistant')
|
||||
|
||||
const model2 = buildNavModel('/repos/5/schedules')
|
||||
expect(model2.primary).toHaveLength(2)
|
||||
expect(model2.primary[0].key).toBe('new-schedule')
|
||||
expect(model2.primary[0].onSelect).toBe('new-schedule')
|
||||
expect(model2.primary[1].key).toBe('assistant')
|
||||
expect(model2.primary[1].to).toBe('/assistant')
|
||||
})
|
||||
|
||||
it('returns assistant primary for unknown routes', () => {
|
||||
const model = buildNavModel('/unknown/path')
|
||||
expect(model.primary).toHaveLength(1)
|
||||
expect(model.primary[0].key).toBe('assistant')
|
||||
expect(model.primary[0].to).toBe('/assistant')
|
||||
})
|
||||
|
||||
it('preserves backwards compatibility with buildMoreItems', () => {
|
||||
const model = buildNavModel('/repos/42')
|
||||
const items = buildMoreItems('/repos/42')
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import { Plug, Sparkles, ShieldOff, CalendarClock, GitCommitHorizontal, Settings, LogOut, Plus, Bot, Folder, Clock, SquarePlus, Home } from 'lucide-react'
|
||||
import { Plug, Sparkles, ShieldOff, CalendarClock, GitCommitHorizontal, Settings, LogOut, Bot, Folder, Home } from 'lucide-react'
|
||||
import { getAssistantPath, isAssistantPath } from '@/lib/navigation'
|
||||
import type { SidebarActionKey } from '@/hooks/useSidebarAction'
|
||||
|
||||
export interface MoreDrawerItem {
|
||||
key: string
|
||||
@@ -16,8 +15,7 @@ export interface NavPrimaryCta {
|
||||
key: string
|
||||
label: string
|
||||
icon: LucideIcon
|
||||
to?: string
|
||||
onSelect?: SidebarActionKey
|
||||
to: string
|
||||
variant?: 'primary' | 'secondary'
|
||||
}
|
||||
|
||||
@@ -26,13 +24,13 @@ export interface NavModel {
|
||||
items: MoreDrawerItem[]
|
||||
}
|
||||
|
||||
function getAssistantNavItem(_pathname: string, variant: NavPrimaryCta['variant'] = 'secondary'): NavPrimaryCta {
|
||||
function getAssistantNavItem(): NavPrimaryCta {
|
||||
return {
|
||||
key: 'assistant',
|
||||
label: 'Assistant',
|
||||
icon: Bot,
|
||||
to: getAssistantPath(),
|
||||
variant,
|
||||
variant: 'secondary',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +62,7 @@ function buildRouteNavModel(pathname: string): NavModel {
|
||||
]
|
||||
|
||||
return {
|
||||
primary: [getAssistantNavItem(pathname)],
|
||||
primary: [getAssistantNavItem()],
|
||||
items,
|
||||
}
|
||||
}
|
||||
@@ -82,7 +80,7 @@ function buildRouteNavModel(pathname: string): NavModel {
|
||||
]
|
||||
|
||||
return {
|
||||
primary: [getAssistantNavItem(pathname)],
|
||||
primary: [getAssistantNavItem()],
|
||||
items,
|
||||
}
|
||||
}
|
||||
@@ -99,30 +97,14 @@ function buildRouteNavModel(pathname: string): NavModel {
|
||||
]
|
||||
|
||||
return {
|
||||
primary: [
|
||||
{ key: 'new-session', label: 'New Session', icon: SquarePlus, onSelect: 'new-session', variant: 'primary' },
|
||||
getAssistantNavItem(pathname, 'secondary'),
|
||||
],
|
||||
primary: [getAssistantNavItem()],
|
||||
items,
|
||||
}
|
||||
}
|
||||
|
||||
if (pathname === '/schedules' || /^\/repos\/\d+\/schedules$/.test(pathname)) {
|
||||
return {
|
||||
primary: [
|
||||
{ key: 'new-schedule', label: 'New Schedule', icon: Clock, onSelect: 'new-schedule', variant: 'primary' },
|
||||
getAssistantNavItem(pathname),
|
||||
],
|
||||
items: baseItems,
|
||||
}
|
||||
}
|
||||
|
||||
if (pathname === '/') {
|
||||
return {
|
||||
primary: [
|
||||
{ key: 'new-repo', label: 'New Repo', icon: Plus, onSelect: 'new-repo', variant: 'primary' },
|
||||
getAssistantNavItem(pathname),
|
||||
],
|
||||
primary: [getAssistantNavItem()],
|
||||
items: [
|
||||
{ key: 'all-schedules', label: 'All Schedules', icon: CalendarClock, to: '/schedules' },
|
||||
{ key: 'files', label: 'Files', icon: Folder, dialog: 'files' },
|
||||
@@ -132,9 +114,7 @@ function buildRouteNavModel(pathname: string): NavModel {
|
||||
}
|
||||
|
||||
return {
|
||||
primary: [
|
||||
getAssistantNavItem(pathname),
|
||||
],
|
||||
primary: [getAssistantNavItem()],
|
||||
items: baseItems,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user