import React, { useState, useEffect } from 'react'; import { Clock, AlertCircle } from 'lucide-react'; import { differenceInSeconds } from 'date-fns'; import { useTranslation } from 'react-i18next'; interface CountdownTimerProps { expiresAt: string; className?: string; } export const CountdownTimer: React.FC = ({ expiresAt, className = '' }) => { const { t } = useTranslation(); const [timeLeft, setTimeLeft] = useState<{ hours: number; minutes: number; seconds: number; isExpired: boolean; }>({ hours: 0, minutes: 0, seconds: 0, isExpired: false }); useEffect(() => { const calculateTimeLeft = () => { const expirationDate = new Date(expiresAt); const now = new Date(); if (expirationDate <= now) { setTimeLeft({ hours: 0, minutes: 0, seconds: 0, isExpired: true }); return; } const totalSeconds = differenceInSeconds(expirationDate, now); const hours = Math.floor(totalSeconds / 3600); const minutes = Math.floor((totalSeconds % 3600) / 60); const seconds = totalSeconds % 60; setTimeLeft({ hours, minutes, seconds, isExpired: false }); }; calculateTimeLeft(); const interval = setInterval(calculateTimeLeft, 1000); return () => clearInterval(interval); }, [expiresAt]); if (timeLeft.isExpired) { return (
{t('gallery.expired')}
); } // Only show countdown if less than 24 hours remain if (timeLeft.hours >= 24) { return null; } return (
{String(timeLeft.hours).padStart(2, '0')}
:
{String(timeLeft.minutes).padStart(2, '0')}
:
{String(timeLeft.seconds).padStart(2, '0')}
{t('gallery.remaining')}
); };