Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(desktop-app): add open-chats switcher to the chat drawer header (Fase 2)

Diego Zacarias6c9036d1de92
diff --git a/desktop-app/src/__tests__/desktopCommands.test.ts b/desktop-app/src/__tests__/desktopCommands.test.ts
index 69c041fa2..750c4c04d 100644
--- a/desktop-app/src/__tests__/desktopCommands.test.ts
+++ b/desktop-app/src/__tests__/desktopCommands.test.ts
@@ -103,6 +103,16 @@ describe('Desktop command registry', () => {
expect(DESKTOP_COMMANDS.some(command => command.id === 'tabs.select9')).toBe(false)
})
+ it('binds the chat switcher to Mod+E, gated on a mounted app, without colliding', () => {
+ const switcher = getDesktopCommand('chat.switcher')
+ expect(switcher.defaultBinding).toEqual({ key: 'e', modifier: 'mod' })
+ expect(switcher.eligibility).toBe('app-mounted')
+ expect(matchDesktopCommand(input({ key: 'e' }), 'darwin', 'host')?.id).toBe('chat.switcher')
+ expect(matchDesktopCommand(input({ key: 'e', meta: false, control: true }), 'win32', 'host')?.id).toBe(
+ 'chat.switcher'
+ )
+ })
+
it('uses Control for tab cycling on macOS to avoid the reserved Command+Tab app switcher', () => {
expect(
matchDesktopCommand(input({ key: 'Tab', meta: false, control: true }), 'darwin', 'host')?.id
diff --git a/desktop-app/src/desktopCommands.ts b/desktop-app/src/desktopCommands.ts
index 4e2293d5f..b745fe81e 100644
--- a/desktop-app/src/desktopCommands.ts
+++ b/desktop-app/src/desktopCommands.ts
@@ -100,6 +100,23 @@ const definitions = [
visibleInPalette: true,
visibleInSettings: true,
},
+ {
+ id: 'chat.switcher',
+ label: 'Open chats switcher',
+ description: 'Open and focus the open-chats selector in the app chat drawer.',
+ group: 'Chat',
+ order: 15,
+ // Mod+E is free of #350's chords (Mod+T/W/1..9, Ctrl+Tab, Mod+K, Mod+F,
+ // Mod+Shift+F, Mod+Shift+L, Mod+,).
+ defaultBinding: { key: 'e', modifier: 'mod' },
+ sources: ['host', 'sandbox'],
+ eligibility: 'app-mounted',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
{
id: 'chat.closeTab',
label: 'Close current tab',
diff --git a/desktop-app/src/preload.ts b/desktop-app/src/preload.ts
index c96eef30d..de4312213 100644
--- a/desktop-app/src/preload.ts
+++ b/desktop-app/src/preload.ts
@@ -12,6 +12,7 @@ import type {
// desktopCommands.test.ts, matching the existing sandbox embed-preload pattern.
const DESKTOP_COMMAND_IDS = new Set<DesktopCommandId>([
'chat.newTab',
+ 'chat.switcher',
'chat.closeTab',
'tabs.select1',
'tabs.select2',
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index 9058e8bc4..d6c48bc4e 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -13,6 +13,7 @@ import { AppHeader } from '@components/AppHeader'
import { BootSplash } from '@components/BootSplash'
import { ChatDrawer } from '@components/ChatDrawer'
import { ChatLocalSearch } from '@components/ChatLocalSearch'
+import { ChatSwitcher } from '@components/ChatSwitcher'
import { ChatViewWorkspace } from '@components/ChatViewWorkspace'
import { CommandPalette } from '@components/CommandPalette'
import { Button, ToastStack } from '@components/Common'
@@ -258,6 +259,7 @@ export function App() {
const [notificationDrawerReady, setNotificationDrawerReady] = React.useState(false)
const [chatDrawerOpen, setChatDrawerOpen] = React.useState(false)
const [chatDrawerReady, setChatDrawerReady] = React.useState(false)
+ const [chatSwitcherFocusRequestId, setChatSwitcherFocusRequestId] = React.useState(0)
const [availableSandboxUiApps, setAvailableSandboxUiApps] = React.useState<ActiveSandboxUiApp[]>(
[]
)
@@ -276,6 +278,10 @@ export function App() {
// callbacks) can tell whether a reveal should target the in-app drawer or the
// full-screen chat route without re-binding on every render.
const chatDrawerVisibleRef = React.useRef(false)
+ // Set when `chat.switcher` fires with the drawer still closed: the switcher is
+ // not mounted yet, so we open the drawer and defer the focus bump until its
+ // column commits.
+ const pendingChatSwitcherFocusRef = React.useRef(false)
const [composerFocusRequestId, setComposerFocusRequestId] = React.useState(0)
const [globalSearchFocusRequestId, setGlobalSearchFocusRequestId] = React.useState(0)
const [notificationOpenRequestId, setNotificationOpenRequestId] = React.useState(0)
@@ -704,6 +710,15 @@ export function App() {
setChatDrawerReady(false)
}, [activeSandboxUiApp?.appRef, chatDrawerVisible])
+ // Once the drawer becomes visible after a `chat.switcher` on a closed drawer,
+ // the switcher is mounted — bump its focus request so it opens the dropdown.
+ React.useEffect(() => {
+ if (chatDrawerVisible && pendingChatSwitcherFocusRef.current) {
+ pendingChatSwitcherFocusRef.current = false
+ setChatSwitcherFocusRequestId(value => value + 1)
+ }
+ }, [chatDrawerVisible])
+
const handleSandboxUiBoundsApplied = React.useCallback(() => {
if (appNotificationDrawerOpen) setNotificationDrawerReady(true)
if (chatDrawerVisible) setChatDrawerReady(true)
@@ -1156,6 +1171,7 @@ export function App() {
setCommandPaletteReturnToSandbox(false)
setSettingsShortcutsRequestId(0)
setChatDrawerOpen(false)
+ setChatSwitcherFocusRequestId(0)
}, [vm.authenticatedPrincipalIdentity])
const desktopCommandContext = React.useMemo(
@@ -1309,6 +1325,18 @@ export function App() {
handleNewChatViewTab()
return
}
+ if (commandId === 'chat.switcher') {
+ closeChatLocalSearch(false)
+ if (chatDrawerVisibleRef.current) {
+ setChatSwitcherFocusRequestId(value => value + 1)
+ } else {
+ // Open the drawer first; the deferred-focus effect opens the switcher
+ // once the switcher's column has mounted.
+ pendingChatSwitcherFocusRef.current = true
+ openChatDrawer()
+ }
+ return
+ }
if (commandId === 'chat.closeTab') {
closeChatLocalSearch(false)
handleCloseChatViewTab(state.activeTabId)
@@ -1365,6 +1393,7 @@ export function App() {
handleCloseChatViewTab,
handleNewChatViewTab,
handleSidebarNavSelect,
+ openChatDrawer,
revealChatViewTab,
sandboxUiMounted,
vm.activeChatId,
@@ -1958,9 +1987,13 @@ export function App() {
{chatDrawerVisible && (
<ChatDrawer
header={
- <span className="chat-drawer__title">
- {activeChatViewTab(chatViewTabs).title}
- </span>
+ <ChatSwitcher
+ tabs={chatViewTabs.tabs}
+ activeTabId={chatViewTabs.activeTabId}
+ onSelect={handleSelectChatViewTab}
+ onNewChat={handleNewChatViewTab}
+ focusRequestId={chatSwitcherFocusRequestId}
+ />
}
onNewChat={handleNewChatViewTab}
onClose={closeChatDrawer}
diff --git a/desktop-app/ui/src/components/ChatSwitcher/ChatSwitcher.test.tsx b/desktop-app/ui/src/components/ChatSwitcher/ChatSwitcher.test.tsx
new file mode 100644
index 000000000..3d8b16b66
--- /dev/null
+++ b/desktop-app/ui/src/components/ChatSwitcher/ChatSwitcher.test.tsx
@@ -0,0 +1,107 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { makeTaskKey } from '@contexts/AgentTaskTrackerContext/types'
+import { ChatListProvider } from '@contexts/ChatListContext'
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { createSessionFsmStore, projectSessionState } from '@hooks/domain/sessionFsm'
+import { ChatSwitcher } from '.'
+
+afterEach(cleanup)
+
+const tabs = [
+ { id: 'one', agentRef: 'alpha', chatId: 'chat-1', title: 'First chat' },
+ { id: 'two', agentRef: 'alpha', chatId: 'chat-2', title: 'Second chat' },
+ { id: 'blank', agentRef: 'alpha', chatId: null, title: 'New chat' },
+]
+
+describe('ChatSwitcher', () => {
+ it('shows the active chat in the trigger and lists open chats on demand', () => {
+ const onSelect = vi.fn()
+ render(
+ <ChatSwitcher tabs={tabs} activeTabId="one" onSelect={onSelect} onNewChat={vi.fn()} />
+ )
+
+ const trigger = screen.getByRole('button', { name: 'Open chats' })
+ expect(trigger.textContent).toContain('First chat')
+ // Collapsed: options are not in the tree.
+ expect(screen.queryByRole('listbox')).toBeNull()
+
+ fireEvent.click(trigger)
+ const listbox = screen.getByRole('listbox', { name: 'Open chats' })
+ expect(listbox).toBeTruthy()
+ const options = screen.getAllByRole('option')
+ expect(options.map(option => option.textContent)).toEqual([
+ 'First chat',
+ 'Second chat',
+ 'New chat',
+ ])
+ expect(options[0]?.getAttribute('aria-selected')).toBe('true')
+
+ fireEvent.click(screen.getByRole('option', { name: 'Second chat' }))
+ expect(onSelect).toHaveBeenCalledWith('two')
+ })
+
+ it('exposes a new-chat action that closes the menu', () => {
+ const onNewChat = vi.fn()
+ render(
+ <ChatSwitcher tabs={tabs} activeTabId="one" onSelect={vi.fn()} onNewChat={onNewChat} />
+ )
+ fireEvent.click(screen.getByRole('button', { name: 'Open chats' }))
+ fireEvent.click(screen.getByRole('button', { name: '+ New chat' }))
+ expect(onNewChat).toHaveBeenCalledTimes(1)
+ expect(screen.queryByRole('listbox')).toBeNull()
+ })
+
+ it('opens the menu when the focus request id increments (the chat.switcher shortcut)', () => {
+ const { rerender } = render(
+ <ChatSwitcher
+ tabs={tabs}
+ activeTabId="one"
+ onSelect={vi.fn()}
+ onNewChat={vi.fn()}
+ focusRequestId={0}
+ />
+ )
+ expect(screen.queryByRole('listbox')).toBeNull()
+
+ rerender(
+ <ChatSwitcher
+ tabs={tabs}
+ activeTabId="one"
+ onSelect={vi.fn()}
+ onNewChat={vi.fn()}
+ focusRequestId={1}
+ />
+ )
+ expect(screen.getByRole('listbox', { name: 'Open chats' })).toBeTruthy()
+ })
+
+ it('reuses per-chat session badges from sessionStateByChatKey', () => {
+ const runningKey = makeTaskKey('alpha', 'chat-1')
+ const fsm = createSessionFsmStore()
+ fsm.dispatch(runningKey, { type: 'SEND_STARTED', taskId: 'task-running' })
+ fsm.dispatch(runningKey, { type: 'TASK_CREATED', taskId: 'task-running' })
+ const sessionStateByChatKey = Object.fromEntries(
+ Object.entries(fsm.getSnapshot()).map(([key, state]) => [key, projectSessionState(state)])
+ )
+
+ render(
+ <ChatListProvider
+ value={{
+ activeChatId: 'chat-1',
+ chatList: [],
+ chatListLoading: false,
+ latestChatSessions: [],
+ latestChatSessionsLoading: false,
+ sessionStateByChatId: {},
+ sessionStateByChatKey,
+ }}
+ >
+ <ChatSwitcher tabs={tabs} activeTabId="one" onSelect={vi.fn()} onNewChat={vi.fn()} />
+ </ChatListProvider>
+ )
+
+ // The badge shows in the collapsed trigger for the active chat.
+ expect(screen.getByLabelText('Running')).toBeTruthy()
+ })
+})
diff --git a/desktop-app/ui/src/components/ChatSwitcher/index.tsx b/desktop-app/ui/src/components/ChatSwitcher/index.tsx
new file mode 100644
index 000000000..5f9fe73f9
--- /dev/null
+++ b/desktop-app/ui/src/components/ChatSwitcher/index.tsx
@@ -0,0 +1,157 @@
+import { useCallback, useContext, useEffect, useRef, useState } from 'react'
+import type { KeyboardEvent } from 'react'
+import { makeTaskKey } from '@contexts/AgentTaskTrackerContext/types'
+import { ChatListContext } from '@contexts/ChatListContext'
+import { Button } from '@components/Common'
+import { ChatStateBadge } from '@components/agents/ChatStateBadge'
+import { useClickOutside } from '@hooks/useClickOutside'
+import { activeChatViewTab } from '@lib/chatViewTabs'
+import type { ChatSwitcherProps } from './types'
+
+/**
+ * Open-chats selector for the chat drawer header. It is a thin, read-only view
+ * over the shared global `chatViewTabs`: `tabs` is the list, `activeTabId` the
+ * selection, and `onSelect`/`onNewChat` mutate that same state — no second tab
+ * store. Per-chat status badges reuse `sessionStateByChatKey` exactly as the
+ * full-screen `ChatTabs` strip does.
+ */
+export function ChatSwitcher({
+ tabs,
+ activeTabId,
+ onSelect,
+ onNewChat,
+ focusRequestId = 0,
+}: ChatSwitcherProps) {
+ const chatList = useContext(ChatListContext)
+ const [open, setOpen] = useState(false)
+ const rootRef = useRef<HTMLDivElement | null>(null)
+ const triggerRef = useRef<HTMLButtonElement | null>(null)
+ const optionRefs = useRef<Array<HTMLButtonElement | null>>([])
+ const lastFocusRequestIdRef = useRef(focusRequestId)
+
+ const active = activeChatViewTab({ tabs, activeTabId })
+ const activeIndex = Math.max(
+ 0,
+ tabs.findIndex(tab => tab.id === active.id)
+ )
+ const close = useCallback(() => setOpen(false), [])
+
+ useClickOutside(rootRef, open, close)
+
+ const badgeFor = (agentRef: string | null, chatId: string | null) =>
+ agentRef && chatId ? chatList?.sessionStateByChatKey[makeTaskKey(agentRef, chatId)] : undefined
+
+ useEffect(() => {
+ if (!open) return
+ optionRefs.current[activeIndex]?.focus()
+ }, [open, activeIndex])
+
+ // The `chat.switcher` shortcut bumps `focusRequestId`; open and focus the list.
+ useEffect(() => {
+ if (focusRequestId <= 0 || focusRequestId === lastFocusRequestIdRef.current) return
+ lastFocusRequestIdRef.current = focusRequestId
+ setOpen(true)
+ }, [focusRequestId])
+
+ const choose = (id: string) => {
+ close()
+ triggerRef.current?.focus()
+ onSelect(id)
+ }
+
+ const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
+ if (event.key === 'Escape' && open) {
+ event.preventDefault()
+ close()
+ triggerRef.current?.focus()
+ return
+ }
+ if (!open && ['ArrowDown', 'ArrowUp'].includes(event.key)) {
+ event.preventDefault()
+ setOpen(true)
+ return
+ }
+ if (!open || !['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return
+ event.preventDefault()
+ const current = optionRefs.current.findIndex(option => option === document.activeElement)
+ let next = current
+ if (event.key === 'Home') next = 0
+ if (event.key === 'End') next = tabs.length - 1
+ if (event.key === 'ArrowDown') next = Math.min(tabs.length - 1, current + 1)
+ if (event.key === 'ArrowUp') next = Math.max(0, current - 1)
+ optionRefs.current[next]?.focus()
+ }
+
+ return (
+ <div className="chat-switcher" onKeyDown={handleKeyDown} ref={rootRef}>
+ <Button
+ align="between"
+ aria-expanded={open}
+ aria-haspopup="listbox"
+ aria-label="Open chats"
+ block
+ className="chat-switcher__trigger"
+ color="neutral"
+ onClick={() => setOpen(current => !current)}
+ ref={triggerRef}
+ size="sm"
+ variant="soft"
+ >
+ <span className="chat-switcher__trigger-main">
+ <ChatStateBadge sessionState={badgeFor(active.agentRef, active.chatId)} unreadTerminal={false} />
+ <span className="chat-switcher__label">{active.title}</span>
+ </span>
+ <span aria-hidden="true" className="chat-switcher__chevron">
+ ▾
+ </span>
+ </Button>
+ {open ? (
+ <div className="chat-switcher__menu" role="listbox" aria-label="Open chats">
+ <div className="chat-switcher__options">
+ {tabs.map((tab, index) => {
+ const isActive = tab.id === activeTabId
+ return (
+ <Button
+ key={tab.id}
+ align="start"
+ aria-selected={isActive}
+ className={`chat-switcher__option${isActive ? ' is-active' : ''}`}
+ color="neutral"
+ onClick={() => choose(tab.id)}
+ ref={element => {
+ optionRefs.current[index] = element
+ }}
+ role="option"
+ size="sm"
+ variant="ghost"
+ >
+ <ChatStateBadge
+ sessionState={badgeFor(tab.agentRef, tab.chatId)}
+ unreadTerminal={false}
+ />
+ <span className="chat-switcher__label">{tab.title}</span>
+ </Button>
+ )
+ })}
+ </div>
+ <div className="chat-switcher__footer">
+ <Button
+ align="start"
+ block
+ className="chat-switcher__new"
+ color="neutral"
+ onClick={() => {
+ close()
+ onNewChat()
+ }}
+ size="sm"
+ variant="ghost"
+ >
+ + New chat
+ </Button>
+ </div>
+ </div>
+ ) : null}
+ </div>
+ )
+}
diff --git a/desktop-app/ui/src/components/ChatSwitcher/types.ts b/desktop-app/ui/src/components/ChatSwitcher/types.ts
new file mode 100644
index 000000000..8d913586b
--- /dev/null
+++ b/desktop-app/ui/src/components/ChatSwitcher/types.ts
@@ -0,0 +1,13 @@
+import type { ChatViewTab } from '@lib/chatViewTabs.types'
+
+export type ChatSwitcherProps = {
+ tabs: ChatViewTab[]
+ activeTabId: string
+ onSelect: (id: string) => void
+ onNewChat: () => void
+ /**
+ * Incremented by the `chat.switcher` shortcut to open and focus the dropdown.
+ * A thin view over the shared `chatViewTabs` — it owns no tab state of its own.
+ */
+ focusRequestId?: number
+}
diff --git a/desktop-app/ui/src/styles.css b/desktop-app/ui/src/styles.css
index 72da5c7da..d184762d5 100644
--- a/desktop-app/ui/src/styles.css
+++ b/desktop-app/ui/src/styles.css
@@ -12611,6 +12611,77 @@ pre {
flex-direction: column;
}
+/* Open-chats selector in the drawer header — a thin dropdown over chatViewTabs.
+ The narrow drawer column can't host a horizontal tab strip, so the switcher
+ lists chats vertically with their status badge. */
+.chat-switcher {
+ position: relative;
+ flex: 1 1 auto;
+ min-width: 0;
+}
+
+.chat-switcher__trigger {
+ min-width: 0;
+}
+
+.chat-switcher__trigger-main {
+ display: inline-flex;
+ align-items: center;
+ gap: var(--space-1-5);
+ min-width: 0;
+ overflow: hidden;
+}
+
+.chat-switcher__chevron {
+ flex: 0 0 auto;
+ margin-left: var(--space-1);
+ color: var(--text-dim);
+}
+
+.chat-switcher__label {
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.chat-switcher__menu {
+ position: absolute;
+ top: calc(100% + var(--space-1));
+ left: 0;
+ right: 0;
+ z-index: calc(var(--layer-dropdown) + 10);
+ border: 1px solid rgba(var(--edge-rgb), 0.32);
+ border-radius: var(--radius-md);
+ background: var(--panel);
+ box-shadow: var(--shadow-hard);
+ padding: var(--space-1);
+ display: grid;
+ gap: var(--space-1);
+}
+
+.chat-switcher__options {
+ display: grid;
+ gap: var(--space-0-5);
+ max-height: min(360px, 50vh);
+ overflow-y: auto;
+ scrollbar-width: thin;
+}
+
+.chat-switcher__option {
+ min-width: 0;
+ gap: var(--space-1-5);
+}
+
+.chat-switcher__option.is-active {
+ background: rgba(var(--accent-rgb), 0.14);
+}
+
+.chat-switcher__footer {
+ border-top: 1px solid rgba(var(--edge-rgb), 0.24);
+ padding-top: var(--space-1);
+}
+
@media (min-width: 901px) and (max-width: 1220px) {
.content-panel {
--app-header-utilities-width: calc(