useSearchClient.js 935 B

1234567891011121314151617181920
  1. import algoliasearch from 'algoliasearch/dist/algoliasearch-lite.esm.browser';
  2. import React from 'react';
  3. import { version } from './version';
  4. export function useSearchClient(appId, apiKey, transformSearchClient) {
  5. var searchClient = React.useMemo(function () {
  6. var client = algoliasearch(appId, apiKey);
  7. client.addAlgoliaAgent('docsearch', version); // Since DocSearch.js relies on DocSearch React with an alias to Preact,
  8. // we cannot add the `docsearch-react` user agent by default, otherwise
  9. // it would also be sent on a DocSearch.js integration.
  10. // We therefore only add the `docsearch-react` user agent if `docsearch.js`
  11. // is not present.
  12. if (/docsearch.js \(.*\)/.test(client.transporter.userAgent.value) === false) {
  13. client.addAlgoliaAgent('docsearch-react', version);
  14. }
  15. return transformSearchClient(client);
  16. }, [appId, apiKey, transformSearchClient]);
  17. return searchClient;
  18. }