Skip to content

Commit

Permalink
feat(docs): prepare query interception with debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenary committed Nov 28, 2024
1 parent be0373b commit b95c68c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"lodash": "^4.17.21"
}
}
36 changes: 35 additions & 1 deletion website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import Translate from '@docusaurus/Translate';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import translations from '@theme/SearchTranslations';
import debounce from 'lodash.debounce';

let DocSearchModal = null;

Expand Down Expand Up @@ -133,7 +134,40 @@ function DocSearch({ contextualSearch, externalUrlRegex, ...props }) {
'docusaurus',
siteMetadata.docusaurusVersion,
);
return searchClient;
const debouncedSearch = debounce(async (requests, resolve) => {

// const requestForApi = requests.map((request) => request.query);

// Change url to your api
// const response = await fetch('http://localhost:3001/angolia', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify(requestForApi),
// });

// const apiData = await response.json();

// const requestsForAngolia = requests.map((request) => ({
// ...request,
// query: apiData.message,
// }));

// Change requests to requestsForAngolia
const results = await searchClient.search(requests);

resolve(results);
}, 300);

const enhancedClient = {
...searchClient,
search: (requests) =>
new Promise((resolve) => {
debouncedSearch(requests, resolve);
}),
};
return enhancedClient;
},
[siteMetadata.docusaurusVersion],
);
Expand Down

0 comments on commit b95c68c

Please sign in to comment.