mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
38 lines
1.0 KiB
TypeScript
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 };
|
|
};
|