From 3048c75d95378c83b2cc0847ac6ad61a4ff101f4 Mon Sep 17 00:00:00 2001
From: Brilliantkid <62819346+brilliantkid87@users.noreply.github.com>
Date: Sat, 15 Mar 2025 13:02:01 +0700
Subject: [PATCH] refactor(NavbarItem): improve readability and maintainability
(#3763)
---
.../theme/NavbarItem/DefaultNavbarItem.tsx | 45 ++++++++++++++-----
1 file changed, 34 insertions(+), 11 deletions(-)
diff --git a/website/src/theme/NavbarItem/DefaultNavbarItem.tsx b/website/src/theme/NavbarItem/DefaultNavbarItem.tsx
index 6042f2bc9..d02097811 100644
--- a/website/src/theme/NavbarItem/DefaultNavbarItem.tsx
+++ b/website/src/theme/NavbarItem/DefaultNavbarItem.tsx
@@ -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] ?? '') : ''
}
-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 =
- version === 'next'
- ? 'https://api.yew.rs/next/yew'
- : `https://docs.rs/yew/${version}`
- return
+/**
+ * @param {Object} props
+ * @param {string} props.label
+ * @returns {React.ReactElement}
+ */
+export default function DefaultNavbarItem(props) {
+ const { label, ...restProps } = props
+
+ if (label === API_BUTTON) {
+ const version = useVersion()
+ const href = getApiUrl(version)
+ return
}
return