Skip to content

Commit 6f22e12

Browse files
docs(examples): improve playground types
1 parent b397149 commit 6f22e12

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

examples/js/app.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '@algolia/autocomplete-plugin-algolia-insights';
1111
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
1212
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
13-
import { Hit } from '@algolia/client-search';
1413
import algoliasearch from 'algoliasearch';
1514
import { h, Fragment } from 'preact';
1615
import insightsClient from 'search-insights';
@@ -19,15 +18,7 @@ import '@algolia/autocomplete-theme-classic';
1918

2019
import { createCategoriesPlugin } from './categoriesPlugin';
2120
import { shortcutsPlugin } from './shortcutsPlugin';
22-
23-
type Product = {
24-
name: string;
25-
image: string;
26-
description: string;
27-
__autocomplete_indexName: string;
28-
__autocomplete_queryID: string;
29-
};
30-
type ProductHit = Hit<Product>;
21+
import { ProductHit, ProductRecord } from './types';
3122

3223
const appId = 'latency';
3324
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
@@ -78,7 +69,7 @@ autocomplete({
7869
{
7970
sourceId: 'products',
8071
getItems() {
81-
return getAlgoliaHits<Product>({
72+
return getAlgoliaHits<ProductRecord>({
8273
searchClient,
8374
queries: [
8475
{

examples/js/categoriesPlugin.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import {
77
import { SearchClient } from 'algoliasearch/lite';
88
import { h, Fragment } from 'preact';
99

10-
type CategoryItem = {
10+
import { Highlighted } from './types';
11+
12+
type CategoryRecord = {
1113
label: string;
1214
count: number;
1315
};
1416

17+
type CategoryHit = Highlighted<CategoryRecord>;
18+
1519
type CreateCategoriesPluginProps = {
1620
searchClient: SearchClient;
1721
};
1822

1923
export function createCategoriesPlugin({
2024
searchClient,
21-
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryItem, undefined> {
25+
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryHit, undefined> {
2226
return {
2327
getSources({ query }) {
2428
return [
@@ -73,7 +77,7 @@ export function createCategoriesPlugin({
7377
</div>
7478
<div className="aa-ItemContent">
7579
<div className="aa-ItemContentTitle">
76-
{highlightHit<CategoryItem>({
80+
{highlightHit<CategoryHit>({
7781
hit: item,
7882
attribute: 'label',
7983
})}

examples/js/shortcutsPlugin.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ export const shortcutsPlugin: AutocompletePlugin<DarkModeItem, undefined> = {
2222
},
2323
];
2424
},
25-
onSelect({ setIsOpen }) {
25+
onSelect({ setIsOpen, setQuery, refresh }) {
2626
toggleTheme();
27+
setQuery('');
2728
setIsOpen(true);
29+
refresh();
2830
},
2931
templates: {
3032
header({ createElement, Fragment }) {

examples/js/types/Highlighted.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { HighlightResult } from '@algolia/client-search';
2+
3+
export type Highlighted<TRecord> = TRecord & {
4+
_highlightResult: HighlightResult<TRecord>;
5+
};

examples/js/types/ProductHit.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Hit } from '@algolia/client-search';
2+
3+
export type ProductRecord = {
4+
brand: string;
5+
categories: string[];
6+
description: string;
7+
free_shipping: boolean;
8+
hierarchicalCategories: {
9+
lvl0: string;
10+
lvl1?: string;
11+
lvl2?: string;
12+
lvl3?: string;
13+
lvl4?: string;
14+
lvl5?: string;
15+
lvl6?: string;
16+
};
17+
image: string;
18+
name: string;
19+
popularity: number;
20+
price: number;
21+
prince_range: string;
22+
rating: number;
23+
type: string;
24+
};
25+
26+
type WithAutocompleteAnalytics<THit> = THit & {
27+
__autocomplete_indexName: string;
28+
__autocomplete_queryID: string;
29+
};
30+
31+
export type ProductHit = WithAutocompleteAnalytics<Hit<ProductRecord>>;

examples/js/types/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './Highlighted';
2+
export * from './ProductHit';

0 commit comments

Comments
 (0)