1
0
Fork 0
docsearch/packages/docsearch-react/src/useSearchClient.ts
renovate[bot] 10c5b2ea14
chore(deps): update lint dependency (#1120)
Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com>
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
2021-11-02 12:06:22 +01:00

31 lines
1.1 KiB
TypeScript

import algoliasearch from 'algoliasearch/dist/algoliasearch-lite.esm.browser';
import type { SearchClient } from 'algoliasearch/lite';
import React from 'react';
import { version } from './version';
export function useSearchClient(
appId: string,
apiKey: string,
transformSearchClient: (searchClient: SearchClient) => SearchClient
): SearchClient {
const searchClient = React.useMemo(() => {
const client = algoliasearch(appId, apiKey);
client.addAlgoliaAgent('docsearch', version);
// Since DocSearch.js relies on DocSearch React with an alias to Preact,
// we cannot add the `docsearch-react` user agent by default, otherwise
// it would also be sent on a DocSearch.js integration.
// We therefore only add the `docsearch-react` user agent if `docsearch.js`
// is not present.
if (
/docsearch.js \(.*\)/.test(client.transporter.userAgent.value) === false
) {
client.addAlgoliaAgent('docsearch-react', version);
}
return transformSearchClient(client);
}, [appId, apiKey, transformSearchClient]);
return searchClient;
}