Skip to content

Commit

Permalink
Docs: do not show version badge if there is only 1 version: #3362
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 31, 2020
1 parent ab29551 commit 58a3f5a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type GetActivePluginOptions = {failfast?: boolean};
export function getActivePlugin(
allPluginDatas: Record<string, GlobalPluginData>,
pathname: string,
options: {failfast: true},
options: {failfast: true}, // use fail-fast option if you know for sure one plugin instance is active
): ActivePlugin;
export function getActivePlugin(
allPluginDatas: Record<string, GlobalPluginData>,
Expand Down
27 changes: 15 additions & 12 deletions packages/docusaurus-theme-classic/src/theme/DocItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ import TOC from '@theme/TOC';

import clsx from 'clsx';
import styles from './styles.module.css';
import {useActivePlugin, useActiveVersion} from '@theme/hooks/useDocs';

// TODO can't we receive the version as props instead?
const useDocVersion = () => {
const version = useActiveVersion(useActivePlugin().pluginId);
if (!version) {
throw new Error("unexpected, can't get version data of doc"); // should not happen
}
return version;
};
import {
useActivePlugin,
useVersions,
useActiveVersion,
} from '@theme/hooks/useDocs';

function DocItem(props: Props): JSX.Element {
const {siteConfig = {}} = useDocusaurusContext();
Expand All @@ -49,7 +44,15 @@ function DocItem(props: Props): JSX.Element {
hide_table_of_contents: hideTableOfContents,
},
} = DocContent;
const version = useDocVersion();

const {pluginId} = useActivePlugin({failfast: true});
const versions = useVersions(pluginId);
const version = useActiveVersion(pluginId);

// If site is not versioned or only one version is included
// we don't show the version badge
// See https://github.com/facebook/docusaurus/issues/3362
const showVersionBadge = versions.length > 1;

const metaTitle = title ? `${title} | ${siteTitle}` : siteTitle;
const metaImageUrl = useBaseUrl(metaImage, {absolute: true});
Expand Down Expand Up @@ -83,7 +86,7 @@ function DocItem(props: Props): JSX.Element {
<DocVersionSuggestions />
<div className={styles.docItemContainer}>
<article>
{version && (
{showVersionBadge && (
<div>
<span className="badge badge--secondary">
Version: {version.label}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,14 @@ import {
useDocVersionSuggestions,
} from '@theme/hooks/useDocs';

const useMandatoryActiveDocsPluginId = () => {
const activePlugin = useActivePlugin();
if (!activePlugin) {
throw new Error(
'DocVersionCallout is only supposed to be used on docs-related routes',
);
}
return activePlugin.pluginId;
};

const getVersionMainDoc = (version) =>
version.docs.find((doc) => doc.id === version.mainDocId);

function DocVersionSuggestions(): JSX.Element {
const {
siteConfig: {title: siteTitle},
} = useDocusaurusContext();
const pluginId = useMandatoryActiveDocsPluginId();
const {pluginId} = useActivePlugin({failfast: true});
const activeVersion = useActiveVersion(pluginId);
const {
latestDocSuggestion,
Expand Down

0 comments on commit 58a3f5a

Please sign in to comment.