fix(web): reset command palette selection when the result list changes

The effect commented 'Reset selection on items change' had an empty
dependency array, so it only ever ran on mount. Arrow down to row 5 of
the recent-documents list, then type a query that returns two results:
selectedIndex stays 5, no row is highlighted (nothing renders
data-index=5), and Enter silently does nothing because items[5] is
undefined. The selection also never snaps back to the top result when
new results stream in.

Snap the selection to the top whenever the query text changes, and
clamp it into range whenever the item list shrinks (async search
results replacing the recent docs, refetches while the palette is
open).
This commit is contained in:
abhay-codes07 2026-07-13 00:08:57 +05:30
parent 2cebe81512
commit 8db722d76c
No known key found for this signature in database

View file

@ -199,10 +199,16 @@ export function DocumentsCommandPalette({
for (const a of actions) items.push(a)
}
// Reset selection on items change
// biome-ignore lint/correctness/useExhaustiveDependencies: search is the trigger — snap the selection back to the top result whenever the query changes
useEffect(() => {
setSelectedIndex(0)
}, [])
}, [search])
// Keep the selection in range when the list shrinks (async results
// replacing recent docs, refetches, etc.)
useEffect(() => {
setSelectedIndex((i) => Math.min(i, Math.max(items.length - 1, 0)))
}, [items.length])
// Scroll selected into view
useEffect(() => {