Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
refactor(docsearch): refactor favorite searches
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Apr 3, 2020
1 parent 32a09c2 commit 2b3a975
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
7 changes: 6 additions & 1 deletion packages/autocomplete-core/src/propGetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,12 @@ export function getPropGetters<TItem, TEvent, TMouseEvent, TKeyboardEvent>({

store.send('mousemove', item.__autocomplete_id);

if (store.getState().highlightedIndex !== null) {
if (
store.getState().highlightedIndex !== null &&
store
.getState()
.suggestions.some(suggestion => suggestion.items.length > 0)
) {
const { item, itemValue, itemUrl, source } = getHighlightedItem({
state: store.getState(),
});
Expand Down
53 changes: 28 additions & 25 deletions packages/docsearch-react/src/DocSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useEffect } from 'react';
import React from 'react';
import {
createAutocomplete,
AutocompleteState,
Expand Down Expand Up @@ -39,39 +39,41 @@ export function DocSearch({
suggestions: [],
} as any);

const searchBoxRef = useRef<HTMLDivElement | null>(null);
const dropdownRef = useRef<HTMLDivElement | null>(null);
const inputRef = useRef<HTMLInputElement | null>(null);
const snipetLength = useRef<number>(10);
const searchBoxRef = React.useRef<HTMLDivElement | null>(null);
const dropdownRef = React.useRef<HTMLDivElement | null>(null);
const inputRef = React.useRef<HTMLInputElement | null>(null);
const snipetLength = React.useRef<number>(10);

const searchClient = React.useMemo(() => createSearchClient(appId, apiKey), [
appId,
apiKey,
]);
const recentSearches = useRef(
const favoriteSearches = React.useRef(
createStoredSearches<StoredDocSearchHit>({
key: '__DOCSEARCH_RECENT_SEARCHES__',
limit: 5,
key: '__DOCSEARCH_FAVORITE_SEARCHES__',
limit: 10,
})
).current;
const favoriteSearches = useRef(
const recentSearches = React.useRef(
createStoredSearches<StoredDocSearchHit>({
key: '__DOCSEARCH_FAVORITE_SEARCHES__',
limit: 10,
key: '__DOCSEARCH_RECENT_SEARCHES__',
limit: 5,
})
).current;

function saveRecentSearch(item: StoredDocSearchHit) {
// We save the recent search only if it's not favorited.
if (
favoriteSearches
.getAll()
.findIndex(search => search.objectID === item.objectID) === -1
) {
console.log('SAVED SEARCH');
recentSearches.add(item);
}
}
const saveRecentSearch = React.useCallback(
function saveRecentSearch(item: StoredDocSearchHit) {
// We save the recent search only if it's not favorited.
if (
favoriteSearches
.getAll()
.findIndex(search => search.objectID === item.objectID) === -1
) {
recentSearches.add(item);
}
},
[favoriteSearches, recentSearches]
);

const autocomplete = React.useMemo(
() =>
Expand Down Expand Up @@ -219,26 +221,27 @@ export function DocSearch({
onClose,
recentSearches,
favoriteSearches,
saveRecentSearch,
]
);

const { getEnvironmentProps, getRootProps } = autocomplete;

useEffect(() => {
React.useEffect(() => {
const isMobileMediaQuery = window.matchMedia('(max-width: 750px)');

if (isMobileMediaQuery.matches) {
snipetLength.current = 5;
}
}, []);

useEffect(() => {
React.useEffect(() => {
if (dropdownRef.current) {
dropdownRef.current.scrollTop = 0;
}
}, [state.query]);

useEffect(() => {
React.useEffect(() => {
if (!(searchBoxRef.current && dropdownRef.current && inputRef.current)) {
return undefined;
}
Expand Down

0 comments on commit 2b3a975

Please sign in to comment.