"use client"; import { useState, useEffect, useMemo } from "react"; import { calculateCountdown } from "@timetoleave/core"; interface ClockResult { countdown: ReturnType; status: "upcoming" | "now" | "past"; } export function useClock(targetDate: Date, departureTime?: Date | null): ClockResult { const [now, setNow] = useState(new Date()); useEffect(() => { const interval = setInterval(() => { setNow(new Date()); }, 10_000); return () => clearInterval(interval); }, []); const effectiveTarget = departureTime ?? targetDate; return useMemo(() => { const countdown = calculateCountdown(effectiveTarget); const diffMs = effectiveTarget.getTime() - now.getTime(); let status: "upcoming" | "now" | "past"; if (diffMs <= 0) { status = "now"; } else if (diffMs <= 10 * 60 * 1000) { status = "now"; } else { status = "upcoming"; } return { countdown, status }; }, [effectiveTarget, now]); }