Skip to content

The agent that grows with you

fix(desktop): read the palette highlight from the cmdk store

ethernet1e3106197b70
diff --git a/apps/desktop/src/app/command-palette/highlight-watcher.test.tsx b/apps/desktop/src/app/command-palette/highlight-watcher.test.tsx
new file mode 100644
index 00000000000..d2929fcc1e1
--- /dev/null
+++ b/apps/desktop/src/app/command-palette/highlight-watcher.test.tsx
@@ -0,0 +1,57 @@
+/**
+ * The regression that motivates this file: cmdk calls the root
+ * `onValueChange` only in controlled mode (a set `value` prop). The palette
+ * is uncontrolled, so a highlight preview wired to that prop never fires.
+ * The watcher must read the cmdk store instead, which works in both modes.
+ */
+
+import { render } from '@testing-library/react'
+import { Command } from 'cmdk'
+import { describe, expect, it, vi } from 'vitest'
+
+import { HighlightWatcher } from './highlight-watcher'
+
+// cmdk observes group headings with a ResizeObserver, which jsdom lacks.
+class TestResizeObserver {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+}
+
+vi.stubGlobal('ResizeObserver', TestResizeObserver)
+
+Element.prototype.scrollIntoView = function scrollIntoView() {}
+
+const palette = (onValue: (value: string) => void, onRootValueChange?: (value: string) => void) => (
+ <Command onValueChange={onRootValueChange}>
+ <HighlightWatcher onValue={onValue} />
+ <Command.List>
+ <Command.Item value="alpha">Alpha</Command.Item>
+ <Command.Item value="beta">Beta</Command.Item>
+ </Command.List>
+ </Command>
+)
+
+describe('HighlightWatcher', () => {
+ it('reports the highlight from the cmdk store in uncontrolled mode', () => {
+ const onValue = vi.fn()
+
+ render(palette(onValue))
+
+ // cmdk auto-highlights the first item on mount.
+ expect(onValue).toHaveBeenCalledWith('alpha')
+ })
+
+ it('covers the gap: the root onValueChange stays silent in uncontrolled mode', () => {
+ const onValue = vi.fn()
+ const onRootValueChange = vi.fn()
+
+ render(palette(onValue, onRootValueChange))
+
+ // The store-based watcher fires. The prop the first implementation used
+ // does not. If cmdk starts to fire it in uncontrolled mode, the watcher
+ // becomes removable — this assertion is the signal.
+ expect(onValue).toHaveBeenCalledWith('alpha')
+ expect(onRootValueChange).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/desktop/src/app/command-palette/highlight-watcher.tsx b/apps/desktop/src/app/command-palette/highlight-watcher.tsx
new file mode 100644
index 00000000000..5f7874b98f9
--- /dev/null
+++ b/apps/desktop/src/app/command-palette/highlight-watcher.tsx
@@ -0,0 +1,31 @@
+/**
+ * Reports the cmdk highlight to the palette.
+ *
+ * cmdk calls the root `onValueChange` only in controlled mode (when the
+ * `value` prop is set). This palette is uncontrolled, so that prop never
+ * fires. This watcher subscribes to the cmdk store with `useCommandState`,
+ * which works in both modes, and reports each highlight change.
+ */
+
+import { useCommandState } from 'cmdk'
+import { useEffect, useRef } from 'react'
+
+interface HighlightWatcherProps {
+ /** Receives the cmdk value of each newly highlighted row. */
+ onValue: (value: string) => void
+}
+
+export function HighlightWatcher({ onValue }: HighlightWatcherProps): null {
+ const value = useCommandState(state => state.value)
+
+ // Pin the callback so the effect fires on highlight changes only. The
+ // parent passes a fresh closure on every render.
+ const onValueRef = useRef(onValue)
+ onValueRef.current = onValue
+
+ useEffect(() => {
+ onValueRef.current(value)
+ }, [value])
+
+ return null
+}
diff --git a/apps/desktop/src/app/command-palette/index.tsx b/apps/desktop/src/app/command-palette/index.tsx
index dc0a1b6d5e6..55b2f85fda7 100644
--- a/apps/desktop/src/app/command-palette/index.tsx
+++ b/apps/desktop/src/app/command-palette/index.tsx
@@ -105,6 +105,7 @@ import { type SettingsSearchEntry, settingsSearchTargetQuery } from '../settings
import { useSettingsSearchCatalog } from '../settings/use-settings-search'
import { usePaletteContributions } from './contrib'
+import { HighlightWatcher } from './highlight-watcher'
import { MarketplaceThemePage } from './marketplace-theme-page'
import { PetInlineToggle, PetPalettePage } from './pet-palette-page'
@@ -1394,9 +1395,10 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) {
const visibleGroups = useMemo(() => rankGroups(unrankedGroups, search), [unrankedGroups, search])
const placeholder = activePage ? activePage.placeholder : t.commandCenter.searchPlaceholder
- // cmdk reports the highlighted row (arrows or hover) via the root's
- // onValueChange. Resolve it back to its PaletteItem so preview-capable rows
- // (the theme pickers) can paint live. Any other highlight clears the preview.
+ // The HighlightWatcher inside <Command> reports the highlighted row (arrows
+ // or hover) from the cmdk store. Resolve it back to its PaletteItem so
+ // preview-capable rows (the theme pickers) can paint live. Any other
+ // highlight clears the preview.
const itemByValue = useMemo(() => {
const map = new Map<string, PaletteItem>()
@@ -1480,7 +1482,8 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) {
}}
>
<DialogPrimitive.Title className="sr-only">{t.commandCenter.paletteTitle}</DialogPrimitive.Title>
- <Command className="bg-transparent" loop onValueChange={handleHighlight} shouldFilter={false}>
+ <Command className="bg-transparent" loop shouldFilter={false}>
+ <HighlightWatcher onValue={handleHighlight} />
{activePage && (
<button
className="flex w-full items-center gap-1.5 border-b border-border px-3 py-1.5 text-left text-xs text-muted-foreground transition-colors hover:text-foreground"