()
| 39 | |
| 40 | /** Hook for page.tsx to get alert data + available categories for the dropdown */ |
| 41 | export function useResolutionData() { |
| 42 | const [alerts, setAlerts] = useState<ResolutionAlertInput[]>([]); |
| 43 | const retryRef = useRef(0); |
| 44 | const fetchAlertsRef = useRef<(() => Promise<void>) | undefined>(undefined); |
| 45 | |
| 46 | const fetchAlerts = useCallback(async () => { |
| 47 | try { |
| 48 | const res = await fetch("/api/resolution-alerts"); |
| 49 | if (!res.ok) return; |
| 50 | const data = await res.json(); |
| 51 | setAlerts( |
| 52 | (data.alerts || []).map((a: Record<string, unknown>) => ({ |
| 53 | id: a.id, |
| 54 | eventId: a.event_id, |
| 55 | title: a.title, |
| 56 | url: a.url, |
| 57 | source: a.source, |
| 58 | detectedAt: a.detected_at, |
| 59 | marketTitle: a.market_title || "", |
| 60 | slug: a.slug || "", |
| 61 | prob: a.prob ?? null, |
| 62 | endDate: a.end_date ?? null, |
| 63 | category: a.category ?? null, |
| 64 | })) |
| 65 | ); |
| 66 | retryRef.current = 0; |
| 67 | } catch { |
| 68 | if (retryRef.current < 2) { |
| 69 | retryRef.current++; |
| 70 | setTimeout(() => fetchAlertsRef.current?.(), 3000); |
| 71 | } |
| 72 | } |
| 73 | }, []); |
| 74 | |
| 75 | useEffect(() => { |
| 76 | fetchAlertsRef.current = fetchAlerts; |
| 77 | queueMicrotask(() => { |
| 78 | void fetchAlerts(); |
| 79 | }); |
| 80 | const iv = setInterval(fetchAlerts, 120_000); |
| 81 | return () => clearInterval(iv); |
| 82 | }, [fetchAlerts]); |
| 83 | |
| 84 | const enriched = useMemo( |
| 85 | () => |
| 86 | alerts.map((a) => ({ ...a, strength: strengthOf(a) })).sort((a, b) => { |
| 87 | const sr = { strong: 3, moderate: 2, weak: 1 }; |
| 88 | const diff = sr[b.strength] - sr[a.strength]; |
| 89 | if (diff !== 0) return diff; |
| 90 | return new Date(b.detectedAt).getTime() - new Date(a.detectedAt).getTime(); |
| 91 | }), |
| 92 | [alerts] |
| 93 | ); |
| 94 | |
| 95 | const categories = useMemo(() => { |
| 96 | const cats = new Set<string>(); |
| 97 | for (const a of enriched) if (a.category) cats.add(a.category); |
| 98 | return Array.from(cats).sort(); |
no test coverage detected