openpanel/documentation/src/hooks/use-countdown.tsx
2024-02-05 10:23:04 +01:00

38 lines
1.0 KiB
TypeScript

import { useEffect, useState } from "react";
type Props = {
targetDate: string;
};
export const useCountdown = ({ targetDate }: Props) => {
const countDownDate = new Date(targetDate).getTime();
const [countDown, setCountDown] = useState(
countDownDate - new Date().getTime(),
);
useEffect(() => {
if (countDown <= 0) return setCountDown(0);
const interval = setInterval(() => {
setCountDown(countDownDate - new Date().getTime());
}, 1000);
return () => clearInterval(interval);
}, [countDownDate, countDown]);
return { value: getReturnValues(countDown) };
};
const getReturnValues = (countDown) => {
// calculate time left
const days = Math.floor(countDown / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(countDown % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60),
);
const minutes = Math.floor((countDown % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((countDown % (1000 * 60)) / 1000);
return { days, hours, minutes, seconds };
};