From 3ce131419fdf55aaf0d566814ce0a0d8184d1b85 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Thu, 6 Feb 2025 00:15:37 +0800 Subject: [PATCH] refactor: optimize Header component with memoization and time tracking --- app/(main)/header.tsx | 175 +++++++++++++++++++++++------------------- 1 file changed, 94 insertions(+), 81 deletions(-) diff --git a/app/(main)/header.tsx b/app/(main)/header.tsx index 6284f02..692c876 100644 --- a/app/(main)/header.tsx +++ b/app/(main)/header.tsx @@ -9,7 +9,94 @@ import NumberFlow, { NumberFlowGroup } from "@number-flow/react" import { DateTime } from "luxon" import { useTranslations } from "next-intl" import { useRouter } from "next/navigation" -import React from "react" +import { memo, useCallback, useEffect, useState } from "react" + +interface TimeState { + hh: number + mm: number + ss: number +} + +interface CustomLink { + link: string + name: string +} + +const useCurrentTime = () => { + const [time, setTime] = useState({ + hh: DateTime.now().setLocale("en-US").hour, + mm: DateTime.now().setLocale("en-US").minute, + ss: DateTime.now().setLocale("en-US").second, + }) + + useEffect(() => { + const timer = setInterval(() => { + const now = DateTime.now().setLocale("en-US") + setTime({ + hh: now.hour, + mm: now.minute, + ss: now.second, + }) + }, 1000) + + return () => clearInterval(timer) + }, []) + + return time +} + +const Links = memo(function Links() { + const linksEnv = getEnv("NEXT_PUBLIC_Links") + const links: CustomLink[] | null = linksEnv ? JSON.parse(linksEnv) : null + + if (!links) return null + + return ( +
+ {links.map((link) => ( + + {link.name} + + ))} +
+ ) +}) + +const Overview = memo(function Overview() { + const t = useTranslations("Overview") + const time = useCurrentTime() + + return ( +
+

{t("p_2277-2331_Overview")}

+
+

{t("p_2390-2457_wherethetimeis")}

+ +
+ + +

:{time.ss.toString().padStart(2, "0")}

+
+
+
+
+ ) +}) function Header() { const t = useTranslations("Header") @@ -19,14 +106,16 @@ function Header() { const router = useRouter() + const handleLogoClick = useCallback(() => { + sessionStorage.removeItem("selectedTag") + router.push("/") + }, [router]) + return (
{ - sessionStorage.removeItem("selectedTag") - router.push("/") - }} + onClick={handleLogoClick} className="flex cursor-pointer items-center text-base font-medium hover:opacity-50 transition-opacity duration-300" >
@@ -67,80 +156,4 @@ function Header() { ) } -type links = { - link: string - name: string -} - -function Links() { - const linksEnv = getEnv("NEXT_PUBLIC_Links") - - const links: links[] | null = linksEnv ? JSON.parse(linksEnv) : null - - if (!links) return null - - return ( -
- {links.map((link) => { - return ( - - {link.name} - - ) - })} -
- ) -} - -function Overview() { - const t = useTranslations("Overview") - const [time, setTime] = React.useState({ - hh: DateTime.now().setLocale("en-US").hour, - mm: DateTime.now().setLocale("en-US").minute, - ss: DateTime.now().setLocale("en-US").second, - }) - - React.useEffect(() => { - const timer = setInterval(() => { - setTime({ - hh: DateTime.now().setLocale("en-US").hour, - mm: DateTime.now().setLocale("en-US").minute, - ss: DateTime.now().setLocale("en-US").second, - }) - }, 1000) - - return () => clearInterval(timer) - }, []) - - return ( -
-

{t("p_2277-2331_Overview")}

-
-

{t("p_2390-2457_wherethetimeis")}

- -
- - -

:{time.ss.toString().padStart(2, "0")}

-
-
-
-
- ) -} export default Header