'use client' import React, { useState, useEffect } from 'react' export default function TradesHistoryPanel() { const [trades, setTrades] = useState([]) const [loading, setLoading] = useState(true) useEffect(() => { fetchTrades() // Refresh trades every 15 seconds const interval = setInterval(fetchTrades, 15000) return () => clearInterval(interval) }, []) const fetchTrades = async () => { try { const response = await fetch('/api/trading/history') const data = await response.json() if (data.success) { setTrades(data.trades || []) } } catch (error) { console.error('Failed to fetch trades:', error) } finally { setLoading(false) } } const clearHistory = async () => { try { const response = await fetch('/api/trading/history', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'clear' }) }) if (response.ok) { setTrades([]) } } catch (error) { console.error('Failed to clear history:', error) } } const getTradeTypeInfo = (trade) => { switch (trade.type) { case 'SPOT_SWAP': return { label: 'SPOT SWAP', color: 'bg-purple-600', icon: '⚡' } case 'market': return { label: 'MARKET', color: 'bg-blue-600', icon: '📈' } case 'limit': return { label: 'LIMIT', color: 'bg-orange-600', icon: '🎯' } case 'stop': return { label: 'STOP', color: 'bg-red-600', icon: '🛑' } default: return { label: trade.type?.toUpperCase() || 'TRADE', color: 'bg-gray-600', icon: '💱' } } } const getSideColor = (side) => { return side === 'BUY' ? 'text-green-400' : 'text-red-400' } const getPnlColor = (pnl) => { if (pnl === null || pnl === 0) return 'text-gray-400' return pnl > 0 ? 'text-green-400' : 'text-red-400' } const formatCurrency = (amount) => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 4 }).format(amount) } const formatTime = (timestamp) => { return new Date(timestamp).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit' }) } if (loading) { return (