Skip to content

Let's use AI to Earn!

feat(首页): 将头部组件抽出通用组件,并且热门内容用首页同步组件

gaoSir7190436f0239
diff --git a/src/app/[lng]/ConditionalHeader.tsx b/src/app/[lng]/ConditionalHeader.tsx
deleted file mode 100644
index 4fa76ff9..00000000
--- a/src/app/[lng]/ConditionalHeader.tsx
+++ /dev/null
@@ -1,23 +0,0 @@
-"use client";
-
-import { usePathname } from "next/navigation";
-import LyaoutHeader from "@/app/layout/LyaoutHeader";
-
-export default function ConditionalHeader() {
- const pathname = usePathname();
-
- // 检查是否是根页面(/zh-CN 或 /en 等)或 websit 下的页面
- const shouldHideHeader = (pathname: string) => {
- // 移除语言前缀,获取实际路径
- const pathWithoutLang = pathname.replace(/^\/[a-z]{2}(-[A-Z]{2})?/, '') || '/';
-
- // 如果是根页面或 websit 下的页面,则隐藏 LyaoutHeader
- return pathWithoutLang === '/' || pathWithoutLang.startsWith('/websit');
- };
-
- if (shouldHideHeader(pathname)) {
- return null;
- }
-
- return <LyaoutHeader />;
-}
\ No newline at end of file
diff --git a/src/app/[lng]/layout.tsx b/src/app/[lng]/layout.tsx
index 5fe2c563..9e58bcf4 100644
--- a/src/app/[lng]/layout.tsx
+++ b/src/app/[lng]/layout.tsx
@@ -5,7 +5,7 @@ import Head from "next/head";
import { dir } from "i18next";
import { languages, fallbackLng } from "@/app/i18n/settings";
import { useTranslation } from "@/app/i18n";
-import ConditionalHeader from "./ConditionalHeader";
+import ConditionalHeader from "../layout/ConditionalHeader";
export async function generateMetadata({
params,
diff --git a/src/app/[lng]/page.tsx b/src/app/[lng]/page.tsx
index 09454655..2b5443b0 100644
--- a/src/app/[lng]/page.tsx
+++ b/src/app/[lng]/page.tsx
@@ -72,71 +72,6 @@ function ReleaseBanner() {
);
}
-// Header 顶部导航
-function Header({ currentModule, onModuleChange }: { currentModule: string, onModuleChange: (module: string) => void }) {
- const { t } = useTransClient('home');
- const router = useRouter();
- const userStore = useUserStore();
-
- const userInfo = useUserStore((state) => state.userInfo)!;
- const toggleLanguage = () => {
- const newLng = userStore.lang === "zh-CN" ? "en" : "zh-CN";
- userStore.setLang(newLng);
- router.push(
- `/${newLng}${location.pathname.replace(`/${userStore.lang}`, "")}`,
- );
- };
-
- return (
- <header className={styles.header}>
- <div className={styles.headerContainer}>
- <div
- className={styles.logo}
- onClick={() => onModuleChange('home')}
- style={{ cursor: 'pointer' }}
- >
- <Image src={logo} alt="logo" width={50} />
- <span className={styles.logoText}>{t('header.logo')}</span>
- </div>
- <nav className={styles.nav}>
- {/* <a href="#marketplace" className={styles.navLink}>{t('header.nav.marketplace')}</a> */}
- <button
- className={`${styles.navLink} ${currentModule === 'pricing' ? styles.active : ''}`}
- onClick={() => onModuleChange('pricing')}
- >
- {t('header.nav.pricing')}
- </button>
- <a href="https://status.aitoearn.ai/" target="_blank" rel="noopener noreferrer" className={styles.navLink} style={{ paddingTop: '3px' }}>{t('header.nav.status' as any)}</a>
- <a href="https://docs.aitoearn.ai/" target="_blank" rel="noopener noreferrer" className={styles.navLink} style={{ paddingTop: '3px' }}>{t('header.nav.docs' as any)}</a>
- <a href="https://blog.aitoearn.ai/" target="_blank" rel="noopener noreferrer" className={styles.navLink} style={{ paddingTop: '3px' }}>{t('header.nav.blog' as any)}</a>
- <Link href="/hotContent" target="_blank" rel="noopener noreferrer" className={styles.navLink} style={{ paddingTop: '3px' }}>{directTrans('route', 'hotContent')}</Link>
-
- {/* <a href="#docs" className={styles.navLink}>{t('header.nav.docs')}</a> */}
- {/* <a href="#blog" className={styles.navLink}>{t('header.nav.blog')}</a> */}
- </nav>
-
- <div className={styles.headerRight}>
- <Button
- type="text"
- icon={<GlobalOutlined />}
- onClick={toggleLanguage}
- className={styles.languageButton}
- >
- {userStore.lang === "zh-CN" ? "EN" : "中文"}
- </Button>
-
- <button onClick={() => {
- router.push("/accounts");
- }} className={styles.getStartedBtn}>
- {t('header.getStarted')}
- </button>
- </div>
-
- </div>
- </header>
- );
-}
-
// Pricing 模块
function PricingModule() {
const { t } = useTransClient('pricing');
@@ -1819,6 +1754,7 @@ function UpcomingFeaturesSection() {
// 移动应用下载区
import { QRCode } from 'react-qrcode-logo';
+import HomeHeader from "@/app/layout/HomeHeader";
function DownloadSection() {
const { t } = useTransClient('home');
@@ -2242,7 +2178,6 @@ export default function Home() {
return (
<div className={styles.difyHome}>
{/* <ReleaseBanner /> */}
- <Header currentModule={currentModule} onModuleChange={handleModuleChange} />
{currentModule === 'pricing' ? (
<PricingModule />
) : (
diff --git a/src/app/layout/ConditionalHeader.tsx b/src/app/layout/ConditionalHeader.tsx
new file mode 100644
index 00000000..9652cfa8
--- /dev/null
+++ b/src/app/layout/ConditionalHeader.tsx
@@ -0,0 +1,21 @@
+"use client";
+
+import { usePathname } from "next/navigation";
+import LyaoutHeader from "@/app/layout/LyaoutHeader";
+import HomeHeader from "@/app/layout/HomeHeader";
+import { homeHeaderRouterData } from "@/app/layout/routerData";
+import { removeLocalePrefix } from "@/app/layout/layout.utils";
+
+export default function ConditionalHeader() {
+ const pathname = usePathname();
+
+ if (
+ homeHeaderRouterData.value.some(
+ (v) => v.href === removeLocalePrefix(pathname),
+ )
+ ) {
+ return <HomeHeader />;
+ }
+
+ return <LyaoutHeader />;
+}
diff --git a/src/app/layout/HomeHeader.tsx b/src/app/layout/HomeHeader.tsx
new file mode 100644
index 00000000..7fc18ca8
--- /dev/null
+++ b/src/app/layout/HomeHeader.tsx
@@ -0,0 +1,82 @@
+import { ForwardedRef, forwardRef, memo } from "react";
+import { useTransClient } from "@/app/i18n/client";
+import { usePathname, useRouter } from "next/navigation";
+import { useUserStore } from "@/store/user";
+import styles from "@/app/[lng]/styles/difyHome.module.scss";
+import Image from "next/image";
+import logo from "@/assets/images/logo.png";
+import Link from "next/link";
+import { Button } from "antd";
+import { GlobalOutlined } from "@ant-design/icons";
+import { removeLocalePrefix } from "@/app/layout/layout.utils";
+import { homeHeaderRouterData } from "@/app/layout/routerData";
+
+export interface IHomeHeaderRef {}
+
+export interface IHomeHeaderProps {}
+
+const HomeHeader = memo(
+ forwardRef(({}: IHomeHeaderProps, ref: ForwardedRef<IHomeHeaderRef>) => {
+ const pathname = usePathname();
+ const { t } = useTransClient("home");
+ const router = useRouter();
+ const userStore = useUserStore();
+
+ const toggleLanguage = () => {
+ const newLng = userStore.lang === "zh-CN" ? "en" : "zh-CN";
+ userStore.setLang(newLng);
+ router.push(
+ `/${newLng}${location.pathname.replace(`/${userStore.lang}`, "")}`,
+ );
+ };
+
+ return (
+ <header className={styles.header}>
+ <div className={styles.headerContainer}>
+ <div className={styles.logo} style={{ cursor: "pointer" }}>
+ <Image src={logo} alt="logo" width={50} />
+ <span className={styles.logoText}>{t("header.logo")}</span>
+ </div>
+ <nav className={styles.nav}>
+ {homeHeaderRouterData.value.map((v) => {
+ return (
+ <Link
+ key={v.title}
+ className={`${styles.navLink} ${removeLocalePrefix(pathname) === v.href ? styles.active : ""}`}
+ href={v.href}
+ target="_blank"
+ rel="noopener noreferrer"
+ style={{ paddingTop: "3px" }}
+ >
+ {v.title}
+ </Link>
+ );
+ })}
+ </nav>
+
+ <div className={styles.headerRight}>
+ <Button
+ type="text"
+ icon={<GlobalOutlined />}
+ onClick={toggleLanguage}
+ className={styles.languageButton}
+ >
+ {userStore.lang === "zh-CN" ? "EN" : "中文"}
+ </Button>
+
+ <button
+ onClick={() => {
+ router.push("/accounts");
+ }}
+ className={styles.getStartedBtn}
+ >
+ {t("header.getStarted")}
+ </button>
+ </div>
+ </div>
+ </header>
+ );
+ }),
+);
+
+export default HomeHeader;
diff --git a/src/app/layout/layout.utils.ts b/src/app/layout/layout.utils.ts
new file mode 100644
index 00000000..64745fee
--- /dev/null
+++ b/src/app/layout/layout.utils.ts
@@ -0,0 +1,3 @@
+export function removeLocalePrefix(path: string): string {
+ return path.replace(/^\/[a-z]{2}(-[A-Z]{2})?/, "") || "/";
+}
diff --git a/src/app/layout/routerData.ts b/src/app/layout/routerData.ts
index 1d788585..801906ab 100644
--- a/src/app/layout/routerData.ts
+++ b/src/app/layout/routerData.ts
@@ -1,5 +1,6 @@
import { StaticImageData } from "next/image";
import { MenuItemType } from "antd/es/menu/interface";
+import { directTrans } from "@/app/i18n/client";
export interface IRouterDataItem {
// 导航标题
@@ -27,11 +28,11 @@ export const routerData: IRouterDataItem[] = [
translationKey: "accounts",
path: "/accounts",
},
- {
- name: "热门内容",
- translationKey: "hotContent",
- path: "/hotContent",
- },
+ // {
+ // name: "热门内容",
+ // translationKey: "hotContent",
+ // path: "/hotContent",
+ // },
{
name: "AI工具",
translationKey: "aiTools",
@@ -68,3 +69,30 @@ const recursion = (
};
export const peRouterData = recursion(routerData);
+
+export const homeHeaderRouterData = {
+ get value() {
+ return [
+ {
+ href: "/",
+ title: directTrans("home", "header.nav.pricing"),
+ },
+ {
+ href: "https://status.aitoearn.ai/",
+ title: directTrans("home", "header.nav.status"),
+ },
+ {
+ href: "https://docs.aitoearn.ai/",
+ title: directTrans("home", "header.nav.docs"),
+ },
+ {
+ href: "https://blog.aitoearn.ai/",
+ title: directTrans("home", "header.nav.blog"),
+ },
+ {
+ href: "/hotContent",
+ title: directTrans("route", "hotContent"),
+ },
+ ];
+ },
+};