import { useState, useEffect, useRef } from 'react'; export interface ArcadeCountdown { /** Total milliseconds remaining */ totalMs: number; /** Days remaining */ days: number; /** Hours remaining (0-23) */ hours: number; /** Minutes remaining (0-59) */ minutes: number; /** Seconds remaining (0-59) */ seconds: number; /** Formatted string e.g. "2d 5h 30m" or "5m 12s" */ formatted: string; /** True when countdown has reached zero */ isExpired: boolean; } function formatCountdown(ms: number): string { if (ms <= 0) return 'Ended'; const s = Math.floor(ms / 1000); const m = Math.floor(s / 60); const h = Math.floor(m / 60); const d = Math.floor(h / 24); if (d > 0) return `${d}d ${h % 24}h ${m % 60}m`; if (h > 0) return `${h}h ${m % 60}m ${s % 60}s`; if (m > 0) return `${m}m ${s % 60}s`; return `${s}s`; } /** * Countdown hook for arcade pool resolution time. * Pass the `next_resolution` ISO string from the pool object. * Ticks every second automatically. */ export function useArcadeCountdown(nextResolution: string | null | undefined): ArcadeCountdown { const [now, setNow] = useState(Date.now()); const intervalRef = useRef | null>(null); useEffect(() => { if (!nextResolution) return; intervalRef.current = setInterval(() => setNow(Date.now()), 1000); return () => { if (intervalRef.current) clearInterval(intervalRef.current); }; }, [nextResolution]); if (!nextResolution) { return { totalMs: 0, days: 0, hours: 0, minutes: 0, seconds: 0, formatted: '--', isExpired: false }; } const target = new Date(nextResolution).getTime(); const totalMs = Math.max(0, target - now); const totalSec = Math.floor(totalMs / 1000); const days = Math.floor(totalSec / 86400); const hours = Math.floor((totalSec % 86400) / 3600); const minutes = Math.floor((totalSec % 3600) / 60); const seconds = totalSec % 60; return { totalMs, days, hours, minutes, seconds, formatted: formatCountdown(totalMs), isExpired: totalMs <= 0, }; }