/**
* 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, {type ComponentType, type ReactNode} from 'react';
import clsx from 'clsx';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import Link from '@docusaurus/Link';
import Translate from '@docusaurus/Translate';
import {
useActivePlugin,
useDocVersionSuggestions,
type GlobalVersion,
} from '@docusaurus/plugin-content-docs/client';
import {ThemeClassNames} from '@docusaurus/theme-common';
import {
useDocsPreferredVersion,
useDocsVersion,
} from '@docusaurus/plugin-content-docs/client';
import type {Props} from '@theme/DocVersionBanner';
import type {
VersionBanner,
PropVersionMetadata,
} from '@docusaurus/plugin-content-docs';
type BannerLabelComponentProps = {
siteTitle: string;
versionMetadata: PropVersionMetadata;
};
function UnreleasedVersionLabel({
siteTitle,
versionMetadata,
}: BannerLabelComponentProps) {
return (
{versionMetadata.label},
}}>
{
'This is unreleased documentation for {siteTitle} {versionLabel} version.'
}
);
}
function UnmaintainedVersionLabel({
siteTitle,
versionMetadata,
}: BannerLabelComponentProps) {
return (
{versionMetadata.label},
}}>
{
'This is documentation for {siteTitle} {versionLabel}, which is no longer actively maintained.'
}
);
}
const BannerLabelComponents: {
[banner in VersionBanner]: ComponentType;
} = {
unreleased: UnreleasedVersionLabel,
unmaintained: UnmaintainedVersionLabel,
};
function BannerLabel(props: BannerLabelComponentProps) {
const BannerLabelComponent =
BannerLabelComponents[props.versionMetadata.banner!];
return ;
}
function LatestVersionSuggestionLabel({
versionLabel,
to,
onClick,
}: {
to: string;
onClick: () => void;
versionLabel: string;
}) {
return (
latest version
),
}}>
{
'For up-to-date documentation, see the {latestVersionLink} ({versionLabel}).'
}
);
}
function DocVersionBannerEnabled({
className,
versionMetadata,
}: Props & {
versionMetadata: PropVersionMetadata;
}): ReactNode {
const {
siteConfig: {title: siteTitle},
} = useDocusaurusContext();
const {pluginId} = useActivePlugin({failfast: true})!;
const getVersionMainDoc = (version: GlobalVersion) =>
version.docs.find((doc) => doc.id === version.mainDocId)!;
const {savePreferredVersionName} = useDocsPreferredVersion(pluginId);
const {latestDocSuggestion, latestVersionSuggestion} =
useDocVersionSuggestions(pluginId);
// Try to link to same doc in latest version (not always possible), falling
// back to main doc of latest version
const latestVersionSuggestedDoc =
latestDocSuggestion ?? getVersionMainDoc(latestVersionSuggestion);
return (