const { CommandPaletteItem, CommandPaletteProvider, CommandPaletteActionResult } = require( '../types.js' ); const SearchClientFactory = require( '../searchClients/SearchClientFactory.js' ); const { getNavigationAction } = require( '../utils/providerActions.js' ); /** @type {CommandPaletteProvider} */ module.exports = { id: 'search', isAsync: true, debounceMs: 250, keepStaleResultsOnQueryChange: true, /** * Determines if this provider should handle the current query. * * @param {string} query The search query. * @return {boolean} */ canProvide( query ) { // Handles non-empty queries that are not slash commands return !!query && !query.startsWith( '/' ); }, /** * Performs the search using the configured SearchClient and returns results. * * @param {string} query The search query. * @return {Promise>} A promise resolving to search results. */ async getResults( query ) { const searchClient = SearchClientFactory.create(); try { const { fetch } = searchClient.fetchByQuery( query ); const searchResponse = await fetch; const results = searchResponse.results ?? []; // Ensure source is added by the provider return Array.isArray( results ) ? results.map( ( item ) => ( { ...item, source: this.id } ) ) : []; } catch ( error ) { if ( error.name === 'AbortError' ) { // Search was aborted, ignore return []; } throw error; } }, /** * Handles the selection of a search result item. * * @param {CommandPaletteItem} item The selected item. * @return {Promise} Action result. */ async onResultSelect( item ) { return getNavigationAction( item ); } };