Skip to content

Commit

Permalink
fix(search): search page should react to querystring changes + cleanu…
Browse files Browse the repository at this point in the history
…p/refactor (#8757)
  • Loading branch information
slorber committed Mar 23, 2023
1 parent 4b37c01 commit a3eef99
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 69 deletions.
74 changes: 16 additions & 58 deletions packages/docusaurus-theme-common/src/hooks/useSearchPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,38 @@
* LICENSE file in the root directory of this source tree.
*/

import {useCallback, useEffect, useState} from 'react';
import {useHistory} from '@docusaurus/router';
import {useCallback} from 'react';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import {useQueryString} from '../utils/historyUtils';
import type {ThemeConfig as AlgoliaThemeConfig} from '@docusaurus/theme-search-algolia';

const SEARCH_PARAM_QUERY = 'q';

/** Some utility functions around search queries. */
export function useSearchPage(): {
/**
* Works hand-in-hand with `setSearchQuery`; whatever the user has inputted
* into the search box.
*/
searchQuery: string;
/**
* Set a new search query. In addition to updating `searchQuery`, this handle
* also mutates the location and appends the query.
*/
setSearchQuery: (newSearchQuery: string) => void;
/**
* Given a query, this handle generates the corresponding search page link,
* with base URL prepended.
*/
generateSearchPageLink: (targetSearchQuery: string) => string;
} {
const history = useHistory();
/**
* Permits to read/write the current search query string
*/
export function useSearchQueryString(): [string, (newValue: string) => void] {
return useQueryString(SEARCH_PARAM_QUERY);
}

/**
* Permits to create links to the search page with the appropriate query string
*/
export function useSearchLinkCreator(): (searchValue: string) => string {
const {
siteConfig: {baseUrl, themeConfig},
} = useDocusaurusContext();
const {
algolia: {searchPagePath},
} = themeConfig as AlgoliaThemeConfig;

const [searchQuery, setSearchQueryState] = useState('');

// Init search query just after React hydration
useEffect(() => {
const searchQueryStringValue =
new URLSearchParams(window.location.search).get(SEARCH_PARAM_QUERY) ?? '';

setSearchQueryState(searchQueryStringValue);
}, []);

const setSearchQuery = useCallback(
(newSearchQuery: string) => {
const searchParams = new URLSearchParams(window.location.search);

if (newSearchQuery) {
searchParams.set(SEARCH_PARAM_QUERY, newSearchQuery);
} else {
searchParams.delete(SEARCH_PARAM_QUERY);
}

history.replace({
search: searchParams.toString(),
});
setSearchQueryState(newSearchQuery);
},
[history],
);

const generateSearchPageLink = useCallback(
(targetSearchQuery: string) =>
return useCallback(
(searchValue: string) =>
// Refer to https://github.com/facebook/docusaurus/pull/2838
// Note: if searchPagePath is falsy, useSearchPage() will not be called
`${baseUrl}${
searchPagePath as string
}?${SEARCH_PARAM_QUERY}=${encodeURIComponent(targetSearchQuery)}`,
}?${SEARCH_PARAM_QUERY}=${encodeURIComponent(searchValue)}`,
[baseUrl, searchPagePath],
);

return {
searchQuery,
setSearchQuery,
generateSearchPageLink,
};
}
5 changes: 5 additions & 0 deletions packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export {
type TagLetterEntry,
} from './utils/tagsUtils';

export {
useSearchQueryString,
useSearchLinkCreator,
} from './hooks/useSearchPage';

export {isMultiColumnFooterLinks} from './utils/footerUtils';

export {isRegexpStringMatch} from './utils/regexpUtils';
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-theme-common/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export {
keyboardFocusedClassName,
} from './hooks/useKeyboardNavigation';
export {useLockBodyScroll} from './hooks/useLockBodyScroll';
export {useSearchPage} from './hooks/useSearchPage';
export {useCodeWordWrap} from './hooks/useCodeWordWrap';
export {getPrismCssVariables} from './utils/codeBlockUtils';
export {useBackToTopButton} from './hooks/useBackToTopButton';
41 changes: 40 additions & 1 deletion packages/docusaurus-theme-common/src/utils/historyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {useEffect} from 'react';
import {useCallback, useEffect} from 'react';
import {useHistory} from '@docusaurus/router';
// @ts-expect-error: TODO temporary until React 18 upgrade
import {useSyncExternalStore} from 'use-sync-external-store/shim';
Expand Down Expand Up @@ -75,3 +75,42 @@ export function useQueryStringValue(key: string | null): string | null {
return new URLSearchParams(history.location.search).get(key);
});
}

export function useQueryStringKeySetter(): (
key: string,
newValue: string | null,
options?: {push: boolean},
) => void {
const history = useHistory();
return useCallback(
(key, newValue, options) => {
const searchParams = new URLSearchParams(history.location.search);
if (newValue) {
searchParams.set(key, newValue);
} else {
searchParams.delete(key);
}
const updaterFn = options?.push ? history.push : history.replace;
updaterFn({
search: searchParams.toString(),
});
},
[history],
);
}

export function useQueryString(
key: string,
): [string, (newValue: string, options?: {push: boolean}) => void] {
const value = useQueryStringValue(key) ?? '';
const setQueryString = useQueryStringKeySetter();
return [
value,
useCallback(
(newValue: string, options) => {
setQueryString(key, newValue, options);
},
[setQueryString, key],
),
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {DocSearchButton, useDocSearchKeyboardEvents} from '@docsearch/react';
import Head from '@docusaurus/Head';
import Link from '@docusaurus/Link';
import {useHistory} from '@docusaurus/router';
import {isRegexpStringMatch} from '@docusaurus/theme-common';
import {useSearchPage} from '@docusaurus/theme-common/internal';
import {
isRegexpStringMatch,
useSearchLinkCreator,
} from '@docusaurus/theme-common';
import {
useAlgoliaContextualFacetFilters,
useSearchResultUrlProcessor,
Expand Down Expand Up @@ -59,10 +61,10 @@ type ResultsFooterProps = {
};

function ResultsFooter({state, onClose}: ResultsFooterProps) {
const {generateSearchPageLink} = useSearchPage();
const createSearchLink = useSearchLinkCreator();

return (
<Link to={generateSearchPageLink(state.query)} onClick={onClose}>
<Link to={createSearchLink(state.query)} onClick={onClose}>
<Translate
id="theme.SearchBar.seeAll"
values={{count: state.context.nbHits}}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import {
HtmlClassNameProvider,
useEvent,
usePluralForm,
useSearchQueryString,
} from '@docusaurus/theme-common';
import {
useSearchPage,
useTitleFormatter,
} from '@docusaurus/theme-common/internal';
import {useTitleFormatter} from '@docusaurus/theme-common/internal';
import Translate, {translate} from '@docusaurus/Translate';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import {
Expand Down Expand Up @@ -167,7 +165,7 @@ function SearchPageContent(): JSX.Element {
const documentsFoundPlural = useDocumentsFoundPlural();

const docsSearchVersionsHelpers = useDocsSearchVersionsHelpers();
const {searchQuery, setSearchQuery} = useSearchPage();
const [searchQuery, setSearchQuery] = useSearchQueryString();
const initialSearchResultState: ResultDispatcherState = {
items: [],
query: null,
Expand Down

0 comments on commit a3eef99

Please sign in to comment.