22import {
33 autocomplete ,
44 getAlgoliaHits ,
5- highlightHit ,
5+ snippetHit ,
66} from '@algolia/autocomplete-js' ;
77import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights' ;
88import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions' ;
99import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches' ;
1010import { Hit } from '@algolia/client-search' ;
1111import algoliasearch from 'algoliasearch' ;
12- import { h } from 'preact' ;
12+ import { h , Fragment } from 'preact' ;
1313import insightsClient from 'search-insights' ;
1414
1515import '@algolia/autocomplete-theme-classic' ;
1616
17- type Product = { name : string ; image : string } ;
17+ import { shortcutsPlugin } from './shortcutsPlugin' ;
18+
19+ type Product = {
20+ name : string ;
21+ image : string ;
22+ description : string ;
23+ } ;
1824type ProductHit = Hit < Product > ;
1925
2026const appId = 'latency' ;
@@ -44,6 +50,7 @@ autocomplete({
4450 debug : true ,
4551 openOnFocus : true ,
4652 plugins : [
53+ shortcutsPlugin ,
4754 algoliaInsightsPlugin ,
4855 recentSearchesPlugin ,
4956 querySuggestionsPlugin ,
@@ -59,16 +66,38 @@ autocomplete({
5966 getItems ( ) {
6067 return getAlgoliaHits < Product > ( {
6168 searchClient,
62- queries : [ { indexName : 'instant_search' , query } ] ,
69+ queries : [
70+ {
71+ indexName : 'instant_search' ,
72+ query,
73+ params : {
74+ clickAnalytics : true ,
75+ attributesToSnippet : [ 'name:10' , 'description:35' ] ,
76+ snippetEllipsisText : '…' ,
77+ } ,
78+ } ,
79+ ] ,
6380 } ) ;
6481 } ,
6582 templates : {
83+ header ( ) {
84+ return (
85+ < Fragment >
86+ < span className = "aa-SourceHeaderTitle" > Products</ span >
87+ < div className = "aa-SourceHeaderLine" > </ div >
88+ </ Fragment >
89+ ) ;
90+ } ,
6691 item ( { item } ) {
6792 return < ProductItem hit = { item } /> ;
6893 } ,
6994 empty ( ) {
7095 return (
71- < div className = "aa-ItemContent" > No results for this query.</ div >
96+ < div className = "aa-ItemContent" >
97+ < div className = "aa-ItemContentTitle" >
98+ No products for this query.
99+ </ div >
100+ </ div >
72101 ) ;
73102 } ,
74103 } ,
@@ -83,14 +112,27 @@ type ProductItemProps = {
83112
84113function ProductItem ( { hit } : ProductItemProps ) {
85114 return (
86- < div className = "aa-ItemContent" >
87- < div className = "aa-ItemSourceIcon " >
88- < img src = { hit . image } alt = { hit . name } width = "20 " height = "20 " />
115+ < Fragment >
116+ < div className = "aa-ItemIcon " >
117+ < img src = { hit . image } alt = { hit . name } width = "40 " height = "40 " />
89118 </ div >
90-
91- < div className = "aa-ItemTitle" >
92- { highlightHit < ProductHit > ( { hit, attribute : 'name' } ) }
119+ < div className = "aa-ItemContent" >
120+ < div className = "aa-ItemContentTitle" >
121+ { snippetHit < ProductHit > ( { hit, attribute : 'name' } ) }
122+ </ div >
123+ < div className = "aa-ItemContentDescription" >
124+ { snippetHit < ProductHit > ( { hit, attribute : 'description' } ) }
125+ </ div >
93126 </ div >
94- </ div >
127+ < button
128+ className = "aa-ItemActionButton aa-TouchOnly aa-ActiveOnly"
129+ type = "button"
130+ title = "Select"
131+ >
132+ < svg fill = "currentColor" viewBox = "0 0 24 24" width = "20" height = "20" >
133+ < path d = "M18.984 6.984h2.016v6h-15.188l3.609 3.609-1.406 1.406-6-6 6-6 1.406 1.406-3.609 3.609h13.172v-4.031z" > </ path >
134+ </ svg >
135+ </ button >
136+ </ Fragment >
95137 ) ;
96138}
0 commit comments