Skip to content

Commit a482116

Browse files
fix: fix countdown without using moment
1 parent fc922a6 commit a482116

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/app/components/CountDown.tsx

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use client";
22

33
import { conferenceDateTime } from "@/data/timing";
4-
import moment from "moment";
54
import Wave from "@/assets/images/home/count-down-wave.svg";
65
import WaveMobile from "@/assets/images/home/count-down-wave-mobile.svg";
76
import Image from "next/image";
@@ -24,26 +23,40 @@ export default function CountDown() {
2423
<HiOutlineClock className="absolute inset-y-0 -z-10 size-60 text-zinc-800/20 lg:size-[460px]" />
2524

2625
<div className="flex flex-col gap-2 text-center text-2xl lg:text-4xl">
27-
<strong className="font-bold">ساعت‌ها رو دوباره کوک کردیم</strong>
28-
<span className="text-zinc-400">اینجا ثانیه‌ها ارزشمندند</span>
26+
<strong className="font-bold">دلتنگتون هستیم</strong>
27+
<span className="text-zinc-400">
28+
به خاطر خاطره‌های زیبایی که ساختیم
29+
</span>
2930
</div>
3031
<CountDownTimer />
3132
<p className="text-xl text-zinc-400 lg:mt-8 lg:text-3xl/snug">
32-
تا شروع دومین همایش فرانت چپتر
33+
گذشته از آخرین دورهمی حضوری
3334
</p>
3435
</section>
3536
);
36-
}
37+
}
3738

3839
const CountDownTimer = () => {
40+
const calculateDuration = (start: number, end: number) => {
41+
const totalSeconds = Math.floor((end - start) / 1000);
42+
const days = Math.floor(totalSeconds / (3600 * 24));
43+
const hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
44+
const minutes = Math.floor((totalSeconds % 3600) / 60);
45+
const seconds = Math.floor(totalSeconds % 60);
46+
47+
return {
48+
days,
49+
hours,
50+
minutes,
51+
seconds,
52+
};
53+
};
54+
3955
const [now, setNow] = useState(Date.now());
40-
const duration = moment.duration(
41-
Math.max(moment(conferenceDateTime).diff(now), 0),
42-
);
56+
const conferenceDate = conferenceDateTime.getTime();
57+
const duration = calculateDuration(now, conferenceDate);
4358

44-
/**
45-
* Update the current time every second
46-
*/
59+
// Update the current time every second
4760
useEffect(() => {
4861
const interval = setInterval(() => {
4962
setNow(Date.now());
@@ -54,32 +67,32 @@ const CountDownTimer = () => {
5467
const times = [
5568
{
5669
label: "ثانیه",
57-
value: duration.seconds(),
70+
value: duration.seconds,
5871
},
5972
{
6073
label: "دقیقه",
61-
value: duration.minutes(),
74+
value: duration.minutes,
6275
},
6376
{
6477
label: "ساعت",
65-
value: duration.hours(),
78+
value: duration.hours,
6679
},
6780
{
6881
label: "روز",
69-
value: Math.trunc(duration.asDays()),
82+
value: duration.days,
7083
},
7184
];
7285

7386
return (
7487
<div className="mt-7 flex gap-3 lg:mt-8 lg:gap-6">
75-
{[...times].map(({ label, value }, index) => (
88+
{[...times].map(({ label, value }) => (
7689
<Fragment key={label}>
7790
<div className="flex w-12 flex-col text-center lg:w-24">
7891
<span
7992
className="text-3xl/snug text-green-500 lg:text-5xl/snug"
8093
suppressHydrationWarning
8194
>
82-
{value.toLocaleString("fa-IR")}
95+
{Math.abs(value).toLocaleString("fa-IR")}
8396
</span>
8497
<span className="text-zinc-500 lg:text-xl">{label}</span>
8598
</div>

0 commit comments

Comments
 (0)