import { useEffect, useState } from "react"; const RATE_TTL_MS = 5 * 60 * 1000; const RATE_URL = "https://api.coingecko.com/api/v3/simple/price?ids=wownero&vs_currencies=usd"; let cachedRate: number | null = null; let cachedAt: number | null = null; let inflight: Promise | null = null; const fetchRate = async (): Promise => { try { const response = await fetch(RATE_URL, { cache: "no-store" }); if (!response.ok) { return null; } const data = (await response.json()) as { wownero?: { usd?: number }; }; const rate = data?.wownero?.usd; if (typeof rate !== "number" || !Number.isFinite(rate)) { return null; } cachedRate = rate; cachedAt = Date.now(); return rate; } catch { return null; } }; export const useXmrUsdRate = () => { const [rate, setRate] = useState(cachedRate); const [updatedAt, setUpdatedAt] = useState(cachedAt); useEffect(() => { let active = true; const now = Date.now(); const hasFreshRate = cachedRate !== null && cachedAt !== null && now - cachedAt < RATE_TTL_MS; if (hasFreshRate) { setRate(cachedRate); setUpdatedAt(cachedAt); return; } const request = inflight ?? (inflight = fetchRate().finally(() => { inflight = null; })); request.then((nextRate) => { if (!active) { return; } if (nextRate !== null) { setRate(nextRate); setUpdatedAt(cachedAt); } }); return () => { active = false; }; }, []); return { rate, updatedAt, source: "coingecko" as const }; };