"use client" import React, { useEffect, useState } from 'react' import AutoTradingPanel from './AutoTradingPanel' import TradingHistory from './TradingHistory' import DeveloperSettings from './DeveloperSettings' import AIAnalysisPanel from './AIAnalysisPanel' import SessionStatus from './SessionStatus' import DriftAccountStatus from './DriftAccountStatus' import DriftTradingPanel from './DriftTradingPanel' export default function Dashboard() { const [positions, setPositions] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [stats, setStats] = useState({ totalPnL: 0, dailyPnL: 0, winRate: 0, totalTrades: 0, accountValue: 0 }) useEffect(() => { async function fetchPositions() { try { setLoading(true) // Try to get Drift positions first const driftRes = await fetch('/api/drift/positions') if (driftRes.ok) { const driftData = await driftRes.json() if (driftData.positions && driftData.positions.length > 0) { setPositions(driftData.positions) // Calculate stats from Drift positions const totalPnL = driftData.positions.reduce((sum: number, pos: any) => sum + (pos.unrealizedPnl || 0), 0) setStats(prev => ({ ...prev, totalPnL, dailyPnL: totalPnL * 0.1, // Approximate daily as 10% of total for demo totalTrades: driftData.positions.length })) // Try to get account balance for account value try { const balanceRes = await fetch('/api/drift/balance') if (balanceRes.ok) { const balanceData = await balanceRes.json() setStats(prev => ({ ...prev, accountValue: balanceData.accountValue || 0 })) } } catch (e) { console.warn('Could not fetch balance:', e) } } else { // Fallback to legacy trading API const res = await fetch('/api/trading') if (res.ok) { const data = await res.json() setPositions(data.positions || []) // Calculate some mock stats for demo setStats({ totalPnL: 1247.50, dailyPnL: 67.25, winRate: 73.2, totalTrades: 156, accountValue: 10000 }) } else { setError('Failed to load positions') } } } else { // Fallback to legacy trading API const res = await fetch('/api/trading') if (res.ok) { const data = await res.json() setPositions(data.positions || []) // Calculate some mock stats for demo setStats({ totalPnL: 1247.50, dailyPnL: 67.25, winRate: 73.2, totalTrades: 156, accountValue: 10000 }) } else { setError('Failed to load positions') } } } catch (e) { setError('Error loading positions') console.error('Error:', e) } setLoading(false) } fetchPositions() }, []) return (
{/* Stats Cards */}

Account Value

${stats.accountValue.toFixed(2)}

🏦

Total P&L

= 0 ? 'text-green-400' : 'text-red-400'}`}> {stats.totalPnL >= 0 ? '+' : ''}${stats.totalPnL.toFixed(2)}

📈

Daily P&L

= 0 ? 'text-green-400' : 'text-red-400'}`}> {stats.dailyPnL >= 0 ? '+' : ''}${stats.dailyPnL.toFixed(2)}

💰

Win Rate

{stats.winRate}%

🎯

Total Trades

{stats.totalTrades}

🔄
{/* Main Content Grid */}
{/* Left Column - Controls & Account Status */}
{/* Right Column - Analysis & Positions */}
{/* Open Positions */}

Open Positions

{loading ? (
Loading positions...
) : error ? (
⚠️

{error}

Please check your connection and try again

) : positions.length === 0 ? (
📊

No open positions

Start trading to see your positions here

) : (
{positions.map((pos, i) => ( ))}
Asset Side Size Entry PnL
{pos.symbol?.slice(0, 2) || 'BT'}
{pos.symbol || 'BTC/USD'}
{pos.side || 'Long'} {typeof pos.size === 'number' ? pos.size.toFixed(4) : (pos.size || '0.1 BTC')} ${typeof pos.entryPrice === 'number' ? pos.entryPrice.toFixed(2) : (pos.entryPrice || '45,230.00')} = 0 ? 'text-green-400' : 'text-red-400' }`}> {(pos.unrealizedPnl || 125.50) >= 0 ? '+' : ''}${typeof pos.unrealizedPnl === 'number' ? pos.unrealizedPnl.toFixed(2) : '125.50'}
)}
) }