Skip to content

Commit f00672a

Browse files
fix(docsearch): don't add search to recent searches if favorited
1 parent 858a498 commit f00672a

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/DocSearch.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ export function DocSearch({
6161
})
6262
).current;
6363

64+
function saveRecentSearch(item: StoredDocSearchHit) {
65+
// We save the recent search only if it's not favorited.
66+
if (
67+
favoriteSearches
68+
.getAll()
69+
.findIndex(search => search.objectID === item.objectID) === -1
70+
) {
71+
console.log('SAVED SEARCH');
72+
recentSearches.add(item);
73+
}
74+
}
75+
6476
const autocomplete = React.useMemo(
6577
() =>
6678
createAutocomplete<
@@ -139,7 +151,7 @@ export function DocSearch({
139151
return [
140152
{
141153
onSelect({ suggestion }) {
142-
recentSearches.add(suggestion);
154+
saveRecentSearch(suggestion);
143155
onClose();
144156
},
145157
getSuggestionUrl({ suggestion }) {
@@ -151,7 +163,7 @@ export function DocSearch({
151163
},
152164
{
153165
onSelect({ suggestion }) {
154-
recentSearches.add(suggestion);
166+
saveRecentSearch(suggestion);
155167
onClose();
156168
},
157169
getSuggestionUrl({ suggestion }) {
@@ -167,7 +179,7 @@ export function DocSearch({
167179
return Object.values<DocSearchHit[]>(sources).map(items => {
168180
return {
169181
onSelect({ suggestion }) {
170-
recentSearches.add(suggestion);
182+
saveRecentSearch(suggestion);
171183
onClose();
172184
},
173185
getSuggestionUrl({ suggestion }) {
@@ -280,7 +292,7 @@ export function DocSearch({
280292
recentSearches={recentSearches}
281293
favoriteSearches={favoriteSearches}
282294
onItemClick={item => {
283-
recentSearches.add(item);
295+
saveRecentSearch(item);
284296
onClose();
285297
}}
286298
inputRef={inputRef}

0 commit comments

Comments
 (0)