'use client' import React, { useState, useEffect } from 'react' export default function PositionsPanel() { const [positions, setPositions] = useState([]) const [loading, setLoading] = useState(true) const [totalPnl, setTotalPnl] = useState(0) const [totalValue, setTotalValue] = useState(0) useEffect(() => { fetchPositions() // Refresh positions every 10 seconds const interval = setInterval(fetchPositions, 10000) return () => clearInterval(interval) }, []) const fetchPositions = async () => { try { const response = await fetch('/api/trading/positions') const data = await response.json() if (data.success) { setPositions(data.positions || []) setTotalPnl(data.totalPnl || 0) setTotalValue(data.totalValue || 0) } } catch (error) { console.error('Failed to fetch positions:', error) } finally { setLoading(false) } } const closePosition = async (positionId, currentPrice) => { try { const response = await fetch('/api/trading/positions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'close', positionId: positionId, exitPrice: currentPrice }) }) if (response.ok) { fetchPositions() // Refresh positions } } catch (error) { console.error('Failed to close position:', error) } } const getPnlColor = (pnl) => { if (pnl > 0) return 'text-green-400' if (pnl < 0) return 'text-red-400' return 'text-gray-400' } const formatCurrency = (amount) => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 4 }).format(amount) } if (loading) { return (