-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathcategoriesPlugin.tsx
More file actions
86 lines (82 loc) · 2.53 KB
/
categoriesPlugin.tsx
File metadata and controls
86 lines (82 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/** @jsx h */
import {
AutocompletePlugin,
getAlgoliaFacetHits,
} from '@algolia/autocomplete-js';
import { SearchClient } from 'algoliasearch/lite';
import { h, Fragment } from 'preact';
type CategoryItem = {
label: string;
count: number;
};
type CreateCategoriesPluginProps = {
searchClient: SearchClient;
};
export function createCategoriesPlugin({
searchClient,
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryItem, undefined> {
return {
getSources({ query }) {
return [
{
sourceId: 'categoriesPlugin',
getItems() {
return getAlgoliaFacetHits({
searchClient,
queries: [
{
indexName: 'instant_search',
params: {
facetName: 'categories',
facetQuery: query,
maxFacetHits: query ? 3 : 10,
},
},
],
});
},
templates: {
header({ items }) {
if (items.length === 0) {
return null;
}
return (
<Fragment>
<span className="aa-SourceHeaderTitle">Categories</span>
<div className="aa-SourceHeaderLine" />
</Fragment>
);
},
item({ item, components }) {
return (
<Fragment>
<div className="aa-ItemIcon aa-ItemIcon--no-border">
<svg
viewBox="0 0 24 24"
width="18"
height="18"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" />
<polyline points="3.27 6.96 12 12.01 20.73 6.96" />
<line x1="12" y1="22.08" x2="12" y2="12" />
</svg>
</div>
<div className="aa-ItemContent">
<div className="aa-ItemContentTitle">
<components.Highlight hit={item} attribute="label" />
</div>
</div>
</Fragment>
);
},
},
},
];
},
};
}