import { useState, useEffect } from 'react' import { TrendingUp, TrendingDown, RefreshCw } from 'lucide-react' interface TickerData { symbol: string lastPrice: string priceChangePercent: string highPrice: string lowPrice: string volume: string } const SYMBOLS = ['BTCUSDT', 'ETHUSDT', 'SOLUSDT'] export function MarketTicker() { const [tickers, setTickers] = useState>({}) const [loading, setLoading] = useState(true) const fetchTickers = async () => { try { const results = await Promise.all( SYMBOLS.map(async (symbol) => { const res = await fetch(`/api/agent/ticker?symbol=${symbol}`) const data = await res.json() return { symbol, ...data } }) ) const map: Record = {} results.forEach((r) => { if (r.lastPrice) map[r.symbol] = r }) setTickers(map) } catch { // ignore } finally { setLoading(false) } } useEffect(() => { fetchTickers() const interval = setInterval(fetchTickers, 15000) return () => clearInterval(interval) }, []) const formatPrice = (price: string) => { const n = parseFloat(price) if (n >= 1000) return n.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) if (n >= 1) return n.toFixed(2) return n.toFixed(4) } const formatVolume = (vol: string) => { const n = parseFloat(vol) if (n >= 1e9) return (n / 1e9).toFixed(1) + 'B' if (n >= 1e6) return (n / 1e6).toFixed(1) + 'M' if (n >= 1e3) return (n / 1e3).toFixed(1) + 'K' return n.toFixed(0) } if (loading) { return (
Loading market data...
) } return (
{SYMBOLS.map((sym) => { const t = tickers[sym] if (!t) return null const pct = parseFloat(t.priceChangePercent) const isUp = pct >= 0 const color = isUp ? '#00e5a0' : '#F6465D' const label = sym.replace('USDT', '') return (
{isUp ? ( ) : ( )}
{label}
Vol {formatVolume(t.volume)}
${formatPrice(t.lastPrice)}
{isUp ? '+' : ''}{pct.toFixed(2)}%
) })}
) }