From 950208f46c19973016a115c8eef18cc755b3fe70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Fri, 10 Apr 2020 14:47:51 +0200 Subject: [PATCH] refactor(docsearch): create search client hook --- src/DocSearch.tsx | 11 ++++------- src/useSearchClient.ts | 15 +++++++++++++++ src/utils/createSearchClient.ts | 13 ------------- src/utils/index.ts | 1 - 4 files changed, 19 insertions(+), 21 deletions(-) create mode 100644 src/useSearchClient.ts delete mode 100644 src/utils/createSearchClient.ts diff --git a/src/DocSearch.tsx b/src/DocSearch.tsx index fcd83fb2..ebecb1df 100644 --- a/src/DocSearch.tsx +++ b/src/DocSearch.tsx @@ -12,8 +12,9 @@ import { InternalDocSearchHit, StoredDocSearchHit, } from './types'; -import { createSearchClient, groupBy, noop } from './utils'; +import { groupBy, noop } from './utils'; import { createStoredSearches } from './stored-searches'; +import { useSearchClient } from './useSearchClient'; import { useTrapFocus } from './useTrapFocus'; import { Hit } from './Hit'; import { SearchBox } from './SearchBox'; @@ -66,11 +67,7 @@ export function DocSearch({ ).current; useTrapFocus({ container: containerRef.current }); - - const searchClient = React.useMemo(() => createSearchClient(appId, apiKey), [ - appId, - apiKey, - ]); + const searchClient = useSearchClient(appId, apiKey); const favoriteSearches = React.useRef( createStoredSearches({ key: '__DOCSEARCH_FAVORITE_SEARCHES__', @@ -88,7 +85,7 @@ export function DocSearch({ const saveRecentSearch = React.useCallback( function saveRecentSearch(item: InternalDocSearchHit) { - // We don't store `content` record, but their parent if available. + // We don't store `content` records, but their parent if available. const search = item.type === 'content' ? item.__docsearch_parent : item; // We save the recent search only if it's not favorited. diff --git a/src/useSearchClient.ts b/src/useSearchClient.ts new file mode 100644 index 00000000..a1a72879 --- /dev/null +++ b/src/useSearchClient.ts @@ -0,0 +1,15 @@ +import React from 'react'; +import algoliasearch from 'algoliasearch/dist/algoliasearch-lite.esm.browser'; + +import { version } from './version'; + +export function useSearchClient(appId: string, apiKey: string) { + const searchClient = React.useMemo(() => { + const client = algoliasearch(appId, apiKey); + client.addAlgoliaAgent(`docsearch (${version})`); + + return client; + }, [appId, apiKey]); + + return searchClient; +} diff --git a/src/utils/createSearchClient.ts b/src/utils/createSearchClient.ts deleted file mode 100644 index 439f66b6..00000000 --- a/src/utils/createSearchClient.ts +++ /dev/null @@ -1,13 +0,0 @@ -import algoliasearch from 'algoliasearch/dist/algoliasearch-lite.esm.browser'; - -import { version } from '../version'; - -export function createSearchClient(appId: string, apiKey: string) { - const searchClient = algoliasearch(appId, apiKey); - - if (typeof searchClient.addAlgoliaAgent === 'function') { - searchClient.addAlgoliaAgent(`docsearch-react (${version})`); - } - - return searchClient; -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 58e751ef..a684cfed 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,2 @@ -export * from './createSearchClient'; export * from './groupBy'; export * from './noop';