11import { useSelectedIndex , useSearchActions } from './search.store'
2- import { useAskAiFromSearch } from './useAskAiFromSearch'
32import { useRef , useCallback , MutableRefObject } from 'react'
43
4+ interface SearchKeyboardNavigationOptions {
5+ resultsCount : number
6+ isLoading : boolean
7+ }
8+
59interface 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