fix(sidebar): wire new-session and new-schedule actions to every page

AssistantRedirect never registered a listener for the sidebar's
new-session event, so the desktop sidebar CTA on /assistant was a
no-op; the Schedules pages had the same gap for new-schedule.

Make useSidebarAction the single source of truth for action keys and
event dispatch, and warn when an action is dispatched with no mounted
handler so an unhandled CTA fails loudly instead of silently. Wire the
missing listeners to the existing per-page handlers.
This commit is contained in:
Chris Scott committed 2026-08-25 10:22:51 -04:00
1 parent 62cf04bbc9
commit 9cbe7870b2
7 files changed
+83 -10

No files matched your search

@@ -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)
}
}
@@ -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'
}
+43 -1
View File
@@ -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'])
})
})
+20 -3
View File
@@ -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<SidebarActionKey, number>()
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])
}
+5
View File
@@ -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 (
<div className="h-dvh max-h-dvh overflow-hidden bg-gradient-to-br from-background via-background to-background flex flex-col pb-[calc(env(safe-area-inset-bottom)+56px)] sm:pb-0">
<Header>
+6
View File
@@ -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])
+5
View File
@@ -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<number | null>(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])