From 8db722d76c5888b514dfd87a52dc9531be046949 Mon Sep 17 00:00:00 2001 From: abhay-codes07 Date: Mon, 13 Jul 2026 00:08:57 +0530 Subject: [PATCH] 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). --- apps/web/components/documents-command-palette.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/web/components/documents-command-palette.tsx b/apps/web/components/documents-command-palette.tsx index 6e711b291..229820913 100644 --- a/apps/web/components/documents-command-palette.tsx +++ b/apps/web/components/documents-command-palette.tsx @@ -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(() => {