"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 AdvancedTradingPanel from './AdvancedTradingPanel' 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, netUsdValue: 0 }) useEffect(() => { async function fetchPositions() { try { setLoading(true) // Get Drift positions const driftRes = await fetch('/api/drift/positions') if (driftRes.ok) { const driftData = await driftRes.json() if (driftData.positions) { 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 })) // 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, netUsdValue: balanceData.netUsdValue || 0 })) } } catch (e) { console.warn('Could not fetch balance:', e) } } else { // No positions available - set empty state setPositions([]) setStats({ totalPnL: 0, dailyPnL: 0, winRate: 0, totalTrades: 0, accountValue: 0, netUsdValue: 0 }) } } else { // API failed - set empty state and show helpful message setError('Failed to connect to Drift. Your account may not be initialized. Visit app.drift.trade to create your account.') setPositions([]) setStats({ totalPnL: 0, dailyPnL: 0, winRate: 0, totalTrades: 0, accountValue: 0, netUsdValue: 0 }) } } catch (e) { setError('Error connecting to Drift') console.error('Error:', e) setPositions([]) setStats({ totalPnL: 0, dailyPnL: 0, winRate: 0, totalTrades: 0, accountValue: 0, netUsdValue: 0 }) } setLoading(false) } fetchPositions() }, []) return (
{/* Stats Cards */}

Net USD Value

${stats.netUsdValue.toFixed(2)}

💎

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) || '--'}
{pos.symbol || '--'}
{pos.side || '--'} {typeof pos.size === 'number' ? pos.size.toFixed(4) : '--'} ${typeof pos.entryPrice === 'number' ? pos.entryPrice.toFixed(2) : '--'} = 0 ? 'text-green-400' : 'text-red-400' }`}> {(pos.unrealizedPnl || 0) >= 0 ? '+' : ''}${typeof pos.unrealizedPnl === 'number' ? pos.unrealizedPnl.toFixed(2) : '0.00'}
)}
) }