@@ -5,7 +5,6 @@ import { t, useLanguage } from '@/intl/client';
55import { SiteContentPointer } from '@/lib/api' ;
66import { tcls } from '@/lib/tailwind' ;
77
8- import { isQuestion } from './isQuestion' ;
98import { SearchPageResultItem } from './SearchPageResultItem' ;
109import { SearchQuestionResultItem } from './SearchQuestionResultItem' ;
1110import { SearchSectionResultItem } from './SearchSectionResultItem' ;
@@ -92,8 +91,6 @@ export const SearchResults = React.forwardRef(function SearchResults(
9291 }
9392
9493 debounceTimeout . current = setTimeout ( async ( ) => {
95- setCursor ( null ) ;
96-
9794 const fetchedResults = await ( global
9895 ? searchAllSiteContent ( query , pointer )
9996 : searchSiteSpaceContent ( query , pointer , revisionId ) ) ;
@@ -110,6 +107,16 @@ export const SearchResults = React.forwardRef(function SearchResults(
110107 }
111108 } , [ query , global , pointer , spaceId , revisionId , withAsk ] ) ;
112109
110+ React . useEffect ( ( ) => {
111+ if ( ! query ) {
112+ // Reset the cursor when there's no query
113+ setCursor ( null ) ;
114+ } else if ( results && results . length > 0 ) {
115+ // Auto-focus the first result
116+ setCursor ( 0 ) ;
117+ }
118+ } , [ results , query ] ) ;
119+
113120 // Scroll to the active result.
114121 React . useEffect ( ( ) => {
115122 if ( cursor === null || ! refs . current [ cursor ] ) {
@@ -256,10 +263,13 @@ export const SearchResults = React.forwardRef(function SearchResults(
256263 ) ;
257264} ) ;
258265
266+ /**
267+ * Add a "Ask <question>" item at the top of the results list.
268+ */
259269function withQuestionResult ( results : null | ResultType [ ] , query : string ) : null | ResultType [ ] {
260270 const without = results ? results . filter ( ( result ) => result . type !== 'question' ) : null ;
261271
262- if ( ! isQuestion ( query ) ) {
272+ if ( query . length === 0 ) {
263273 return without ;
264274 }
265275
0 commit comments