Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: 优化ConditionalHeader的匹配规则
gaoSir5768be773f9d
diff --git a/src/app/layout/ConditionalHeader.tsx b/src/app/layout/ConditionalHeader.tsxindex 9652cfa8..b542104d 100644--- a/src/app/layout/ConditionalHeader.tsx+++ b/src/app/layout/ConditionalHeader.tsx@@ -8,12 +8,17 @@ import { removeLocalePrefix } from "@/app/layout/layout.utils";export default function ConditionalHeader() {const pathname = usePathname();+ const currentPath = removeLocalePrefix(pathname).replace(/\/+$/, "") || "/";- if (- homeHeaderRouterData.value.some(- (v) => v.href === removeLocalePrefix(pathname),- )- ) {+ const isActive = (href: string) => {+ if (!href.startsWith("/")) return false;+ const normalizedHref = href.replace(/\/+$/, "") || "/";+ if (normalizedHref === "/") return currentPath === "/";+ if (currentPath === normalizedHref) return true;+ return currentPath.startsWith(normalizedHref + "/");+ };++ if (homeHeaderRouterData.value.some((v) => isActive(v.href))) {return <HomeHeader />;}diff --git a/src/app/layout/HomeHeader.tsx b/src/app/layout/HomeHeader.tsxindex 35390065..0c64d58a 100644--- a/src/app/layout/HomeHeader.tsx+++ b/src/app/layout/HomeHeader.tsx@@ -21,6 +21,7 @@ const HomeHeader = memo(const { t } = useTransClient("home");const router = useRouter();const userStore = useUserStore();+ const currentPath = removeLocalePrefix(pathname).replace(/\/+$/, "") || "/";const toggleLanguage = () => {const newLng = userStore.lang === "zh-CN" ? "en" : "zh-CN";@@ -30,6 +31,14 @@ const HomeHeader = memo();};+ const isActive = (href: string) => {+ if (!href.startsWith("/")) return false;+ const normalizedHref = href.replace(/\/+$/, "") || "/";+ if (normalizedHref === "/") return currentPath === "/";+ if (currentPath === normalizedHref) return true;+ return currentPath.startsWith(normalizedHref + "/");+ };+return (<header className={styles.header}><div className={styles.headerContainer}>@@ -46,7 +55,7 @@ const HomeHeader = memo(return (<Linkkey={v.title}- className={`${styles.navLink} ${removeLocalePrefix(pathname) === v.href ? styles.active : ""}`}+ className={`${styles.navLink} ${isActive(v.href) ? styles.active : ""}`}href={v.href}target="_blank"rel="noopener noreferrer"