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>
This commit is contained in:
雨霖铃
2026-05-27 13:54:38 +08:00
parent 67629263f9
commit 27d3360f9d
+9 -7
View File
@@ -126,17 +126,19 @@ export default function TopBar({ onOpenOptions }: { onOpenOptions: () => void })
setSelectedIndex((prev) => (prev > 0 ? prev - 1 : prev)); setSelectedIndex((prev) => (prev > 0 ? prev - 1 : prev));
} else if (e.key === 'Enter') { } else if (e.key === 'Enter') {
e.preventDefault(); e.preventDefault();
if (selectedIndex >= 0) { if (selectedIndex >= 0 && selectedIndex < totalItems) {
if (searchQuery.trim()) { if (searchQuery.trim()) {
handleSelectFeature(searchResults[selectedIndex]); handleSelectFeature(searchResults[selectedIndex]);
} else { } else {
const selectedQuery = displayedHistory[selectedIndex]; const selectedQuery = displayedHistory[selectedIndex];
setSearchQuery(selectedQuery); if (selectedQuery) {
setSelectedIndex(-1); setSearchQuery(selectedQuery);
const matched = FEATURES.find( setSelectedIndex(-1);
(f) => f.key !== 'dashboard' && t(f.labelKey) === selectedQuery, const matched = FEATURES.find(
); (f) => f.key !== 'dashboard' && t(f.labelKey) === selectedQuery,
if (matched) handleSelectFeature(matched); );
if (matched) handleSelectFeature(matched);
}
} }
} else if (searchQuery.trim() && searchResults.length > 0) { } else if (searchQuery.trim() && searchResults.length > 0) {
handleSelectFeature(searchResults[0]); handleSelectFeature(searchResults[0]);