Skip to content

The agent that grows with you

fix(webapp): picker rows resolve their menu on open; focus returns to the picker

Tekniumb435451a7354
diff --git a/apps/desktop/src/app/chat/compact-tab-menu.test.tsx b/apps/desktop/src/app/chat/compact-tab-menu.test.tsx
index e9d2f87f60f..7b9ab948f77 100644
--- a/apps/desktop/src/app/chat/compact-tab-menu.test.tsx
+++ b/apps/desktop/src/app/chat/compact-tab-menu.test.tsx
@@ -7,12 +7,12 @@ import { group } from '@/components/pane-shell/tree/model'
import { TreeGroup } from '@/components/pane-shell/tree/renderer/tree-group'
import { $layoutTree, $narrowViewport } from '@/components/pane-shell/tree/store'
import { registry } from '@/contrib/registry'
-import { $sessions } from '@/store/session'
+import { $selectedStoredSessionId, $sessions } from '@/store/session'
import { $sessionTiles, type SessionTileDelegate, setSessionTileDelegate } from '@/store/session-states'
import { stubMenuDomApis, stubResizeObserver } from '@/test/jsdom'
import type { SessionInfo } from '@/types/hermes'
-import { watchSessionTiles } from './session-tile'
+import { watchSessionTiles, workspaceTabActions } from './session-tile'
// The transport: the REST rename the dialog falls back to with no live runtime.
const renameSession = vi.hoisted(() => vi.fn())
@@ -73,10 +73,11 @@ beforeEach(() => {
area: 'panes',
id: 'workspace',
title: 'Main chat',
- data: { placement: 'main' },
+ // The production main-tab menu factory, gated on the selected session.
+ data: { placement: 'main', tabActions: workspaceTabActions },
render: () => <p>main chat</p>
})
- $sessions.set([row('s2', 'Trip plan'), row('s3', 'Old draft')])
+ $sessions.set([row('s1', 'Main chat'), row('s2', 'Trip plan'), row('s3', 'Old draft')])
setSessionTileDelegate(delegate as unknown as SessionTileDelegate)
renameSession.mockResolvedValue({ title: 'Trip plan v2' })
$layoutTree.set({ ...group(panes), active: 'workspace', tabStrip: 'always' })
@@ -88,6 +89,7 @@ afterEach(() => {
disposeWorkspace()
$layoutTree.set(null)
$narrowViewport.set(false)
+ $selectedStoredSessionId.set(null)
delete globalThis.document.documentElement.dataset.hermesDesktopHost
vi.clearAllMocks()
vi.unstubAllGlobals()
@@ -122,3 +124,26 @@ it('renames a background tab from the phone picker', async () => {
expect(renameSession).toHaveBeenCalledWith('s2', 'Trip plan v2', undefined)
await waitFor(() => expect($sessions.get().find(s => s.id === 's2')?.title).toBe('Trip plan v2'))
})
+
+it("offers the main tab's menu only once it holds a session, and targets that session", async () => {
+ render(<Host />)
+ const picker = () => screen.getByRole('button', { name: /^3 tabs/, hidden: true })
+
+ fireEvent.pointerDown(picker(), { button: 0, ctrlKey: false })
+ expect(screen.queryByRole('menuitem', { name: 'Actions for Main chat' })).toBeNull()
+ fireEvent.pointerDown(picker(), { button: 0, ctrlKey: false })
+
+ act(() => $selectedStoredSessionId.set('s1'))
+ openTabMenu('Main chat')
+ fireEvent.click(await screen.findByRole('menuitem', { name: 'Archive' }))
+ expect(delegate.archiveSession).toHaveBeenCalledWith('s1')
+})
+
+it('hands focus back to the picker when a tab menu closes without a dialog', async () => {
+ render(<Host />)
+
+ openTabMenu('Trip plan')
+ fireEvent.keyDown((await screen.findByRole('menuitem', { name: 'Archive' })).closest('[role=menu]')!, { key: 'Escape' })
+
+ await waitFor(() => expect(globalThis.document.activeElement).toBe(screen.getByRole('button', { name: /^3 tabs/ })))
+})
diff --git a/apps/desktop/src/components/pane-shell/tree/renderer/compact-tab-picker.tsx b/apps/desktop/src/components/pane-shell/tree/renderer/compact-tab-picker.tsx
index 4569e51f6c4..03a1fa4ed93 100644
--- a/apps/desktop/src/components/pane-shell/tree/renderer/compact-tab-picker.tsx
+++ b/apps/desktop/src/components/pane-shell/tree/renderer/compact-tab-picker.tsx
@@ -26,6 +26,36 @@ interface CompactTabPickerProps {
tabActions?: (id: string) => TabActionsMenu | undefined
}
+interface PickerRowProps {
+ /** Called when the row MOUNTS (each time the picker opens): the main tab
+ * gains its menu once it holds a session, and the picker's own props do
+ * not change when that happens. */
+ hasActions: () => boolean
+ onActions: () => void
+ tab: CompactTabPickerProps['tabs'][number]
+}
+
+function PickerRow({ hasActions, onActions, tab }: PickerRowProps) {
+ const { t } = useI18n()
+
+ return (
+ <div className="flex items-stretch">
+ <DropdownMenuRadioItem className="min-h-11 min-w-0 flex-1" value={tab.id}>
+ <span className="min-w-0 whitespace-normal break-words">{tab.label ?? tab.title}</span>
+ </DropdownMenuRadioItem>
+ {hasActions() && (
+ <DropdownMenuItem
+ aria-label={t.zones.tabActions(tab.title)}
+ className="min-h-11 min-w-11 shrink-0 justify-center"
+ onSelect={onActions}
+ >
+ <Codicon name="ellipsis" />
+ </DropdownMenuItem>
+ )}
+ </div>
+ )
+}
+
/** A phone's strip shows its current tab and a count; every open tab stays
* reachable without a hidden horizontal drag gesture, and so does every tab's
* menu (archive, delete, rename…) — a phone has no strip to right-click. */
@@ -39,6 +69,7 @@ export function CompactTabPicker({ activeId, tabs, onSelect, onClose, newTab, ta
// A row's ⋯ hands focus to the tab menu; the closing picker must not pull
// it back to its trigger (that reads as an outside focus and shuts the menu).
const handingOff = useRef(false)
+ const pickerRef = useRef<HTMLButtonElement>(null)
const active = tabs.find(tab => tab.id === activeId)
const count = t.zones.tabCount(tabs.length)
const menuFor = menu ? tabActions?.(menu.id) : undefined
@@ -51,6 +82,7 @@ export function CompactTabPicker({ activeId, tabs, onSelect, onClose, newTab, ta
aria-labelledby={`${countId} ${labelId}`}
className="min-w-0 flex-1 justify-between"
data-slot="compact-tab-picker"
+ ref={pickerRef}
size="touch"
variant="ghost"
>
@@ -72,23 +104,15 @@ export function CompactTabPicker({ activeId, tabs, onSelect, onClose, newTab, ta
>
<DropdownMenuRadioGroup onValueChange={onSelect} value={activeId}>
{tabs.map(tab => (
- <div className="flex items-stretch" key={tab.id}>
- <DropdownMenuRadioItem className="min-h-11 min-w-0 flex-1" value={tab.id}>
- <span className="min-w-0 whitespace-normal break-words">{tab.label ?? tab.title}</span>
- </DropdownMenuRadioItem>
- {tabActions?.(tab.id) && (
- <DropdownMenuItem
- aria-label={t.zones.tabActions(tab.title)}
- className="min-h-11 min-w-11 shrink-0 justify-center"
- onSelect={() => {
- handingOff.current = true
- setMenu({ id: tab.id, open: true })
- }}
- >
- <Codicon name="ellipsis" />
- </DropdownMenuItem>
- )}
- </div>
+ <PickerRow
+ hasActions={() => Boolean(tabActions?.(tab.id))}
+ key={tab.id}
+ onActions={() => {
+ handingOff.current = true
+ setMenu({ id: tab.id, open: true })
+ }}
+ tab={tab}
+ />
))}
</DropdownMenuRadioGroup>
{onClose && (
@@ -104,7 +128,22 @@ export function CompactTabPicker({ activeId, tabs, onSelect, onClose, newTab, ta
{menu && menuFor && (
<Fragment key={menu.id}>
{menuFor({
- onOpenChange: open => setMenu({ id: menu.id, open }),
+ onOpenChange: open => {
+ setMenu({ id: menu.id, open })
+
+ // The menu's own trigger is an inert anchor, so a plain close
+ // (Escape, a non-dialog verb) would drop focus on the page.
+ // Hand it back to the picker unless a dialog took it.
+ if (!open) {
+ requestAnimationFrame(() => {
+ const doc = pickerRef.current?.ownerDocument
+
+ if (!doc?.activeElement || doc.activeElement === doc.body) {
+ pickerRef.current?.focus()
+ }
+ })
+ }
+ },
open: menu.open,
// The menu drops from the picker row; the row's ⋯ that asked for
// it went away with the picker.