File size: 2,089 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import { useCallback } from 'react';
import { useInstantSearchContext } from '../lib/useInstantSearchContext';
import { useIsomorphicLayoutEffect } from '../lib/useIsomorphicLayoutEffect';
import { useSearchResults } from '../lib/useSearchResults';
import { useSearchState } from '../lib/useSearchState';
import type { SearchResultsApi } from '../lib/useSearchResults';
import type { SearchStateApi } from '../lib/useSearchState';
import type { InstantSearch, Middleware, UiState } from 'instantsearch.js';
type InstantSearchApi<TUiState extends UiState> = SearchStateApi<TUiState> &
SearchResultsApi & {
use: (...middlewares: Middleware[]) => () => void;
refresh: InstantSearch['refresh'];
status: InstantSearch['status'];
error: InstantSearch['error'];
};
export type UseInstantSearchProps = {
/**
* catch any error happening in the search lifecycle and handle it with this hook.
*/
catchError?: boolean;
};
export function useInstantSearch<TUiState extends UiState = UiState>({
catchError,
}: UseInstantSearchProps = {}): InstantSearchApi<TUiState> {
const search = useInstantSearchContext<TUiState>();
const { uiState, setUiState, indexUiState, setIndexUiState } =
useSearchState<TUiState>();
const { results, scopedResults } = useSearchResults();
const use: InstantSearchApi<TUiState>['use'] = useCallback(
(...middlewares: Middleware[]) => {
search.use(...middlewares);
return () => {
search.unuse(...middlewares);
};
},
[search]
);
const refresh: InstantSearchApi<TUiState>['refresh'] = useCallback(() => {
search.refresh();
}, [search]);
useIsomorphicLayoutEffect(() => {
if (catchError) {
const onError = () => {};
search.addListener('error', onError);
return () => search.removeListener('error', onError);
}
return () => {};
}, [search, catchError]);
return {
results,
scopedResults,
uiState,
setUiState,
indexUiState,
setIndexUiState,
use,
refresh,
status: search.status,
error: search.error,
};
}
|