From 27d3360f9d523c275ca0eda55b7a27bcb672ba45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=A8=E9=9C=96=E9=93=83?= Date: Wed, 27 May 2026 13:54:38 +0800 Subject: [PATCH] fix(TopBar): add selectedIndex boundary protection in keyboard nav Guard against out-of-bounds access when search results change during keyboard navigation. Check selectedIndex < totalItems before accessing arrays. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- components/TopBar.tsx | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/components/TopBar.tsx b/components/TopBar.tsx index b69c22f..bad30ac 100644 --- a/components/TopBar.tsx +++ b/components/TopBar.tsx @@ -126,17 +126,19 @@ export default function TopBar({ onOpenOptions }: { onOpenOptions: () => void }) setSelectedIndex((prev) => (prev > 0 ? prev - 1 : prev)); } else if (e.key === 'Enter') { e.preventDefault(); - if (selectedIndex >= 0) { + if (selectedIndex >= 0 && selectedIndex < totalItems) { if (searchQuery.trim()) { handleSelectFeature(searchResults[selectedIndex]); } else { const selectedQuery = displayedHistory[selectedIndex]; - setSearchQuery(selectedQuery); - setSelectedIndex(-1); - const matched = FEATURES.find( - (f) => f.key !== 'dashboard' && t(f.labelKey) === selectedQuery, - ); - if (matched) handleSelectFeature(matched); + if (selectedQuery) { + setSearchQuery(selectedQuery); + setSelectedIndex(-1); + const matched = FEATURES.find( + (f) => f.key !== 'dashboard' && t(f.labelKey) === selectedQuery, + ); + if (matched) handleSelectFeature(matched); + } } } else if (searchQuery.trim() && searchResults.length > 0) { handleSelectFeature(searchResults[0]);