diff --git a/frontend/src/components/navigation/DesktopSidebar.tsx b/frontend/src/components/navigation/DesktopSidebar.tsx index feb46608..5c804f86 100644 --- a/frontend/src/components/navigation/DesktopSidebar.tsx +++ b/frontend/src/components/navigation/DesktopSidebar.tsx @@ -2,6 +2,7 @@ import { useState } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import { useDesktop } from '@/hooks/useDesktop' import { useSidebarCollapsed } 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,11 +40,7 @@ export function DesktopSidebar() { if (item.to) { navigate(item.to) } else if (item.onSelect) { - window.dispatchEvent( - new CustomEvent('oc:sidebar:action', { - detail: { action: item.onSelect }, - }) - ) + emitSidebarAction(item.onSelect) } } diff --git a/frontend/src/components/navigation/moreDrawerItems.ts b/frontend/src/components/navigation/moreDrawerItems.ts index 23d451cf..169770e5 100644 --- a/frontend/src/components/navigation/moreDrawerItems.ts +++ b/frontend/src/components/navigation/moreDrawerItems.ts @@ -1,6 +1,7 @@ import type { LucideIcon } from 'lucide-react' import { Plug, Sparkles, ShieldOff, CalendarClock, GitCommitHorizontal, Code2, Settings, LogOut, Plus, Bot, Folder, Clock, SquarePlus } from 'lucide-react' import { getAssistantPath, isAssistantPath } from '@/lib/navigation' +import type { SidebarActionKey } from '@/hooks/useSidebarAction' export interface MoreDrawerItem { key: string @@ -16,7 +17,7 @@ export interface NavPrimaryCta { label: string icon: LucideIcon to?: string - onSelect?: 'new-session' | 'new-repo' | 'new-schedule' + onSelect?: SidebarActionKey variant?: 'primary' | 'secondary' } diff --git a/frontend/src/hooks/useSidebarAction.test.tsx b/frontend/src/hooks/useSidebarAction.test.tsx index 0a108e0c..5e9bb2bc 100644 --- a/frontend/src/hooks/useSidebarAction.test.tsx +++ b/frontend/src/hooks/useSidebarAction.test.tsx @@ -1,6 +1,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { renderHook } from '@testing-library/react' -import { useSidebarAction } from './useSidebarAction' +import { useSidebarAction, emitSidebarAction, SIDEBAR_ACTIONS } from './useSidebarAction' describe('useSidebarAction', () => { beforeEach(() => { @@ -54,3 +54,45 @@ describe('useSidebarAction', () => { expect(handler).not.toHaveBeenCalled() }) }) + +describe('emitSidebarAction', () => { + afterEach(() => { + vi.restoreAllMocks() + }) + + it('reaches a registered handler without warning', () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) + const handler = vi.fn() + + renderHook(() => useSidebarAction('new-schedule', handler)) + emitSidebarAction('new-schedule') + + expect(handler).toHaveBeenCalledTimes(1) + expect(warn).not.toHaveBeenCalled() + }) + + it('warns when an action is dispatched with no mounted handler', () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) + + emitSidebarAction('new-schedule') + + expect(warn).toHaveBeenCalledWith(expect.stringContaining('new-schedule')) + }) + + it('warns again once the last handler unmounts', () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) + const { unmount } = renderHook(() => useSidebarAction('new-repo', vi.fn())) + + emitSidebarAction('new-repo') + expect(warn).not.toHaveBeenCalled() + + unmount() + emitSidebarAction('new-repo') + + expect(warn).toHaveBeenCalledWith(expect.stringContaining('new-repo')) + }) + + it('exposes every action the nav model can emit', () => { + expect([...SIDEBAR_ACTIONS]).toEqual(['new-session', 'new-repo', 'new-schedule']) + }) +}) diff --git a/frontend/src/hooks/useSidebarAction.ts b/frontend/src/hooks/useSidebarAction.ts index 92b28721..2de1188c 100644 --- a/frontend/src/hooks/useSidebarAction.ts +++ b/frontend/src/hooks/useSidebarAction.ts @@ -1,16 +1,33 @@ import { useEffect } from 'react' -type SidebarActionKey = 'new-session' | 'new-repo' | 'new-schedule' +export const SIDEBAR_ACTIONS = ['new-session', 'new-repo', 'new-schedule'] as const + +export type SidebarActionKey = (typeof SIDEBAR_ACTIONS)[number] + +const SIDEBAR_ACTION_EVENT = 'oc:sidebar:action' + +const handlerCounts = new Map() + +export function emitSidebarAction(action: SidebarActionKey) { + if (!handlerCounts.get(action)) { + console.warn(`Sidebar action "${action}" was dispatched but no mounted page handles it`) + } + window.dispatchEvent(new CustomEvent(SIDEBAR_ACTION_EVENT, { detail: { action } })) +} export function useSidebarAction(action: SidebarActionKey, handler: () => void) { useEffect(() => { + handlerCounts.set(action, (handlerCounts.get(action) ?? 0) + 1) const listener = (event: Event) => { const detail = (event as CustomEvent<{ action?: SidebarActionKey }>).detail if (detail?.action === action) { handler() } } - window.addEventListener('oc:sidebar:action', listener) - return () => window.removeEventListener('oc:sidebar:action', listener) + window.addEventListener(SIDEBAR_ACTION_EVENT, listener) + return () => { + window.removeEventListener(SIDEBAR_ACTION_EVENT, listener) + handlerCounts.set(action, Math.max(0, (handlerCounts.get(action) ?? 1) - 1)) + } }, [action, handler]) } diff --git a/frontend/src/pages/AssistantRedirect.tsx b/frontend/src/pages/AssistantRedirect.tsx index 2b211434..ec863dc2 100644 --- a/frontend/src/pages/AssistantRedirect.tsx +++ b/frontend/src/pages/AssistantRedirect.tsx @@ -4,6 +4,7 @@ import { useQuery, useQueryClient } from "@tanstack/react-query" import { getRepo } from "@/api/repos" import { useCreateSession } from "@/hooks/useOpenCode" import { useDialogParam } from "@/hooks/useDialogParam" +import { useSidebarAction } from "@/hooks/useSidebarAction" import { useSSE } from "@/hooks/useSSE" import { OPENCODE_API_ENDPOINT } from "@/config" import { Button } from "@/components/ui/button" @@ -49,6 +50,10 @@ export function AssistantRedirect() { await createSessionMutation.mutateAsync({ agent: undefined }) } + useSidebarAction('new-session', () => { + handleCreateSession() + }) + return (
diff --git a/frontend/src/pages/GlobalSchedules.tsx b/frontend/src/pages/GlobalSchedules.tsx index c3f40583..592f7b2a 100644 --- a/frontend/src/pages/GlobalSchedules.tsx +++ b/frontend/src/pages/GlobalSchedules.tsx @@ -16,6 +16,7 @@ import { CalendarClock, Loader2, Plus, ArrowLeft, Play, Pencil, Trash2, Pause, P import { useScheduleUrlState } from '@/hooks/useScheduleUrlState' import type { ScheduleTab } from '@/hooks/useScheduleUrlState' +import { useSidebarAction } from '@/hooks/useSidebarAction' import type { ScheduleJobWithRepo, ScheduleRunWithContext } from '@/api/schedules' import { Combobox } from '@/components/ui/combobox' @@ -46,6 +47,11 @@ export function GlobalSchedules() { const cancelRunMutation = useCancelRepoScheduleRun() const cancelRunPending = cancelRunMutation.isPending + useSidebarAction('new-schedule', () => { + openNewJob() + setSelectedRepoId(undefined) + }) + useEffect(() => { runOffsetRef.current = runOffset }, [runOffset]) diff --git a/frontend/src/pages/Schedules.tsx b/frontend/src/pages/Schedules.tsx index 26133a8d..0642daf7 100644 --- a/frontend/src/pages/Schedules.tsx +++ b/frontend/src/pages/Schedules.tsx @@ -17,6 +17,7 @@ import { import { useRepoActivity } from '@/hooks/useRepoActivity' import { useScheduleTarget } from '@/hooks/useScheduleTarget' import { useScheduleUrlState } from '@/hooks/useScheduleUrlState' +import { useSidebarAction } from '@/hooks/useSidebarAction' import { ScheduleJobDialog, JobsTab, JobDetailTab, RunHistoryTab, ScheduleTabMenu } from '@/components/schedules' import { toUpdateScheduleRequest } from '@/components/schedules/schedule-utils' import { Header } from '@/components/ui/header' @@ -69,6 +70,10 @@ export function Schedules() { const [clearRunsOpen, setClearRunsOpen] = useState(false) const [runToDelete, setRunToDelete] = useState(null) + useSidebarAction('new-schedule', () => { + openNewJob() + }) + const clearableRuns = useMemo(() => (runs ?? []).filter((run) => run.status !== 'running'), [runs]) const clearableWorktrees = useMemo(() => clearableRuns.filter((run) => run.worktreePath).length, [clearableRuns]) const clearableBranches = useMemo(() => clearableRuns.filter((run) => run.runBranch).length, [clearableRuns])