refactor(frontend): drop sidebar action bus and primary CTAs

This commit is contained in:
Chris Scott committed 2026-10-01 23:56:55 -04:00
1 parent 5a8a81419c
commit 4cf69e9023
19 files changed
+51 -323

No files matched your search

@@ -1,98 +0,0 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { renderHook } from '@testing-library/react'
import { useSidebarAction, emitSidebarAction, SIDEBAR_ACTIONS } from './useSidebarAction'
describe('useSidebarAction', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.restoreAllMocks()
})
it('calls handler when matching event is dispatched', () => {
const handler = vi.fn()
renderHook(() => useSidebarAction('new-session', handler))
window.dispatchEvent(
new CustomEvent('oc:sidebar:action', {
detail: { action: 'new-session' },
})
)
expect(handler).toHaveBeenCalledTimes(1)
})
it('does not call handler for non-matching action', () => {
const handler = vi.fn()
renderHook(() => useSidebarAction('new-session', handler))
window.dispatchEvent(
new CustomEvent('oc:sidebar:action', {
detail: { action: 'new-repo' },
})
)
expect(handler).not.toHaveBeenCalled()
})
it('cleans up event listener on unmount', () => {
const handler = vi.fn()
const { unmount } = renderHook(() => useSidebarAction('new-session', handler))
unmount()
window.dispatchEvent(
new CustomEvent('oc:sidebar:action', {
detail: { action: 'new-session' },
})
)
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'])
})
})
-33
View File
@@ -1,33 +0,0 @@
import { useEffect } from 'react'
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(SIDEBAR_ACTION_EVENT, listener)
return () => {
window.removeEventListener(SIDEBAR_ACTION_EVENT, listener)
handlerCounts.set(action, Math.max(0, (handlerCounts.get(action) ?? 1) - 1))
}
}, [action, handler])
}