Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(NavbarItem): improve readability and maintainability #3763

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions website/src/theme/NavbarItem/DefaultNavbarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
import React from 'react'
import { useLocation } from '@docusaurus/router'
export * from '@theme-original/NavbarItem/DefaultNavbarItem'
import OriginalNavbarItem from '@theme-original/NavbarItem/DefaultNavbarItem'
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 location = useLocation()
const match = location.pathname.match(regex)
const match = location.pathname.match(VERSION_REGEX)
return match ? (match[1] ?? '') : ''
}

/**
* @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
}

/**
* @param {Object} props
* @param {string} props.label
* @returns {React.ReactElement}
*/
export default function DefaultNavbarItem(props) {
const version = useVersion()
const { label, ...restProps } = props

if (props.label === API_BUTTON) {
const href =
version === 'next'
? 'https://api.yew.rs/next/yew'
: `https://docs.rs/yew/${version}`
return <OriginalNavbarItem {...props} href={href} />
if (label === API_BUTTON) {
const version = useVersion()
const href = getApiUrl(version)
return <OriginalNavbarItem {...restProps} label={label} href={href} />
}

return <OriginalNavbarItem {...props} />
Expand Down
Loading