Skip to content

Commit b397149

Browse files
docs(createRecentSearchesPlugin): rewrite docs (#469)
* docs(createRecentSearchesPlugin): rewrite docs * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]>
1 parent f2db4bd commit b397149

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

packages/website/docs/createRecentSearchesPlugin.md

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,37 @@
22
id: createRecentSearchesPlugin
33
---
44

5+
The Recent Searches plugin displays a list of the latest searches the user made.
6+
7+
The `createRecentSearchesPlugin` plugin lets you implement your own storage. To connect with the user's [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), check [`createLocalStorageRecentSearchesPlugin`](createLocalStorageRecentSearchesPlugin).
8+
9+
## Installation
10+
11+
First, you need to install the plugin.
12+
13+
```bash
14+
yarn add @algolia/autocomplete-plugin-recent-searches@alpha
15+
# or
16+
npm install @algolia/autocomplete-plugin-recent-searches@alpha
17+
```
18+
19+
Then import it in your project:
20+
21+
```js
22+
import { createRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
23+
```
24+
25+
If you don't use a package manager, you can use a standalone endpoint:
26+
27+
```html
28+
<script src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-plugin-recent-searches@alpha"></script>
29+
```
30+
531
## Example
632

7-
```ts
33+
This example uses the plugin within [`autocomplete-js`](autocomplete-js). You're in charge of implementing the storage to fetch and save recent searches.
34+
35+
```js
836
import { autocomplete } from '@algolia/autocomplete-js';
937
import { createRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
1038

@@ -24,9 +52,45 @@ autocomplete({
2452
});
2553
```
2654

27-
With [Query Suggestions](createQuerySuggestionsPlugin):
55+
For example, you can plug it to a MongoDB database using [mongoose](https://mongoosejs.com/).
2856

29-
```ts
57+
```js
58+
import mongoose from 'mongoose';
59+
import { createRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
60+
import { search } from '@algolia/autocomplete-plugin-recent-searches/usecases/localStorage';
61+
62+
mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true });
63+
64+
const schema = new mongoose.Schema({
65+
objectID: String,
66+
query: String,
67+
category: {
68+
type: String,
69+
default: undefined,
70+
},
71+
});
72+
const RecentSearchesItem = mongoose.model('RecentSearchesItem', schema);
73+
74+
const recentSearchesPlugin = createRecentSearchesPlugin({
75+
storage: {
76+
async getAll(query) {
77+
const items = await RecentSearchesItem.find({});
78+
79+
return search({ query, items, limit: 5 });
80+
},
81+
onAdd(item) {
82+
RecentSearchesItem.create(item);
83+
},
84+
onRemove(objectID) {
85+
RecentSearchesItem.deleteOne({ objectID });
86+
},
87+
},
88+
});
89+
```
90+
91+
You can combine this plugin with the [Query Suggestions](createQuerySuggestionsPlugin) plugin to leverage the empty screen with recent and popular queries.
92+
93+
```js
3094
import algoliasearch from 'algoliasearch/lite';
3195
import { autocomplete } from '@algolia/autocomplete-js';
3296
import { createRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
@@ -59,18 +123,14 @@ autocomplete({
59123
});
60124
```
61125

62-
## Import
63-
64-
```ts
65-
import { createRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
66-
```
67-
68-
## Params
126+
## Parameters
69127

70128
### `storage`
71129

72130
> `RecentSearchesStorage` | required
73131
132+
The storage to fetch from and save recent searches into.
133+
74134
```ts
75135
type RecentSearchesItem = {
76136
id: string;
@@ -85,9 +145,11 @@ type RecentSearchesStorage<TItem extends RecentSearchesItem> = {
85145

86146
### `transformSource`
87147

88-
> `(params: { source: AutocompleteSource, onRemove: () => void })`
148+
> `(params: { source: AutocompleteSource, onRemove: () => void, onTapAhead: () => void })`
89149
90-
#### Example
150+
A function to transform the provided source.
151+
152+
#### Examples
91153

92154
Keeping the panel open on select:
93155

@@ -142,3 +204,10 @@ const recentSearchesPlugin = createRecentSearchesPlugin({
142204
#### `getAlgoliaSearchParams`
143205

144206
> `SearchParameters => SearchParameters`
207+
208+
Optimized [Algolia search parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/). This is useful when using the plugin along with the [Query Suggestions](createQuerySuggestionsPlugin) plugin.
209+
210+
This function enhances the provided search parameters by:
211+
212+
- Excluding Query Suggestions that are already displayed in recent searches.
213+
- Using a shared `hitsPerPage` value to get a group limit of Query Suggestions and recent searches.

packages/website/sidebars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ module.exports = {
5151
type: 'category',
5252
label: 'autocomplete-plugin-recent-searches',
5353
items: [
54-
'createLocalStorageRecentSearchesPlugin',
5554
'createRecentSearchesPlugin',
55+
'createLocalStorageRecentSearchesPlugin',
5656
],
5757
},
5858
{

0 commit comments

Comments
 (0)