|
| 1 | +import { |
| 2 | + autocomplete, |
| 3 | + getAlgoliaHits, |
| 4 | + reverseHighlightHit, |
| 5 | +} from '@algolia/autocomplete-js'; |
| 6 | +import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights'; |
| 7 | +import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions'; |
| 8 | +import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches'; |
| 9 | +import algoliasearch from 'algoliasearch'; |
| 10 | +import insightsClient from 'search-insights'; |
| 11 | + |
| 12 | +import '@algolia/autocomplete-theme-classic'; |
| 13 | + |
| 14 | +const appId = 'latency'; |
| 15 | +const apiKey = '6be0576ff61c053d5f9a3225e2a90f76'; |
| 16 | +const searchClient = algoliasearch(appId, apiKey); |
| 17 | +insightsClient('init', { appId, apiKey }); |
| 18 | + |
| 19 | +const algoliaInsightsPlugin = createAlgoliaInsightsPlugin({ insightsClient }); |
| 20 | +const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({ |
| 21 | + key: 'search', |
| 22 | + limit: 3, |
| 23 | +}); |
| 24 | +const querySuggestionsPlugin = createQuerySuggestionsPlugin({ |
| 25 | + searchClient, |
| 26 | + indexName: 'instant_search_demo_query_suggestions', |
| 27 | + getSearchParams() { |
| 28 | + return recentSearchesPlugin.data.getAlgoliaSearchParams({ |
| 29 | + clickAnalytics: true, |
| 30 | + }); |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +autocomplete({ |
| 35 | + container: '#autocomplete', |
| 36 | + placeholder: 'Search', |
| 37 | + openOnFocus: true, |
| 38 | + plugins: [ |
| 39 | + algoliaInsightsPlugin, |
| 40 | + recentSearchesPlugin, |
| 41 | + querySuggestionsPlugin, |
| 42 | + ], |
| 43 | + getSources({ query }) { |
| 44 | + if (!query) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + return [ |
| 49 | + { |
| 50 | + getItems() { |
| 51 | + return getAlgoliaHits({ |
| 52 | + searchClient, |
| 53 | + queries: [{ indexName: 'instant_search', query }], |
| 54 | + }); |
| 55 | + }, |
| 56 | + templates: { |
| 57 | + item({ item, root }) { |
| 58 | + const itemContent = document.createElement('div'); |
| 59 | + const ItemSourceIcon = document.createElement('div'); |
| 60 | + const itemTitle = document.createElement('div'); |
| 61 | + const sourceIcon = document.createElement('img'); |
| 62 | + |
| 63 | + sourceIcon.width = 20; |
| 64 | + sourceIcon.height = 20; |
| 65 | + sourceIcon.src = item.image; |
| 66 | + |
| 67 | + ItemSourceIcon.classList.add('aa-ItemSourceIcon'); |
| 68 | + ItemSourceIcon.appendChild(sourceIcon); |
| 69 | + |
| 70 | + itemTitle.innerHTML = reverseHighlightHit({ |
| 71 | + hit: item, |
| 72 | + attribute: 'name', |
| 73 | + }); |
| 74 | + itemTitle.classList.add('aa-ItemTitle'); |
| 75 | + |
| 76 | + itemContent.classList.add('aa-ItemContent'); |
| 77 | + itemContent.appendChild(ItemSourceIcon); |
| 78 | + itemContent.appendChild(itemTitle); |
| 79 | + |
| 80 | + root.appendChild(itemContent); |
| 81 | + }, |
| 82 | + empty({ root }) { |
| 83 | + const itemContent = document.createElement('div'); |
| 84 | + |
| 85 | + itemContent.innerHTML = 'No results for this query'; |
| 86 | + itemContent.classList.add('aa-ItemContent'); |
| 87 | + |
| 88 | + root.appendChild(itemContent); |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + ]; |
| 93 | + }, |
| 94 | +}); |
0 commit comments