@@ -43,7 +43,8 @@ export function useSearchResults(props: {
4343 const  [ resultsState ,  setResultsState ]  =  React . useState < { 
4444 results : ResultType [ ] ; 
4545 fetching : boolean ; 
46-  } > ( {  results : [ ] ,  fetching : false  } ) ; 
46+  error : boolean ; 
47+  } > ( {  results : [ ] ,  fetching : false ,  error : false  } ) ; 
4748
4849 const  {  assistants }  =  useAI ( ) ; 
4950 const  withAI  =  assistants . length  >  0 ; 
@@ -54,7 +55,7 @@ export function useSearchResults(props: {
5455 } 
5556 if  ( ! query )  { 
5657 if  ( ! withAI )  { 
57-  setResultsState ( {  results : [ ] ,  fetching : false  } ) ; 
58+  setResultsState ( {  results : [ ] ,  fetching : false , error :  false  } ) ; 
5859 return ; 
5960 } 
6061
@@ -64,11 +65,11 @@ export function useSearchResults(props: {
6465 results , 
6566 `Cached recommended questions should be set for site-space ${ siteSpaceId }  
6667 ) ; 
67-  setResultsState ( {  results,  fetching : false  } ) ; 
68+  setResultsState ( {  results,  fetching : false , error :  false  } ) ; 
6869 return ; 
6970 } 
7071
71-  setResultsState ( {  results : [ ] ,  fetching : false  } ) ; 
72+  setResultsState ( {  results : [ ] ,  fetching : false , error :  false  } ) ; 
7273
7374 let  cancelled  =  false ; 
7475
@@ -102,7 +103,11 @@ export function useSearchResults(props: {
102103 cachedRecommendedQuestions . set ( siteSpaceId ,  recommendedQuestions ) ; 
103104
104105 if  ( ! cancelled )  { 
105-  setResultsState ( {  results : [ ...recommendedQuestions ] ,  fetching : false  } ) ; 
106+  setResultsState ( { 
107+  results : [ ...recommendedQuestions ] , 
108+  fetching : false , 
109+  error : false , 
110+  } ) ; 
106111 } 
107112 } 
108113 } ,  100 ) ; 
@@ -112,44 +117,55 @@ export function useSearchResults(props: {
112117 clearTimeout ( timeout ) ; 
113118 } ; 
114119 } 
115-  setResultsState ( ( prev )  =>  ( {  results : prev . results ,  fetching : true  } ) ) ; 
120+  setResultsState ( ( prev )  =>  ( {  results : prev . results ,  fetching : true , error :  false  } ) ) ; 
116121 let  cancelled  =  false ; 
117122 const  timeout  =  setTimeout ( async  ( )  =>  { 
118-  const  results  =  await  ( ( )  =>  { 
119-  if  ( scope  ===  'all' )  { 
120-  // Search all content on the site 
121-  return  searchAllSiteContent ( query ) ; 
122-  } 
123-  if  ( scope  ===  'default' )  { 
124-  // Search the current section's variant + matched/default variant for other sections 
125-  return  searchCurrentSiteSpaceContent ( query ,  siteSpaceId ) ; 
126-  } 
127-  if  ( scope  ===  'extended' )  { 
128-  // Search all variants of the current section 
129-  return  searchSpecificSiteSpaceContent ( query ,  siteSpaceIds ) ; 
123+  try  { 
124+  const  results  =  await  ( ( )  =>  { 
125+  if  ( scope  ===  'all' )  { 
126+  // Search all content on the site 
127+  return  searchAllSiteContent ( query ) ; 
128+  } 
129+  if  ( scope  ===  'default' )  { 
130+  // Search the current section's variant + matched/default variant for other sections 
131+  return  searchCurrentSiteSpaceContent ( query ,  siteSpaceId ) ; 
132+  } 
133+  if  ( scope  ===  'extended' )  { 
134+  // Search all variants of the current section 
135+  return  searchSpecificSiteSpaceContent ( query ,  siteSpaceIds ) ; 
136+  } 
137+  if  ( scope  ===  'current' )  { 
138+  // Search only the current section's current variant 
139+  return  searchSpecificSiteSpaceContent ( query ,  [ siteSpaceId ] ) ; 
140+  } 
141+  throw  new  Error ( `Unhandled search scope: ${ scope }  ) ; 
142+  } ) ( ) ; 
143+ 144+  if  ( cancelled )  { 
145+  return ; 
130146 } 
131-  if  ( scope  ===  'current' )  { 
132-  // Search only the current section's current variant 
133-  return  searchSpecificSiteSpaceContent ( query ,  [ siteSpaceId ] ) ; 
147+ 148+  if  ( ! results )  { 
149+  // One time when this one returns undefined is when it cannot find the server action and returns the html from the page. 
150+  // In that case, we want to avoid being stuck in a loading state, but it is an error. 
151+  // We could potentially try to force reload the page here, but i'm not 100% sure it would be a better experience. 
152+  setResultsState ( {  results : [ ] ,  fetching : false ,  error : true  } ) ; 
153+  return ; 
134154 } 
135-  throw  new  Error ( `Unhandled search scope: ${ scope }  ) ; 
136-  } ) ( ) ; 
137155
138-  if  ( cancelled )  { 
139-  return ; 
140-  } 
156+  setResultsState ( {  results,  fetching : false ,  error : false  } ) ; 
141157
142-  if  ( ! results )  { 
143-  setResultsState ( {  results : [ ] ,  fetching : false  } ) ; 
144-  return ; 
158+  trackEvent ( { 
159+  type : 'search_type_query' , 
160+  query, 
161+  } ) ; 
162+  }  catch  { 
163+  // If there is an error, we need to catch it to avoid infinite loading state. 
164+  if  ( cancelled )  { 
165+  return ; 
166+  } 
167+  setResultsState ( {  results : [ ] ,  fetching : false ,  error : true  } ) ; 
145168 } 
146- 147-  setResultsState ( {  results,  fetching : false  } ) ; 
148- 149-  trackEvent ( { 
150-  type : 'search_type_query' , 
151-  query, 
152-  } ) ; 
153169 } ,  350 ) ; 
154170
155171 return  ( )  =>  { 
0 commit comments