|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { useVersions, useActiveDocContext, useDocsVersionCandidates } from '@docusaurus/plugin-content-docs/client'; |
| 3 | +import { useDocsPreferredVersion } from '@docusaurus/theme-common'; |
| 4 | +import { translate } from '@docusaurus/Translate'; |
| 5 | +import { useLocation } from '@docusaurus/router'; |
| 6 | +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; |
| 7 | +import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem'; |
| 8 | +import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem'; |
| 9 | + |
| 10 | +const getVersionMainDoc = (version) => version.docs.find((doc) => doc.id === version.mainDocId); |
| 11 | + |
| 12 | +/* eslint-disable react/prop-types */ |
| 13 | +export default function DocsVersionDropdownNavbarItem({ |
| 14 | + mobile, |
| 15 | + docsPluginId, |
| 16 | + dropdownActiveClassDisabled, |
| 17 | + dropdownItemsBefore, |
| 18 | + dropdownItemsAfter, |
| 19 | + ...props |
| 20 | +}) { |
| 21 | + const { search, hash, pathname } = useLocation(); |
| 22 | + const { siteConfig } = useDocusaurusContext(); |
| 23 | + const baseUrl = siteConfig.baseUrl.endsWith('/') ? siteConfig.baseUrl : `${siteConfig.baseUrl}/`; |
| 24 | + const apiLinks = useMemo(() => { |
| 25 | + if (!pathname.startsWith(`${baseUrl}reference`)) { |
| 26 | + return []; |
| 27 | + } |
| 28 | + try { |
| 29 | + return JSON.parse(props['data-api-links']); |
| 30 | + } catch { |
| 31 | + return []; |
| 32 | + } |
| 33 | + }, [props['data-api-links'], pathname, baseUrl]); |
| 34 | + |
| 35 | + const activeDocContext = useActiveDocContext(docsPluginId); |
| 36 | + const versions = useVersions(docsPluginId); |
| 37 | + const { savePreferredVersionName } = useDocsPreferredVersion(docsPluginId); |
| 38 | + const versionLinks = versions.map((version, idx) => { |
| 39 | + // We try to link to the same doc, in another version |
| 40 | + // When not possible, fallback to the "main doc" of the version |
| 41 | + const versionDoc = activeDocContext.alternateDocVersions[version.name] ?? getVersionMainDoc(version); |
| 42 | + return { |
| 43 | + label: version.label, |
| 44 | + // preserve ?search#hash suffix on version switches |
| 45 | + to: `${apiLinks[idx] ?? versionDoc.path}${search}${hash}`, |
| 46 | + isActive: () => version === activeDocContext.activeVersion, |
| 47 | + onClick: () => savePreferredVersionName(version.name), |
| 48 | + }; |
| 49 | + }); |
| 50 | + const items = [...dropdownItemsBefore, ...versionLinks, ...dropdownItemsAfter]; |
| 51 | + const dropdownVersion = useDocsVersionCandidates(docsPluginId)[0]; |
| 52 | + // Mobile dropdown is handled a bit differently |
| 53 | + const dropdownLabel = |
| 54 | + mobile && items.length > 1 |
| 55 | + ? translate({ |
| 56 | + id: 'theme.navbar.mobileVersionsDropdown.label', |
| 57 | + message: 'Versions', |
| 58 | + description: 'The label for the navbar versions dropdown on mobile view', |
| 59 | + }) |
| 60 | + : dropdownVersion.label; |
| 61 | + let dropdownTo = mobile && items.length > 1 ? undefined : getVersionMainDoc(dropdownVersion).path; |
| 62 | + |
| 63 | + if (dropdownTo && pathname.startsWith(`${baseUrl}reference`)) { |
| 64 | + dropdownTo = versionLinks.find((v) => v.label === dropdownVersion.label)?.to; |
| 65 | + } |
| 66 | + |
| 67 | + // We don't want to render a version dropdown with 0 or 1 item. If we build |
| 68 | + // the site with a single docs version (onlyIncludeVersions: ['1.0.0']), |
| 69 | + // We'd rather render a button instead of a dropdown |
| 70 | + if (items.length <= 1) { |
| 71 | + return ( |
| 72 | + <DefaultNavbarItem |
| 73 | + {...props} |
| 74 | + mobile={mobile} |
| 75 | + label={dropdownLabel} |
| 76 | + to={dropdownTo} |
| 77 | + isActive={dropdownActiveClassDisabled ? () => false : undefined} |
| 78 | + /> |
| 79 | + ); |
| 80 | + } |
| 81 | + return ( |
| 82 | + <DropdownNavbarItem |
| 83 | + {...props} |
| 84 | + mobile={mobile} |
| 85 | + label={dropdownLabel} |
| 86 | + to={dropdownTo} |
| 87 | + items={items} |
| 88 | + isActive={dropdownActiveClassDisabled ? () => false : undefined} |
| 89 | + /> |
| 90 | + ); |
| 91 | +} |
0 commit comments