import React, { useEffect, useState } from 'react'; import axios from 'axios'; import { TrendingUp, TrendingDown } from 'lucide-react'; const PAIRS = ['EUR/USD', 'GBP/USD', 'USD/JPY', 'XAU/USD', 'BTC/USD']; export function LiveMarketTicker() { const [quotes, setQuotes] = useState>({}); const [loading, setLoading] = useState(true); useEffect(() => { const fetchMarketData = async () => { try { const promises = PAIRS.map(symbol => axios.get(`/api/twelve/quote?symbol=${symbol}`).catch(() => null) ); const results = await Promise.all(promises); const newQuotes: Record = {}; results.forEach((res, index) => { if (res && res.data && !res.data.error) { newQuotes[PAIRS[index]] = res.data; } }); setQuotes(newQuotes); setLoading(false); } catch (e) { console.error("Failed to fetch market data", e); setLoading(false); } }; fetchMarketData(); const interval = setInterval(fetchMarketData, 60000); // 1 min update limit for free api generally return () => clearInterval(interval); }, []); if (loading) { return (
Conectando ao mercado em tempo real...
); } return (
{PAIRS.map(pair => { const q = quotes[pair]; if (!q) return null; const change = parseFloat(q.change); const isUp = change >= 0; return (
{pair} {parseFloat(q.close).toFixed(4)} {isUp ? : } {change > 0 ? '+' : ''}{change} ({q.percent_change}%)
); })}
); }