feat: enhance time display with NumberFlow component

This commit is contained in:
hamster1963 2025-01-30 00:28:43 +08:00
parent 43fb66552e
commit b74e7827d3
3 changed files with 43 additions and 23 deletions

View File

@ -3,12 +3,13 @@
import { LanguageSwitcher } from "@/components/LanguageSwitcher" import { LanguageSwitcher } from "@/components/LanguageSwitcher"
import { ModeToggle } from "@/components/ThemeSwitcher" import { ModeToggle } from "@/components/ThemeSwitcher"
import { Separator } from "@/components/ui/separator" import { Separator } from "@/components/ui/separator"
import { Skeleton } from "@/components/ui/skeleton"
import getEnv from "@/lib/env-entry" import getEnv from "@/lib/env-entry"
import NumberFlow, { NumberFlowGroup } from "@number-flow/react"
import { DateTime } from "luxon" import { DateTime } from "luxon"
import { useTranslations } from "next-intl" import { useTranslations } from "next-intl"
import { useRouter } from "next/navigation" import { useRouter } from "next/navigation"
import { useEffect, useState } from "react" import React from "react"
function Header() { function Header() {
const t = useTranslations("Header") const t = useTranslations("Header")
@ -99,33 +100,51 @@ function Links() {
function Overview() { function Overview() {
const t = useTranslations("Overview") const t = useTranslations("Overview")
const [mouted, setMounted] = useState(false) const [time, setTime] = React.useState({
useEffect(() => { hh: DateTime.now().setLocale("en-US").hour,
setMounted(true) mm: DateTime.now().setLocale("en-US").minute,
}, []) ss: DateTime.now().setLocale("en-US").second,
const timeOption = DateTime.TIME_WITH_SECONDS })
timeOption.hour12 = true
const [timeString, setTimeString] = useState( React.useEffect(() => {
DateTime.now().setLocale("en-US").toLocaleString(timeOption), const timer = setInterval(() => {
) setTime({
useEffect(() => { hh: DateTime.now().setLocale("en-US").hour,
const updateTime = () => { mm: DateTime.now().setLocale("en-US").minute,
const now = DateTime.now().setLocale("en-US").toLocaleString(timeOption) ss: DateTime.now().setLocale("en-US").second,
setTimeString(now) })
requestAnimationFrame(updateTime) }, 1000)
}
requestAnimationFrame(updateTime) return () => clearInterval(timer)
}, []) }, [])
return ( return (
<section className={"mt-10 flex flex-col md:mt-16"}> <section className={"mt-10 flex flex-col md:mt-16"}>
<p className="text-base font-semibold">{t("p_2277-2331_Overview")}</p> <p className="text-base font-semibold">{t("p_2277-2331_Overview")}</p>
<div className="flex items-center gap-1.5"> <div className="flex items-center gap-1.5">
<p className="text-sm font-medium opacity-50">{t("p_2390-2457_wherethetimeis")}</p> <p className="text-sm font-medium opacity-50">{t("p_2390-2457_wherethetimeis")}</p>
{mouted ? ( <NumberFlowGroup>
<p className="text-sm font-medium">{timeString}</p> <div
) : ( style={{ fontVariantNumeric: "tabular-nums" }}
<Skeleton className="h-[20px] w-[50px] rounded-[5px] bg-muted-foreground/10 animate-none" /> className="flex text-sm font-medium mt-0.5"
)} >
<NumberFlow trend={1} value={time.hh} format={{ minimumIntegerDigits: 2 }} />
<NumberFlow
prefix=":"
trend={1}
value={time.mm}
digits={{ 1: { max: 5 } }}
format={{ minimumIntegerDigits: 2 }}
/>
<NumberFlow
prefix=":"
trend={1}
value={time.ss}
digits={{ 1: { max: 5 } }}
format={{ minimumIntegerDigits: 2 }}
/>
</div>
</NumberFlowGroup>
</div> </div>
</section> </section>
) )

BIN
bun.lockb

Binary file not shown.

View File

@ -15,6 +15,7 @@
"dependencies": { "dependencies": {
"@ducanh2912/next-pwa": "^10.2.9", "@ducanh2912/next-pwa": "^10.2.9",
"@heroicons/react": "^2.2.0", "@heroicons/react": "^2.2.0",
"@number-flow/react": "^0.5.5",
"@radix-ui/react-dialog": "^1.1.5", "@radix-ui/react-dialog": "^1.1.5",
"@radix-ui/react-dropdown-menu": "^2.1.5", "@radix-ui/react-dropdown-menu": "^2.1.5",
"@radix-ui/react-label": "^2.1.1", "@radix-ui/react-label": "^2.1.1",