added days, fixed >99 not displaying
This commit is contained in:
@@ -8,27 +8,37 @@ interface PenaltyCountdownProps {
|
|||||||
function getTimeLeft(until: string | Date) {
|
function getTimeLeft(until: string | Date) {
|
||||||
const untilDate = new Date(until).getTime();
|
const untilDate = new Date(until).getTime();
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
let diff = Math.max(0, untilDate - now);
|
const diff = Math.max(0, untilDate - now);
|
||||||
const hours = Math.floor(diff / (1000 * 60 * 60));
|
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
|
||||||
diff -= hours * 1000 * 60 * 60;
|
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
|
||||||
const minutes = Math.floor(diff / (1000 * 60));
|
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
|
||||||
diff -= minutes * 1000 * 60;
|
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
|
||||||
const seconds = Math.floor(diff / 1000);
|
return { days, hours, minutes, seconds };
|
||||||
return { hours, minutes, seconds };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PenaltyCountdown: React.FC<PenaltyCountdownProps> = ({ until }) => {
|
export const PenaltyCountdown: React.FC<PenaltyCountdownProps> = ({ until }) => {
|
||||||
const [timeLeft, setTimeLeft] = useState(() => getTimeLeft(until));
|
const [timeLeft, setTimeLeft] = useState(() => getTimeLeft(until));
|
||||||
|
const [mounted, setMounted] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
setMounted(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mounted) return;
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
setTimeLeft(getTimeLeft(until));
|
setTimeLeft(getTimeLeft(until));
|
||||||
}, 1000);
|
}, 1000);
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, [until]);
|
}, [until, mounted]);
|
||||||
|
|
||||||
|
console.log("PenaltyCountdown until:", until, "timeLeft:", timeLeft);
|
||||||
|
|
||||||
|
if (!mounted) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span className="countdown text-3xl">
|
<span className="countdown text-3xl">
|
||||||
|
{timeLeft.days > 0 && timeLeft.days + "d"}
|
||||||
<span style={{ "--value": timeLeft.hours } as React.CSSProperties} aria-live="polite">
|
<span style={{ "--value": timeLeft.hours } as React.CSSProperties} aria-live="polite">
|
||||||
{timeLeft.hours}
|
{timeLeft.hours}
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user