Skip to content

Commit 42ec442

Browse files
committed
Remove the behaviour where AskAI is triggered if there are no results
1 parent 9e1cb8f commit 42ec442

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/Search/Search.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export const Search = () => {
4747
}
4848

4949
const { inputRef, buttonRef, itemRefs, filterRefs, handleInputKeyDown } =
50-
useSearchKeyboardNavigation(resultsCount)
50+
useSearchKeyboardNavigation({
51+
resultsCount,
52+
isLoading: isLoading || isFetching,
53+
})
5154

5255
// Listen for Cmd+K to focus input
5356
useEffect(() => {

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/Search/useSearchKeyboardNavigation.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { useSelectedIndex, useSearchActions } from './search.store'
2-
import { useAskAiFromSearch } from './useAskAiFromSearch'
32
import { useRef, useCallback, MutableRefObject } from 'react'
43

4+
interface SearchKeyboardNavigationOptions {
5+
resultsCount: number
6+
isLoading: boolean
7+
}
8+
59
interface SearchKeyboardNavigationReturn {
610
inputRef: React.RefObject<HTMLInputElement>
711
buttonRef: React.RefObject<HTMLButtonElement>
@@ -10,30 +14,31 @@ interface SearchKeyboardNavigationReturn {
1014
handleInputKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void
1115
}
1216

13-
export const useSearchKeyboardNavigation = (
14-
resultsCount: number
15-
): SearchKeyboardNavigationReturn => {
17+
export const useSearchKeyboardNavigation = ({
18+
resultsCount,
19+
isLoading,
20+
}: SearchKeyboardNavigationOptions): SearchKeyboardNavigationReturn => {
1621
const inputRef = useRef<HTMLInputElement>(null)
1722
const buttonRef = useRef<HTMLButtonElement>(null)
1823
const itemRefs = useRef<(HTMLAnchorElement | null)[]>([])
1924
const filterRefs = useRef<(HTMLButtonElement | null)[]>([])
20-
const { askAi } = useAskAiFromSearch()
2125
const selectedIndex = useSelectedIndex()
2226
const { setSelectedIndex } = useSearchActions()
2327

2428
const handleInputKeyDown = useCallback(
2529
(e: React.KeyboardEvent<HTMLInputElement>) => {
2630
if (e.key === 'Enter') {
2731
e.preventDefault()
28-
if (resultsCount > 0 && selectedIndex >= 0) {
32+
if (isLoading || resultsCount === 0) {
33+
// Don't do anything while search is loading or if there are no results
34+
return
35+
}
36+
if (selectedIndex >= 0) {
2937
// Navigate to selected result
3038
itemRefs.current[selectedIndex]?.click()
31-
} else if (resultsCount > 0) {
39+
} else {
3240
// No selection, click first result
3341
itemRefs.current[0]?.click()
34-
} else {
35-
// No results, ask AI
36-
askAi()
3742
}
3843
} else if (e.key === 'ArrowDown') {
3944
e.preventDefault()
@@ -71,7 +76,7 @@ export const useSearchKeyboardNavigation = (
7176
}
7277
// Tab works naturally - goes to filters, then button
7378
},
74-
[resultsCount, selectedIndex, setSelectedIndex, askAi]
79+
[resultsCount, isLoading, selectedIndex, setSelectedIndex]
7580
)
7681

7782
return {

0 commit comments

Comments
 (0)