Skip to content

Commit

Permalink
fix: added simple debouncing to search to reduce queries while user t…
Browse files Browse the repository at this point in the history
…ypes (#1280)
  • Loading branch information
dmitrymatio committed Jan 11, 2023
1 parent 835582a commit 1796c51
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions packages/gatsby-theme-aio/src/components/Search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ const Search = ({ algolia, indexAll, indexPrefix, showSearch, setShowSearch, sea
const [keywordResults, setKeywordResults] = useState([]);
const [isSuggestionsOpen, setIsSuggestionsOpen] = useState(false);
const [triggerSearch, setTriggerSearch] = useState(false);
const [searchQueryCounter, setSearchQueryCounter] = useState(0);

const search = async () => {
if (searchQuery.length) {
Expand Down Expand Up @@ -397,6 +398,42 @@ const Search = ({ algolia, indexAll, indexPrefix, showSearch, setShowSearch, sea
};
}, [setShowSearch]);

useEffect(() => {
// simple debouncing strategy for search queries while user is typing
const timeOutId = setTimeout(() => debounceCallback(), 500);
const debounceCallback = async () => {
if (searchQuery.length && !searchResults.length) {
setShowClear(true);

const suggestions = await searchSuggestions(algolia, searchQuery, searchIndex, indexAll, existingIndices, setExistingIndices);
setSearchQueryCounter(searchQueryCounter + 1);
console.log('Total search queries counted is:', searchQueryCounter);

if (suggestions?.results?.length) {
const results = [];
suggestions.results.forEach(({ hits }) => {
mapSearchResults(hits, results);
});
setSearchSuggestionResults(results);

if (!searchResults.length) {
setShowSearchResults(false);
}
} else {
setSearchSuggestionResults([]);
}

setIsSuggestionsOpen(true);
} else {
setShowClear(false);
setIsSuggestionsOpen(false);
}
}

return () => clearTimeout(timeOutId);

}, [searchQuery])

if (isIFramed) {
useEffect(() => {
if (suggestionsRef) {
Expand Down Expand Up @@ -485,31 +522,6 @@ const Search = ({ algolia, indexAll, indexPrefix, showSearch, setShowSearch, sea
onChange={async (e) => {
const query = e.target.value;
setSearchQuery(query);

if (query.length && !searchResults.length) {
setShowClear(true);

const suggestions = await searchSuggestions(algolia, query, searchIndex, indexAll, existingIndices, setExistingIndices);

if (suggestions?.results?.length) {
const results = [];
suggestions.results.forEach(({ hits }) => {
mapSearchResults(hits, results);
});
setSearchSuggestionResults(results);

if (!searchResults.length) {
setShowSearchResults(false);
}
} else {
setSearchSuggestionResults([]);
}

setIsSuggestionsOpen(true);
} else {
setShowClear(false);
setIsSuggestionsOpen(false);
}
}}
aria-label="Search"
type="search"
Expand Down

0 comments on commit 1796c51

Please sign in to comment.