@@ -4,6 +4,14 @@ import type { AlgoliaMultiSearchChecks } from './useAlgoliaSearch'
44import { type SearchSuggestion , emptySearchResponse , parseSuggestionIntent } from './search-utils'
55import { isValidNewPackageName , checkPackageExists } from '~/utils/package-name'
66
7+ function emptySearchPayload ( ) {
8+ return {
9+ searchResponse : emptySearchResponse ( ) ,
10+ suggestions : [ ] as SearchSuggestion [ ] ,
11+ packageAvailability : null as { name : string ; available : boolean } | null ,
12+ }
13+ }
14+
715export interface SearchOptions {
816 size ?: number
917}
@@ -142,7 +150,7 @@ export function useSearch(
142150
143151 if ( ! q . trim ( ) ) {
144152 isRateLimited . value = false
145- return emptySearchResponse ( )
153+ return emptySearchPayload ( )
146154 }
147155
148156 const opts = toValue ( options )
@@ -156,29 +164,37 @@ export function useSearch(
156164 const result = await algoliaMultiSearch ( q , { size : opts . size ?? 25 } , checks )
157165
158166 if ( q !== toValue ( query ) ) {
159- return emptySearchResponse ( )
167+ return emptySearchPayload ( )
160168 }
161169
162170 isRateLimited . value = false
163171 processAlgoliaChecks ( q , checks , result )
164- return result . search
172+ return {
173+ searchResponse : result . search ,
174+ suggestions : suggestions . value ,
175+ packageAvailability : packageAvailability . value ,
176+ }
165177 }
166178
167179 const response = await searchAlgolia ( q , { size : opts . size ?? 25 } )
168180
169181 if ( q !== toValue ( query ) ) {
170- return emptySearchResponse ( )
182+ return emptySearchPayload ( )
171183 }
172184
173185 isRateLimited . value = false
174- return response
186+ return {
187+ searchResponse : response ,
188+ suggestions : [ ] ,
189+ packageAvailability : null ,
190+ }
175191 }
176192
177193 try {
178194 const response = await searchNpm ( q , { size : opts . size ?? 25 } , signal )
179195
180196 if ( q !== toValue ( query ) ) {
181- return emptySearchResponse ( )
197+ return emptySearchPayload ( )
182198 }
183199
184200 cache . value = {
@@ -189,20 +205,41 @@ export function useSearch(
189205 }
190206
191207 isRateLimited . value = false
192- return response
208+ return {
209+ searchResponse : response ,
210+ suggestions : [ ] ,
211+ packageAvailability : null ,
212+ }
193213 } catch ( error : unknown ) {
194214 const errorMessage = ( error as { message ?: string } ) ?. message || String ( error )
195215 const isRateLimitError =
196216 errorMessage . includes ( 'Failed to fetch' ) || errorMessage . includes ( '429' )
197217
198218 if ( isRateLimitError ) {
199219 isRateLimited . value = true
200- return emptySearchResponse ( )
220+ return emptySearchPayload ( )
201221 }
202222 throw error
203223 }
204224 } ,
205- { default : emptySearchResponse } ,
225+ { default : emptySearchPayload } ,
226+ )
227+
228+ watch (
229+ [ ( ) => asyncData . data . value . suggestions , ( ) => suggestions . value ] ,
230+ ( [ payloadSuggestions , localSuggestions ] ) => {
231+ if ( ! payloadSuggestions . length && ! localSuggestions . length ) return
232+ suggestions . value = payloadSuggestions . length ? payloadSuggestions : localSuggestions
233+ } ,
234+ { immediate : true } ,
235+ )
236+
237+ watch (
238+ [ ( ) => asyncData . data . value ?. packageAvailability , ( ) => packageAvailability . value ] ,
239+ ( [ payloadPackageAvailability , localPackageAvailability ] ) => {
240+ packageAvailability . value = payloadPackageAvailability || localPackageAvailability
241+ } ,
242+ { immediate : true } ,
206243 )
207244
208245 async function fetchMore ( targetSize : number ) : Promise < void > {
@@ -222,12 +259,12 @@ export function useSearch(
222259
223260 // Seed cache from asyncData for Algolia (which skips cache on initial fetch)
224261 if ( ! cache . value && asyncData . data . value ) {
225- const d = asyncData . data . value
262+ const { searchResponse } = asyncData . data . value
226263 cache . value = {
227264 query : q ,
228265 provider,
229- objects : [ ...d . objects ] ,
230- total : d . total ,
266+ objects : [ ...searchResponse . objects ] ,
267+ total : searchResponse . total ,
231268 }
232269 }
233270
@@ -306,10 +343,10 @@ export function useSearch(
306343 time : new Date ( ) . toISOString ( ) ,
307344 }
308345 }
309- return asyncData . data . value
346+ return asyncData . data . value ?. searchResponse ?? null
310347 } )
311348
312- if ( import . meta. client && asyncData . data . value ?. isStale ) {
349+ if ( import . meta. client && asyncData . data . value ?. searchResponse . isStale ) {
313350 onMounted ( ( ) => {
314351 asyncData . refresh ( )
315352 } )
0 commit comments