feat(docsearch): introduce transformSearchClient API
`transformSearchClient` allows to attach user agents to the search client, to manipulate the cache, etc.
This commit is contained in:
parent
a358695c25
commit
70b4de690d
6 changed files with 20 additions and 10 deletions
|
|
@ -9,9 +9,10 @@ import { MAX_QUERY_SIZE } from './constants';
|
|||
import {
|
||||
DocSearchHit,
|
||||
InternalDocSearchHit,
|
||||
SearchClient,
|
||||
StoredDocSearchHit,
|
||||
} from './types';
|
||||
import { groupBy, noop } from './utils';
|
||||
import { groupBy, identity, noop } from './utils';
|
||||
import { createStoredSearches } from './stored-searches';
|
||||
import { useSearchClient } from './useSearchClient';
|
||||
import { useTrapFocus } from './useTrapFocus';
|
||||
|
|
@ -23,14 +24,11 @@ import { ScreenState } from './ScreenState';
|
|||
import { Footer } from './Footer';
|
||||
|
||||
interface DocSearchModalProps extends DocSearchProps {
|
||||
transformSearchClient?(searchClient: SearchClient): SearchClient;
|
||||
initialScrollY: number;
|
||||
onClose?(): void;
|
||||
}
|
||||
|
||||
function defaultTransformItems(x: DocSearchHit[]) {
|
||||
return x;
|
||||
}
|
||||
|
||||
export function DocSearchModal({
|
||||
appId = 'BH4D9OD16A',
|
||||
apiKey,
|
||||
|
|
@ -38,11 +36,12 @@ export function DocSearchModal({
|
|||
placeholder = 'Search docs',
|
||||
searchParameters,
|
||||
onClose = noop,
|
||||
transformItems = defaultTransformItems,
|
||||
transformItems = identity,
|
||||
hitComponent = Hit,
|
||||
resultsFooterComponent = () => null,
|
||||
navigator,
|
||||
initialScrollY = 0,
|
||||
transformSearchClient = identity,
|
||||
}: DocSearchModalProps) {
|
||||
const [state, setState] = React.useState<
|
||||
AutocompleteState<InternalDocSearchHit>
|
||||
|
|
@ -62,7 +61,7 @@ export function DocSearchModal({
|
|||
: ''
|
||||
).current;
|
||||
|
||||
const searchClient = useSearchClient(appId, apiKey);
|
||||
const searchClient = useSearchClient(appId, apiKey, transformSearchClient);
|
||||
const favoriteSearches = React.useRef(
|
||||
createStoredSearches<StoredDocSearchHit>({
|
||||
key: `__DOCSEARCH_FAVORITE_SEARCHES__${indexName}`,
|
||||
|
|
|
|||
1
src/types/SearchClient.ts
Normal file
1
src/types/SearchClient.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export type SearchClient = any;
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
export * from './DocSearchHit';
|
||||
export * from './InternalDocSearchHit';
|
||||
export * from './SearchClient';
|
||||
export * from './StoredDocSearchHit';
|
||||
|
|
|
|||
|
|
@ -1,15 +1,20 @@
|
|||
import React from 'react';
|
||||
import algoliasearch from 'algoliasearch/dist/algoliasearch-lite.esm.browser';
|
||||
|
||||
import { SearchClient } from './types';
|
||||
import { version } from './version';
|
||||
|
||||
export function useSearchClient(appId: string, apiKey: string) {
|
||||
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})`);
|
||||
|
||||
return client;
|
||||
}, [appId, apiKey]);
|
||||
return transformSearchClient(client);
|
||||
}, [appId, apiKey, transformSearchClient]);
|
||||
|
||||
return searchClient;
|
||||
}
|
||||
|
|
|
|||
3
src/utils/identity.ts
Normal file
3
src/utils/identity.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export function identity<TParam>(x: TParam): TParam {
|
||||
return x;
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
export * from './groupBy';
|
||||
export * from './identity';
|
||||
export * from './noop';
|
||||
|
|
|
|||
Loading…
Reference in a new issue