Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Add dynamic countdown logic (#1236)

Alexander Durham59d6c604d9df
diff --git a/apps/web/src/app/[locale]/_components/countdown-timer.tsx b/apps/web/src/app/[locale]/_components/countdown-timer.tsx
index 18f59bfe..21bf6141 100644
--- a/apps/web/src/app/[locale]/_components/countdown-timer.tsx
+++ b/apps/web/src/app/[locale]/_components/countdown-timer.tsx
@@ -13,15 +13,24 @@ const DateCard = ({ date, label }: { date: React.ReactNode; label: string }) =>
};
export const CountdownTimer = () => {
- const releaseDateTimeInMilliSeconds = new Date('2023-12-02T05:00:00.000Z').getTime();
+ const [releaseDate, setReleaseDate] = useState(() => {
+ return calculateNextReleaseTime();
+ });
+
const [remainingTime, setRemainingTime] = useState(
- Math.max(0, releaseDateTimeInMilliSeconds - Date.now()),
+ Math.max(0, releaseDate.getTime() - Date.now()),
);
useEffect(() => {
const countdown = () => {
- const newRemainingTime = Math.max(0, releaseDateTimeInMilliSeconds - Date.now());
+ const newRemainingTime = Math.max(0, releaseDate.getTime() - Date.now());
setRemainingTime(newRemainingTime);
+
+ if (newRemainingTime === 0) {
+ const nextReleaseDateTime = calculateNextReleaseTime();
+ setRemainingTime(nextReleaseDateTime.getTime() - Date.now());
+ setReleaseDate(nextReleaseDateTime);
+ }
};
const timerId = setInterval(countdown, 100);
@@ -29,22 +38,49 @@ export const CountdownTimer = () => {
return () => {
clearInterval(timerId);
};
- }, [remainingTime, releaseDateTimeInMilliSeconds]);
+ }, [remainingTime, releaseDate]);
const { days, hours, minutes, seconds } = calculateTimeComponents(remainingTime);
if (remainingTime === 0) return null;
return (
- <div className="flex gap-4 font-bold tabular-nums">
- <DateCard date={days} label="Days" />
- <DateCard date={hours} label="Hours" />
- <DateCard date={minutes} label="Minutes" />
- <DateCard date={seconds} label="Seconds" />
- </div>
+ <>
+ <p className="text-center text-xl font-semibold">
+ The next type challenge will unlock at <span className="text-primary">midnight(est)</span>{' '}
+ on December {formatNumberWithSuffix(Math.min(releaseDate.getUTCDate(), 25))}
+ </p>
+ <div className="flex gap-4 font-bold tabular-nums">
+ <DateCard date={days} label="Days" />
+ <DateCard date={hours} label="Hours" />
+ <DateCard date={minutes} label="Minutes" />
+ <DateCard date={seconds} label="Seconds" />
+ </div>
+ </>
);
};
+const calculateNextReleaseTime = () => {
+ const currentDate = new Date();
+ const releaseTime = new Date(currentDate);
+ releaseTime.setUTCHours(5, 0, 0, 0);
+
+ if (releaseTime.getUTCHours() <= currentDate.getUTCHours()) {
+ releaseTime.setUTCDate(releaseTime.getUTCDate() + 1);
+ }
+
+ return releaseTime;
+};
+
+const formatNumberWithSuffix = (n: number) => {
+ const suffixes = ['th', 'st', 'nd', 'rd'];
+ const lastDigit = n % 10;
+ const isSpecialCase = n >= 11 && n <= 13;
+ const suffix = isSpecialCase ? 'th' : suffixes[lastDigit] || 'th';
+
+ return `${n}${suffix}`;
+};
+
const calculateTimeComponents = (milliseconds: number, isFormatted = false) => {
const millisecondsInSecond = 1000;
const millisecondsInMinute = 60 * millisecondsInSecond;
diff --git a/apps/web/src/app/[locale]/aot-2023/_components/index.tsx b/apps/web/src/app/[locale]/aot-2023/_components/index.tsx
index 378d57ab..511c34f2 100644
--- a/apps/web/src/app/[locale]/aot-2023/_components/index.tsx
+++ b/apps/web/src/app/[locale]/aot-2023/_components/index.tsx
@@ -20,10 +20,6 @@ export async function AotLandingPage() {
</h1>
</div>
<div className="flex flex-col items-center justify-center gap-5">
- <p className="text-center text-xl font-semibold">
- The second type challenge will unlock at{' '}
- <span className="text-primary">midnight(est)</span> on December 2nd
- </p>
<CountdownTimer />
<div className="flex w-full flex-col items-center justify-center gap-2 md:w-auto md:flex-row md:gap-5">
<a