Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
import { useState, useEffect, useCallback } from "react";
import { cn } from "@/lib/core";
interface CountdownTimerProps {
endDate: Date | string;
onExpire?: () => void;
variant?: "default" | "compact" | "large";
showLabels?: boolean;
className?: string;
}
interface TimeRemaining {
days: number;
hours: number;
minutes: number;
seconds: number;
isExpired: boolean;
}
interface TimeUnitProps {
value: number;
label: string;
variant: "default" | "compact" | "large";
showLabels: boolean;
}
function calculateTimeRemaining(endDate: Date): TimeRemaining {
const now = new Date().getTime();
const end = endDate.getTime();
const difference = end - now;
if (difference <= 0) {
return { days: 0, hours: 0, minutes: 0, seconds: 0, isExpired: true };
}
return {
days: Math.floor(difference / (1000 * 60 * 60 * 24)),
hours: Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
minutes: Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)),
seconds: Math.floor((difference % (1000 * 60)) / 1000),
isExpired: false};
}
function TimeUnit({ value, label, variant, showLabels }: TimeUnitProps) {
if (variant === "compact") {
return (
<span className="font-mono font-bold">
{String(value).padStart(2, "0")}
</span>
);
}
if (variant === "large") {
return (
<div className="flex flex-col items-center p-3 bg-gray-900 text-white rounded-lg min-w-[70px]">
<span className="text-3xl font-bold font-mono">
{String(value).padStart(2, "0")}
</span>
{showLabels && (
<span className="text-xs uppercase tracking-wider text-gray-400 mt-1">
{label}
</span>
)}
</div>
);
}
return (
<div className="flex flex-col items-center p-2 bg-gray-100 rounded min-w-[50px]">
<span className="text-xl font-bold font-mono">
{String(value).padStart(2, "0")}
</span>
{showLabels && (
<span className="text-xs text-gray-500 uppercase">{label}</span>
)}
</div>
);
}
export function CountdownTimer({
endDate,
onExpire,
variant = "default",
showLabels = true,
className}: CountdownTimerProps) {
const [timeRemaining, setTimeRemaining] = useState<TimeRemaining>(() =>
calculateTimeRemaining(new Date(endDate))
);
const handleExpire = useCallback(() => {
onExpire?.();
}, [onExpire]);
useEffect(() => {
const timer = setInterval(() => {
const remaining = calculateTimeRemaining(new Date(endDate));
setTimeRemaining(remaining);
if (remaining.isExpired) {
clearInterval(timer);
handleExpire();
}
}, 1000);
return () => clearInterval(timer);
}, [endDate, handleExpire]);
if (timeRemaining.isExpired) {
return (
<div className={cn("text-red-600 font-semibold", className)}>
Expired
</div>
);
}
if (variant === "compact") {
return (
<div className={cn("flex items-center gap-1 text-lg", className)}>
{timeRemaining.days > 0 && (
<>
<TimeUnit value={timeRemaining.days} label="d" variant={variant} showLabels={showLabels} />
<span className="text-gray-400">:</span>
</>
)}
<TimeUnit value={timeRemaining.hours} label="h" variant={variant} showLabels={showLabels} />
<span className="text-gray-400">:</span>
<TimeUnit value={timeRemaining.minutes} label="m" variant={variant} showLabels={showLabels} />
<span className="text-gray-400">:</span>
<TimeUnit value={timeRemaining.seconds} label="s" variant={variant} showLabels={showLabels} />
</div>
);
}
return (
<div className={cn("flex items-center gap-2", className)}>
{timeRemaining.days > 0 && (
<>
<TimeUnit value={timeRemaining.days} label="days" variant={variant} showLabels={showLabels} />
{variant === "large" && (
<span className="text-2xl font-bold text-gray-400">:</span>
)}
</>
)}
<TimeUnit value={timeRemaining.hours} label="hours" variant={variant} showLabels={showLabels} />
{variant === "large" && (
<span className="text-2xl font-bold text-gray-400">:</span>
)}
<TimeUnit value={timeRemaining.minutes} label="mins" variant={variant} showLabels={showLabels} />
{variant === "large" && (
<span className="text-2xl font-bold text-gray-400">:</span>
)}
<TimeUnit value={timeRemaining.seconds} label="secs" variant={variant} showLabels={showLabels} />
</div>
);
}
export default CountdownTimer;
|