feat(docsearch): introduce disableUserPersonalization API
This commit is contained in:
parent
77daceeb23
commit
a40cc0351e
3 changed files with 19 additions and 5 deletions
|
|
@ -5,7 +5,7 @@ import {
|
|||
AutocompleteState,
|
||||
} from '@francoischalifour/autocomplete-core';
|
||||
|
||||
import { DocSearchHit, InternalDocSearchHit } from './types';
|
||||
import { DocSearchHit, InternalDocSearchHit, SearchClient } from './types';
|
||||
import { DocSearchButton } from './DocSearchButton';
|
||||
import { DocSearchModal } from './DocSearchModal';
|
||||
import { useDocSearchKeyboardEvents } from './useDocSearchKeyboardEvents';
|
||||
|
|
@ -25,6 +25,8 @@ export interface DocSearchProps
|
|||
resultsFooterComponent?(props: {
|
||||
state: AutocompleteState<InternalDocSearchHit>;
|
||||
}): JSX.Element | null;
|
||||
transformSearchClient?(searchClient: SearchClient): SearchClient;
|
||||
disableUserPersonalization?: boolean;
|
||||
}
|
||||
|
||||
export function DocSearch(props: DocSearchProps) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import { MAX_QUERY_SIZE } from './constants';
|
|||
import {
|
||||
DocSearchHit,
|
||||
InternalDocSearchHit,
|
||||
SearchClient,
|
||||
StoredDocSearchHit,
|
||||
} from './types';
|
||||
import { groupBy, identity, noop } from './utils';
|
||||
|
|
@ -24,7 +23,6 @@ import { ScreenState } from './ScreenState';
|
|||
import { Footer } from './Footer';
|
||||
|
||||
interface DocSearchModalProps extends DocSearchProps {
|
||||
transformSearchClient?(searchClient: SearchClient): SearchClient;
|
||||
initialScrollY: number;
|
||||
onClose?(): void;
|
||||
}
|
||||
|
|
@ -42,6 +40,7 @@ export function DocSearchModal({
|
|||
navigator,
|
||||
initialScrollY = 0,
|
||||
transformSearchClient = identity,
|
||||
disableUserPersonalization = false,
|
||||
}: DocSearchModalProps) {
|
||||
const [state, setState] = React.useState<
|
||||
AutocompleteState<InternalDocSearchHit>
|
||||
|
|
@ -79,6 +78,10 @@ export function DocSearchModal({
|
|||
|
||||
const saveRecentSearch = React.useCallback(
|
||||
function saveRecentSearch(item: InternalDocSearchHit) {
|
||||
if (disableUserPersonalization) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We don't store `content` record, but their parent if available.
|
||||
const search = item.type === 'content' ? item.__docsearch_parent : item;
|
||||
|
||||
|
|
@ -92,7 +95,7 @@ export function DocSearchModal({
|
|||
recentSearches.add(search);
|
||||
}
|
||||
},
|
||||
[favoriteSearches, recentSearches]
|
||||
[favoriteSearches, recentSearches, disableUserPersonalization]
|
||||
);
|
||||
|
||||
const autocomplete = React.useMemo(
|
||||
|
|
@ -120,6 +123,10 @@ export function DocSearchModal({
|
|||
// @ts-ignore Temporarily ignore bad typing in autocomplete-core.
|
||||
getSources({ query, state, setContext, setStatus }) {
|
||||
if (!query) {
|
||||
if (disableUserPersonalization) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
onSelect({ suggestion }) {
|
||||
|
|
@ -263,6 +270,7 @@ export function DocSearchModal({
|
|||
placeholder,
|
||||
navigator,
|
||||
transformItems,
|
||||
disableUserPersonalization,
|
||||
]
|
||||
);
|
||||
|
||||
|
|
@ -351,6 +359,7 @@ export function DocSearchModal({
|
|||
state={state}
|
||||
hitComponent={hitComponent}
|
||||
resultsFooterComponent={resultsFooterComponent}
|
||||
disableUserPersonalization={disableUserPersonalization}
|
||||
recentSearches={recentSearches}
|
||||
favoriteSearches={favoriteSearches}
|
||||
onItemClick={(item) => {
|
||||
|
|
|
|||
|
|
@ -21,13 +21,16 @@ interface StartScreenProps
|
|||
onItemClick(item: StoredDocSearchHit): void;
|
||||
recentSearches: StoredSearchPlugin<StoredDocSearchHit>;
|
||||
favoriteSearches: StoredSearchPlugin<StoredDocSearchHit>;
|
||||
disableUserPersonalization: boolean;
|
||||
}
|
||||
|
||||
export function StartScreen(props: StartScreenProps) {
|
||||
if (props.state.status === 'idle' && props.hasSuggestions === false) {
|
||||
return (
|
||||
<div className="DocSearch-StartScreen">
|
||||
<p className="DocSearch-Help">No recent searches</p>
|
||||
{!props.disableUserPersonalization && (
|
||||
<p className="DocSearch-Help">No recent searches</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue