|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + useVersions, |
| 4 | + useActiveDocContext, |
| 5 | + useDocsVersionCandidates, |
| 6 | + useDocsPreferredVersion, |
| 7 | +} from '@docusaurus/plugin-content-docs/client'; |
| 8 | +import {translate} from '@docusaurus/Translate'; |
| 9 | +import {useLocation} from '@docusaurus/router'; |
| 10 | +import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem'; |
| 11 | +import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem'; |
| 12 | + |
| 13 | +function getVersionMainDoc(version) { |
| 14 | + return version.docs.find((doc) => doc.id === version.mainDocId); |
| 15 | +} |
| 16 | + |
| 17 | +function getVersionTargetDoc(version, activeDocContext) { |
| 18 | + // We try to link to the same doc, in another version |
| 19 | + // When not possible, fallback to the "main doc" of the version |
| 20 | + return ( |
| 21 | + activeDocContext.alternateDocVersions[version.name] ?? |
| 22 | + getVersionMainDoc(version) |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +export default function DocsVersionDropdownNavbarItem({ |
| 27 | + mobile, |
| 28 | + docsPluginId, |
| 29 | + dropdownActiveClassDisabled, |
| 30 | + dropdownItemsBefore = [], |
| 31 | + dropdownItemsAfter = [], |
| 32 | + ...props |
| 33 | +}) { |
| 34 | + const {search, hash} = useLocation(); |
| 35 | + const activeDocContext = useActiveDocContext(docsPluginId); |
| 36 | + const versions = useVersions(docsPluginId); |
| 37 | + const {savePreferredVersionName} = useDocsPreferredVersion(docsPluginId); |
| 38 | + |
| 39 | + // Filter out versions with "unsupported" or "サポートされていない" in the label |
| 40 | + const supportedVersions = versions.filter((version) => { |
| 41 | + const label = version.label.toLowerCase(); |
| 42 | + return ( |
| 43 | + !label.includes('(unsupported)') && |
| 44 | + !label.includes('(サポートされていない)') |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + function versionToLink(version) { |
| 49 | + const targetDoc = getVersionTargetDoc(version, activeDocContext); |
| 50 | + return { |
| 51 | + label: version.label, |
| 52 | + // preserve ?search#hash suffix on version switches |
| 53 | + to: `${targetDoc.path}${search}${hash}`, |
| 54 | + isActive: () => version === activeDocContext.activeVersion, |
| 55 | + onClick: () => savePreferredVersionName(version.name), |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + const items = [ |
| 60 | + ...dropdownItemsBefore, |
| 61 | + ...supportedVersions.map(versionToLink), |
| 62 | + ...dropdownItemsAfter, |
| 63 | + { |
| 64 | + label: translate({ |
| 65 | + id: 'navbar.unsupportedVersions', |
| 66 | + message: 'Unsupported Versions', |
| 67 | + }), |
| 68 | + to: '/unsupported-versions', |
| 69 | + }, |
| 70 | + ]; |
| 71 | + |
| 72 | + const dropdownVersion = useDocsVersionCandidates(docsPluginId)[0]; |
| 73 | + // Mobile dropdown is handled a bit differently |
| 74 | + const dropdownLabel = |
| 75 | + mobile && items.length > 1 |
| 76 | + ? translate({ |
| 77 | + id: 'theme.navbar.mobileVersionsDropdown.label', |
| 78 | + message: 'Versions', |
| 79 | + description: |
| 80 | + 'The label for the navbar versions dropdown on mobile view', |
| 81 | + }) |
| 82 | + : dropdownVersion.label; |
| 83 | + |
| 84 | + const dropdownTo = |
| 85 | + mobile && items.length > 1 |
| 86 | + ? undefined |
| 87 | + : getVersionTargetDoc(dropdownVersion, activeDocContext).path; |
| 88 | + // We don't want to render a version dropdown with 0 or 1 item. If we build |
| 89 | + // the site with a single docs version (onlyIncludeVersions: ['1.0.0']), |
| 90 | + // We'd rather render a button instead of a dropdown |
| 91 | + |
| 92 | + if (items.length <= 1) { |
| 93 | + return ( |
| 94 | + <DefaultNavbarItem |
| 95 | + {...props} |
| 96 | + mobile={mobile} |
| 97 | + label={dropdownLabel} |
| 98 | + to={dropdownTo} |
| 99 | + isActive={dropdownActiveClassDisabled ? () => false : undefined} |
| 100 | + /> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return ( |
| 105 | + <DropdownNavbarItem |
| 106 | + {...props} |
| 107 | + mobile={mobile} |
| 108 | + label={dropdownLabel} |
| 109 | + to={dropdownTo} |
| 110 | + items={items} |
| 111 | + isActive={dropdownActiveClassDisabled ? () => false : undefined} |
| 112 | + /> |
| 113 | + ); |
| 114 | +} |
0 commit comments