ohif-viewer/platform/docs/src/theme/DocVersionBanner/index.js

139 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-06-23 04:30:33 +02:00
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import Link from '@docusaurus/Link';
import Translate from '@docusaurus/Translate';
import { useActivePlugin, useDocVersionSuggestions } from '@theme/hooks/useDocs';
2021-06-23 04:30:33 +02:00
import { useDocsPreferredVersion } from '@docusaurus/theme-common';
function UnreleasedVersionLabel({ siteTitle, versionMetadata }) {
2021-06-23 04:30:33 +02:00
return (
<Translate
id="theme.docs.versions.unreleasedVersionLabel"
description="The label used to tell the user that he's browsing an unreleased doc version"
values={{
siteTitle,
versionLabel: <b>{versionMetadata.label}</b>,
2021-06-23 04:30:33 +02:00
latestVersionLink: (
<strong>
<Link to={"/"}>
<Translate
id="theme.docs.versions.latestVersionLinkLabel"
description="The label used for the latest version suggestion link label">
docs
</Translate>
</Link>
</strong>
),
}}>
{
'This is unreleased documentation for {siteTitle} {versionLabel} version. For up-to-date documentation, see OHIF v2.0 {latestVersionLink}.'
}
</Translate>
);
}
function UnmaintainedVersionLabel({ siteTitle, versionMetadata }) {
2021-06-23 04:30:33 +02:00
return (
<Translate
id="theme.docs.versions.unmaintainedVersionLabel"
description="The label used to tell the user that he's browsing an unmaintained doc version"
values={{
siteTitle,
versionLabel: <b>{versionMetadata.label}</b>,
2021-06-23 04:30:33 +02:00
}}>
{
'This is documentation for {siteTitle} {versionLabel}, which is no longer actively maintained.'
}
</Translate>
);
}
const BannerLabelComponents = {
unreleased: UnreleasedVersionLabel,
unmaintained: UnmaintainedVersionLabel,
};
function BannerLabel(props) {
const BannerLabelComponent =
BannerLabelComponents[props.versionMetadata.banner];
return <BannerLabelComponent {...props} />;
}
2021-06-23 04:30:33 +02:00
function LatestVersionSuggestionLabel({ versionLabel, to, onClick }) {
return (
<Translate
id="theme.docs.versions.latestVersionSuggestionLabel"
description="The label used to tell the user to check the latest version"
2021-06-23 04:30:33 +02:00
values={{
versionLabel,
latestVersionLink: (
<b>
2021-06-23 04:30:33 +02:00
<Link to={to} onClick={onClick}>
<Translate
id="theme.docs.versions.latestVersionLinkLabel"
description="The label used for the latest version suggestion link label">
latest version
</Translate>
</Link>
</b>
2021-06-23 04:30:33 +02:00
),
}}>
{
'For up-to-date documentation, see the {latestVersionLink} ({versionLabel}).'
}
</Translate>
);
}
function DocVersionBannerEnabled({ versionMetadata }) {
2021-06-23 04:30:33 +02:00
const {
siteConfig: { title: siteTitle },
} = useDocusaurusContext();
const { pluginId } = useActivePlugin({
failfast: true,
});
const getVersionMainDoc = (version) =>
version.docs.find((doc) => doc.id === version.mainDocId);
2021-06-23 04:30:33 +02:00
const { savePreferredVersionName } = useDocsPreferredVersion(pluginId);
const {
latestDocSuggestion,
latestVersionSuggestion,
} = useDocVersionSuggestions(pluginId); // try to link to same doc in latest version (not always possible)
2021-06-23 04:30:33 +02:00
// fallback to main doc of latest version
const latestVersionSuggestedDoc =
latestDocSuggestion ?? getVersionMainDoc(latestVersionSuggestion);
return (
<div className="alert alert--warning margin-bottom--md" role="alert">
<div>
<BannerLabel siteTitle={siteTitle} versionMetadata={versionMetadata} />
2021-06-23 04:30:33 +02:00
</div>
{/* <div className="margin-top--md">
<LatestVersionSuggestionLabel
versionLabel={latestVersionSuggestion.label}
to={latestVersionSuggestedDoc.path}
onClick={() => savePreferredVersionName(latestVersionSuggestion.name)}
/>
</div> */}
2021-06-23 04:30:33 +02:00
</div>
);
}
function DocVersionBanner({ versionMetadata }) {
if (versionMetadata.banner === 'none') {
return <></>;
} else {
return <DocVersionBannerEnabled versionMetadata={versionMetadata} />;
}
}
export default DocVersionBanner;