Skip to content

Commit

Permalink
feat(docsearch): introduce transformSearchClient API
Browse files Browse the repository at this point in the history
`transformSearchClient` allows to attach user agents to the search client, to manipulate the cache, etc.
  • Loading branch information
francoischalifour committed Jul 9, 2020
1 parent a358695 commit 70b4de6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
13 changes: 6 additions & 7 deletions src/DocSearchModal.tsx
Expand Up @@ -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';
Expand All @@ -23,26 +24,24 @@ 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,
indexName,
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>
Expand All @@ -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}`,
Expand Down
1 change: 1 addition & 0 deletions src/types/SearchClient.ts
@@ -0,0 +1 @@
export type SearchClient = any;
1 change: 1 addition & 0 deletions src/types/index.ts
@@ -1,3 +1,4 @@
export * from './DocSearchHit';
export * from './InternalDocSearchHit';
export * from './SearchClient';
export * from './StoredDocSearchHit';
11 changes: 8 additions & 3 deletions src/useSearchClient.ts
@@ -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 changes: 3 additions & 0 deletions src/utils/identity.ts
@@ -0,0 +1,3 @@
export function identity<TParam>(x: TParam): TParam {
return x;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
@@ -1,2 +1,3 @@
export * from './groupBy';
export * from './identity';
export * from './noop';

0 comments on commit 70b4de6

Please sign in to comment.