File
Blob: src/client/hooks/useCountdown.ts
| 1 | import { useEffect, useState } from "react"; |
| 2 | import { formatCountdown } from "@/client/lib/time"; |
| 3 | |
| 4 | export function useCountdown(expiresAt: string | null) { |
| 5 | const [text, setText] = useState(() => (expiresAt ? formatCountdown(expiresAt) : "")); |
| 6 | |
| 7 | useEffect(() => { |
| 8 | if (!expiresAt) { |
| 9 | setText(""); |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | setText(formatCountdown(expiresAt)); |
| 14 | const id = window.setInterval(() => setText(formatCountdown(expiresAt)), 30_000); |
| 15 | return () => clearInterval(id); |
| 16 | }, [expiresAt]); |
| 17 | |
| 18 | return text; |
| 19 | } |