Skip to content

Commit 79c8849

Browse files
committed
Fix types
1 parent eeb4d25 commit 79c8849

6 files changed

Lines changed: 42 additions & 17 deletions

File tree

examples/recently-viewed-items/app.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { h, Fragment } from 'preact';
1010
import '@algolia/autocomplete-theme-classic';
1111

1212
import { createLocalStorageRecentlyViewedItems } from './recentlyViewedItemsPlugin';
13-
import { ProductItem, ProductHit } from './types/ProductHit';
13+
import { ProductHit } from './types';
1414

1515
const appId = 'latency';
1616
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
@@ -21,7 +21,7 @@ const recentlyViewedItems = createLocalStorageRecentlyViewedItems({
2121
limit: 5,
2222
});
2323

24-
autocomplete({
24+
autocomplete<ProductHit>({
2525
container: '#autocomplete',
2626
placeholder: 'Search',
2727
openOnFocus: true,
@@ -35,7 +35,7 @@ autocomplete({
3535
{
3636
sourceId: 'products',
3737
getItems() {
38-
return getAlgoliaHits<ProductItem>({
38+
return getAlgoliaHits<ProductHit>({
3939
searchClient,
4040
queries: [
4141
{
@@ -55,6 +55,8 @@ autocomplete({
5555
id: item.objectID,
5656
label: item.name,
5757
image: item.image,
58+
url: item.url,
59+
_highlightResult: item._highlightResult,
5860
});
5961
},
6062
templates: {

examples/recently-viewed-items/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": "MIT",
77
"main": "index.html",
88
"scripts": {
9-
"build": "parcel build index.html",
9+
"build": "parcel build index.html --dist-dir ./dist",
1010
"start": "parcel index.html"
1111
},
1212
"dependencies": {
@@ -18,6 +18,8 @@
1818
"preact": "10.5.13"
1919
},
2020
"devDependencies": {
21+
"@algolia/autocomplete-core": "1.0.0-alpha.45",
22+
"@algolia/autocomplete-shared": "1.0.0-alpha.45",
2123
"parcel": "2.0.0-beta.2"
2224
},
2325
"keywords": [

examples/recently-viewed-items/recentlyViewedItemsPlugin.tsx

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
/** @jsx h */
2-
import { AutocompletePlugin } from '@algolia/autocomplete-js';
2+
import {
3+
AutocompletePlugin,
4+
AutocompleteSource,
5+
AutocompleteState,
6+
} from '@algolia/autocomplete-js';
37
import {
48
createLocalStorageRecentSearchesPlugin,
59
search,
10+
SearchParams,
611
} from '@algolia/autocomplete-plugin-recent-searches';
12+
import { MaybePromise } from '@algolia/autocomplete-shared';
713
import { h, Fragment } from 'preact';
814

9-
type RecentlyViewedItem = {
15+
import { Highlighted } from './types';
16+
17+
type RecentlyViewedRecord = {
1018
id: string;
1119
label: string;
20+
url: string;
1221
image: string;
13-
_highlightResult: {
14-
label: {
15-
value: string;
16-
};
17-
};
1822
};
1923

20-
type CreateLocalStorageRecentlyViewedItemsParams<TItem> = {
24+
type CreateLocalStorageRecentlyViewedItemsParams<
25+
TItem extends RecentlyViewedRecord
26+
> = {
2127
key: string;
2228
limit?: number;
23-
search?(params: any): any[];
29+
search?(params: SearchParams<TItem>): Array<Highlighted<TItem>>;
30+
transformSource?(params: {
31+
source: AutocompleteSource<TItem>;
32+
state: AutocompleteState<TItem>;
33+
onRemove(id: string): void;
34+
}): AutocompleteSource<TItem>;
2435
};
2536

26-
type RecentlyViewedItemsPluginData<TItem> = {
37+
type RecentlyViewedItemsPluginData<TItem extends RecentlyViewedRecord> = {
2738
addItem(item: TItem): void;
2839
removeItem(id: string): void;
29-
getAll(query?: string): any[];
40+
getAll(query?: string): MaybePromise<Array<Highlighted<TItem>>>;
3041
};
3142

3243
export function createLocalStorageRecentlyViewedItems<
33-
TItem extends RecentlyViewedItem
44+
TItem extends RecentlyViewedRecord
3445
>(
3546
params: CreateLocalStorageRecentlyViewedItemsParams<TItem>
3647
): AutocompletePlugin<TItem, RecentlyViewedItemsPluginData<TItem>> {
@@ -39,7 +50,7 @@ export function createLocalStorageRecentlyViewedItems<
3950
onSubmit,
4051
subscribe,
4152
...plugin
42-
} = createLocalStorageRecentSearchesPlugin({
53+
} = createLocalStorageRecentSearchesPlugin<TItem>({
4354
...params,
4455
search(params) {
4556
if (params.query) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type Highlighted<TItem> = TItem & {
2+
_highlightResult: {
3+
label: {
4+
value: string;
5+
};
6+
};
7+
};

examples/recently-viewed-items/types/ProductHit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Hit } from '@algolia/client-search';
33
export type ProductItem = {
44
name: string;
55
image: string;
6+
url: string;
67
description: string;
78
};
89

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)