refactor(NavbarItem): improve readability and maintainability (#3763)

This commit is contained in:
Brilliantkid 2025-03-15 13:02:01 +07:00 committed by GitHub
parent 2ceba0d976
commit 3048c75d95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,26 +1,49 @@
import React from 'react' import React from 'react'
import { useLocation } from '@docusaurus/router'
export * from '@theme-original/NavbarItem/DefaultNavbarItem' export * from '@theme-original/NavbarItem/DefaultNavbarItem'
import OriginalNavbarItem from '@theme-original/NavbarItem/DefaultNavbarItem' import OriginalNavbarItem from '@theme-original/NavbarItem/DefaultNavbarItem'
import { API_BUTTON } from '../../constants.js' import { API_BUTTON } from '../../constants.js'
import { useLocation } from '@docusaurus/router'
const regex = /\/docs\/(0.([0-9]+)(\.[0-9]+)?|next)?/ const VERSION_REGEX = /\/docs\/(0.([0-9]+)(\.[0-9]+)?|next)?/
const API_BASE_URLS = {
next: 'https://api.yew.rs/next/yew',
default: 'https://docs.rs/yew',
}
/**
* @returns {string}
*/
const useVersion = () => { const useVersion = () => {
const location = useLocation() const location = useLocation()
const match = location.pathname.match(regex) const match = location.pathname.match(VERSION_REGEX)
return match ? (match[1] ?? '') : '' return match ? (match[1] ?? '') : ''
} }
export default function DefaultNavbarItem(props) { /**
const version = useVersion() * @param {string} version
* @returns {string}
*/
const getApiUrl = (version) => {
if (version === 'next') {
return API_BASE_URLS.next
}
return version
? `${API_BASE_URLS.default}/${version}`
: API_BASE_URLS.default
}
if (props.label === API_BUTTON) { /**
const href = * @param {Object} props
version === 'next' * @param {string} props.label
? 'https://api.yew.rs/next/yew' * @returns {React.ReactElement}
: `https://docs.rs/yew/${version}` */
return <OriginalNavbarItem {...props} href={href} /> export default function DefaultNavbarItem(props) {
const { label, ...restProps } = props
if (label === API_BUTTON) {
const version = useVersion()
const href = getApiUrl(version)
return <OriginalNavbarItem {...restProps} label={label} href={href} />
} }
return <OriginalNavbarItem {...props} /> return <OriginalNavbarItem {...props} />