Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added simple debouncing to search suggestions to reduce queries while user is typing #1280

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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