29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
import { liteClient } from 'algoliasearch/lite';
|
|
import React from 'react';
|
|
|
|
import type { DocSearchTransformClient } from './DocSearch';
|
|
import { version } from './version';
|
|
|
|
export function useSearchClient(
|
|
appId: string,
|
|
apiKey: string,
|
|
transformSearchClient: (searchClient: DocSearchTransformClient) => DocSearchTransformClient,
|
|
): DocSearchTransformClient {
|
|
const searchClient = React.useMemo(() => {
|
|
const client = liteClient(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.algoliaAgent.value) === false) {
|
|
client.addAlgoliaAgent('docsearch-react', version);
|
|
}
|
|
|
|
return transformSearchClient(client);
|
|
}, [appId, apiKey, transformSearchClient]);
|
|
|
|
return searchClient;
|
|
}
|