Skip to content

Commit

Permalink
fix: fix active plugin may be undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jun 21, 2022
1 parent 824e134 commit 0bfbbb2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
24 changes: 17 additions & 7 deletions docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
import { useHistory, useLocation } from "@docusaurus/router";
import { translate } from "@docusaurus/Translate";
import { useDocsPreferredVersion } from '@docusaurus/theme-common';
import { useActivePlugin } from '@docusaurus/plugin-content-docs/client';
import { useDocsPreferredVersion } from "@docusaurus/theme-common";
import { useActivePlugin } from "@docusaurus/plugin-content-docs/client";

import { fetchIndexes } from "./fetchIndexes";
import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
Expand Down Expand Up @@ -46,12 +46,19 @@ interface SearchBarProps {
export default function SearchBar({
handleSearchBarToggle,
}: SearchBarProps): ReactElement {
let {
const {
siteConfig: { baseUrl },
} = useDocusaurusContext();
const { pluginId } = useActivePlugin({failfast: true})!;
const { preferredVersion } = useDocsPreferredVersion(pluginId);

// It returns undefined for non-docs pages
const activePlugin = useActivePlugin();
let versionUrl = baseUrl;

// For non-docs pages while using plugin-content-docs with custom ids,
// this will throw an error of:
// > Docusaurus plugin global data not found for "docusaurus-plugin-content-docs" plugin with id "default".
// It seems that we can not get the correct id for non-docs pages.
const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId);
if (preferredVersion && !preferredVersion.isLast) {
versionUrl = preferredVersion.path + "/";
}
Expand Down Expand Up @@ -248,8 +255,11 @@ export default function SearchBar({
const onClearSearch = useCallback(() => {
const params = new URLSearchParams(location.search);
params.delete(SEARCH_PARAM_HIGHLIGHT);
let paramsStr = params.toString();
let searchUrl = location.pathname + (paramsStr != "" ? `?${paramsStr}` : "") + location.hash;
const paramsStr = params.toString();
const searchUrl =
location.pathname +
(paramsStr != "" ? `?${paramsStr}` : "") +
location.hash;
if (searchUrl != location.pathname + location.search + location.hash) {
history.push(searchUrl);
}
Expand Down
26 changes: 17 additions & 9 deletions docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import Layout from "@theme/Layout";
import Head from "@docusaurus/Head";
import Link from "@docusaurus/Link";
import { translate } from "@docusaurus/Translate";
import { usePluralForm, useDocsPreferredVersion } from "@docusaurus/theme-common";
import { useActivePlugin } from '@docusaurus/plugin-content-docs/client';
import {
usePluralForm,
useDocsPreferredVersion,
} from "@docusaurus/theme-common";
import { useActivePlugin } from "@docusaurus/plugin-content-docs/client";

import useSearchQuery from "../hooks/useSearchQuery";
import { fetchIndexes } from "../SearchBar/fetchIndexes";
Expand All @@ -28,13 +31,18 @@ export default function SearchPage(): React.ReactElement {
}

function SearchPageContent(): React.ReactElement {
let {
const {
siteConfig: { baseUrl },
} = useDocusaurusContext();
const { pluginId } = useActivePlugin({failfast: true})!;
const { preferredVersion } = useDocsPreferredVersion(pluginId);
if (preferredVersion && !preferredVersion.isLast) {
baseUrl = preferredVersion.path + "/";

// It returns undefined for non-docs pages.
const activePlugin = useActivePlugin();
let versionUrl = baseUrl;

// There is an issue, see `SearchBar.tsx`.
const { preferredVersion } = useDocsPreferredVersion(activePlugin?.pluginId);
if (preferredVersion && !preferredVersion.isLast) {
versionUrl = preferredVersion.path + "/";
}
const { selectMessage } = usePluralForm();
const { searchValue, updateSearchPath } = useSearchQuery();
Expand Down Expand Up @@ -98,13 +106,13 @@ function SearchPageContent(): React.ReactElement {

useEffect(() => {
async function doFetchIndexes() {
const { wrappedIndexes, zhDictionary } = await fetchIndexes(baseUrl);
const { wrappedIndexes, zhDictionary } = await fetchIndexes(versionUrl);
setSearchSource(() =>
SearchSourceFactory(wrappedIndexes, zhDictionary, 100)
);
}
doFetchIndexes();
}, [baseUrl]);
}, [versionUrl]);

return (
<React.Fragment>
Expand Down

0 comments on commit 0bfbbb2

Please sign in to comment.