|
| 1 | +/** |
| 2 | + * Swizzled DocsVersionDropdownNavbarItem that is aware of API reference pages. |
| 3 | + * |
| 4 | + * The upstream component only handles docs pages. When the user is browsing |
| 5 | + * the API reference (routes under /api), switching versions must navigate to |
| 6 | + * the matching API version path, not to a docs page. |
| 7 | + */ |
| 8 | +import React from 'react'; |
| 9 | +import { |
| 10 | + useVersions, |
| 11 | + useActiveDocContext, |
| 12 | + useDocsVersionCandidates, |
| 13 | + useDocsPreferredVersion, |
| 14 | +} from '@docusaurus/plugin-content-docs/client'; |
| 15 | +import { useLocation } from '@docusaurus/router'; |
| 16 | +import { translate } from '@docusaurus/Translate'; |
| 17 | +import { useHistorySelector } from '@docusaurus/theme-common'; |
| 18 | +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; |
| 19 | +import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem'; |
| 20 | +import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem'; |
| 21 | + |
| 22 | +import { getApiVersionPath } from './apiVersionUtils'; |
| 23 | + |
| 24 | +function getVersionItems(versions, configs) { |
| 25 | + if (configs) { |
| 26 | + const versionMap = new Map(versions.map((version) => [version.name, version])); |
| 27 | + const toVersionItem = (name, config) => { |
| 28 | + const version = versionMap.get(name); |
| 29 | + if (!version) { |
| 30 | + throw new Error( |
| 31 | + `No docs version exists for name '${name}', please verify your 'docsVersionDropdown' navbar item versions config.\nAvailable version names:\n- ${versions.map((v) => `${v.name}`).join('\n- ')}`, |
| 32 | + ); |
| 33 | + } |
| 34 | + return { version, label: config?.label ?? version.label }; |
| 35 | + }; |
| 36 | + if (Array.isArray(configs)) { |
| 37 | + return configs.map((name) => toVersionItem(name, undefined)); |
| 38 | + } |
| 39 | + return Object.entries(configs).map(([name, config]) => toVersionItem(name, config)); |
| 40 | + } |
| 41 | + return versions.map((version) => ({ version, label: version.label })); |
| 42 | +} |
| 43 | + |
| 44 | +function useVersionItems({ docsPluginId, configs }) { |
| 45 | + const versions = useVersions(docsPluginId); |
| 46 | + return getVersionItems(versions, configs); |
| 47 | +} |
| 48 | + |
| 49 | +function getVersionMainDoc(version) { |
| 50 | + return version.docs.find((doc) => doc.id === version.mainDocId); |
| 51 | +} |
| 52 | + |
| 53 | +function getVersionTargetDoc(version, activeDocContext) { |
| 54 | + return activeDocContext.alternateDocVersions[version.name] ?? getVersionMainDoc(version); |
| 55 | +} |
| 56 | + |
| 57 | +function useDisplayedVersionItem({ docsPluginId, versionItems }) { |
| 58 | + const candidates = useDocsVersionCandidates(docsPluginId); |
| 59 | + const candidateItems = candidates |
| 60 | + .map((candidate) => versionItems.find((vi) => vi.version === candidate)) |
| 61 | + .filter((vi) => vi !== undefined); |
| 62 | + return candidateItems[0] ?? versionItems[0]; |
| 63 | +} |
| 64 | + |
| 65 | +/** Detect whether the user is currently on an API reference page and, if so, |
| 66 | + * determine which version they are viewing. */ |
| 67 | +function useApiVersionInfo(baseUrl, versions) { |
| 68 | + const { pathname } = useLocation(); |
| 69 | + const apiPrefix = `${baseUrl}api`; |
| 70 | + |
| 71 | + if (!pathname.startsWith(apiPrefix)) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + const afterApi = pathname.slice(apiPrefix.length); |
| 76 | + const segments = afterApi.split('/').filter(Boolean); |
| 77 | + |
| 78 | + if (segments.length > 0 && segments[0] === 'next') { |
| 79 | + return { currentVersionName: 'current' }; |
| 80 | + } |
| 81 | + |
| 82 | + if (segments.length > 0) { |
| 83 | + const versionNames = new Set(versions.map((v) => v.name)); |
| 84 | + if (versionNames.has(segments[0])) { |
| 85 | + return { currentVersionName: segments[0] }; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const lastVersion = versions.find((v) => v.isLast); |
| 90 | + return { currentVersionName: lastVersion?.name }; |
| 91 | +} |
| 92 | + |
| 93 | +export default function DocsVersionDropdownNavbarItem({ |
| 94 | + mobile, |
| 95 | + docsPluginId, |
| 96 | + dropdownActiveClassDisabled, |
| 97 | + dropdownItemsBefore, |
| 98 | + dropdownItemsAfter, |
| 99 | + versions: configs, |
| 100 | + ...props |
| 101 | +}) { |
| 102 | + const { siteConfig } = useDocusaurusContext(); |
| 103 | + const { baseUrl } = siteConfig; |
| 104 | + const search = useHistorySelector((history) => history.location.search); |
| 105 | + const hash = useHistorySelector((history) => history.location.hash); |
| 106 | + const activeDocContext = useActiveDocContext(docsPluginId); |
| 107 | + const { savePreferredVersionName } = useDocsPreferredVersion(docsPluginId); |
| 108 | + const versionItems = useVersionItems({ docsPluginId, configs }); |
| 109 | + const displayedVersionItem = useDisplayedVersionItem({ docsPluginId, versionItems }); |
| 110 | + |
| 111 | + const versions = useVersions(docsPluginId); |
| 112 | + const apiInfo = useApiVersionInfo(baseUrl, versions); |
| 113 | + const isOnApiPage = apiInfo !== null; |
| 114 | + |
| 115 | + function versionItemToLink({ version, label }) { |
| 116 | + if (isOnApiPage) { |
| 117 | + const apiPath = getApiVersionPath(version, baseUrl); |
| 118 | + return { |
| 119 | + label, |
| 120 | + to: `${apiPath}${search}${hash}`, |
| 121 | + isActive: () => version.name === apiInfo.currentVersionName, |
| 122 | + onClick: () => savePreferredVersionName(version.name), |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + const targetDoc = getVersionTargetDoc(version, activeDocContext); |
| 127 | + return { |
| 128 | + label, |
| 129 | + to: `${targetDoc.path}${search}${hash}`, |
| 130 | + isActive: () => version === activeDocContext.activeVersion, |
| 131 | + onClick: () => savePreferredVersionName(version.name), |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + // When on an API page, show the version matching the current API path. |
| 136 | + let effectiveDisplayedItem = displayedVersionItem; |
| 137 | + if (isOnApiPage) { |
| 138 | + const match = versionItems.find((vi) => vi.version.name === apiInfo.currentVersionName); |
| 139 | + if (match) { |
| 140 | + effectiveDisplayedItem = match; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + const items = [...dropdownItemsBefore, ...versionItems.map(versionItemToLink), ...dropdownItemsAfter]; |
| 145 | + |
| 146 | + const dropdownLabel = |
| 147 | + mobile && items.length > 1 |
| 148 | + ? translate({ |
| 149 | + id: 'theme.navbar.mobileVersionsDropdown.label', |
| 150 | + message: 'Versions', |
| 151 | + description: 'The label for the navbar versions dropdown on mobile view', |
| 152 | + }) |
| 153 | + : effectiveDisplayedItem.label; |
| 154 | + |
| 155 | + let dropdownTo; |
| 156 | + if (mobile && items.length > 1) { |
| 157 | + dropdownTo = undefined; |
| 158 | + } else if (isOnApiPage) { |
| 159 | + dropdownTo = getApiVersionPath(effectiveDisplayedItem.version, baseUrl); |
| 160 | + } else { |
| 161 | + dropdownTo = getVersionTargetDoc(effectiveDisplayedItem.version, activeDocContext).path; |
| 162 | + } |
| 163 | + |
| 164 | + if (items.length <= 1) { |
| 165 | + return ( |
| 166 | + <DefaultNavbarItem |
| 167 | + {...props} |
| 168 | + mobile={mobile} |
| 169 | + label={dropdownLabel} |
| 170 | + to={dropdownTo} |
| 171 | + isActive={dropdownActiveClassDisabled ? () => false : undefined} |
| 172 | + /> |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + return ( |
| 177 | + <DropdownNavbarItem |
| 178 | + {...props} |
| 179 | + mobile={mobile} |
| 180 | + label={dropdownLabel} |
| 181 | + to={dropdownTo} |
| 182 | + items={items} |
| 183 | + isActive={dropdownActiveClassDisabled ? () => false : undefined} |
| 184 | + /> |
| 185 | + ); |
| 186 | +} |
0 commit comments