import { useState, useEffect, useCallback } from 'react';
import type { Alert } from '../types/market';
export function useAlerts() {
const [alerts, setAlerts] = useState<Alert[]>([]);
const [loading, setLoading] = useState(false);
const fetchAlerts = useCallback(() => {
setLoading(true);
fetch('/api/alerts')
.then(r => r.json())
.then(d => {
if (d.success) setAlerts(d.data);
})
.catch(() => {})
.finally(() => setLoading(false));
}, []);
useEffect(() => {
const t = setTimeout(fetchAlerts, 0);
const interval = setInterval(fetchAlerts, 60_000);
return () => { clearTimeout(t); clearInterval(interval); };
}, [fetchAlerts]);
const createAlert = useCallback(async (
marketId: string, side: string, direction: string, threshold: number
): Promise<boolean> => {
try {
const res = await fetch('/api/alerts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ market_id: marketId, side, direction, threshold }),
});
const d = await res.json();
if (d.success) {
fetchAlerts();
return true;
}
return false;
} catch {
return false;
}
}, [fetchAlerts]);
const deleteAlert = useCallback(async (id: number): Promise<boolean> => {
try {
const res = await fetch(`/api/alerts/${id}`, { method: 'DELETE' });
const d = await res.json();
if (d.success) {
setAlerts(prev => prev.filter(a => a.id !== id));
return true;
}
return false;
} catch {
return false;
}
}, []);
const triggeredCount = alerts.filter(a => a.triggered).length;
return { alerts, loading, triggeredCount, createAlert, deleteAlert, refresh: fetchAlerts };
}
📜 Git History
6c47fa4chore: local Polikopi project home + Phase 1 redesign artifacts12 days ago
Show last diff
Loading...