"use client" import React, { useState, useEffect } from 'react' interface TradeModalProps { isOpen: boolean onClose: () => void tradeData: { symbol: string timeframe: string entry: string tp: string sl: string } | null onExecute: (data: any) => void } interface FormData { entry: string tp1: string tp2: string sl: string positionValue: string leverage: number tradingCoin: string tp1Percentage: number tp2Percentage: number } const supportedCoins = [ { symbol: 'SOL', name: 'Solana', icon: '◎', price: 159.5 }, { symbol: 'USDC', name: 'USD Coin', icon: '$', price: 1.0 } ] export default function TradeModal({ isOpen, onClose, tradeData, onExecute }: TradeModalProps) { console.log('🚀 TradeModal loaded with enhanced features - Version 2.0') const [loading, setLoading] = useState(false) const [walletBalance, setWalletBalance] = useState(null) const [formData, setFormData] = useState({ entry: tradeData?.entry || '', tp1: tradeData?.tp || '', tp2: '', sl: tradeData?.sl || '', positionValue: '1000', // Position size in chosen coin leverage: 3, tradingCoin: 'SOL', tp1Percentage: 50, // % of position to close at TP1 tp2Percentage: 50 // % of position to close at TP2 }) // Fetch wallet balance when modal opens useEffect(() => { if (isOpen) { fetchWalletBalance() } }, [isOpen]) const fetchWalletBalance = async () => { try { const response = await fetch('/api/wallet/balance') const data = await response.json() if (data.success) { setWalletBalance(data.balance) } } catch (error) { console.error('Failed to fetch wallet balance:', error) } } // Helper function to safely format numbers const formatNumber = (value: number, decimals: number = 2): string => { if (isNaN(value) || !isFinite(value)) return '0.00' return value.toFixed(decimals) } // Calculate derived values with proper error handling const currentCoin = supportedCoins.find(coin => coin.symbol === formData.tradingCoin) const coinPrice = currentCoin?.price || 159.5 // Default to SOL price // Safe number parsing - position size in chosen coin const positionSize = parseFloat(formData.positionValue) || 0 const positionValueUSD = positionSize * coinPrice const leverageNum = formData.leverage || 1 const entryPrice = parseFloat(formData.entry) || 0 const tp1Price = parseFloat(formData.tp1) || 0 const tp2Price = parseFloat(formData.tp2) || 0 const slPrice = parseFloat(formData.sl) || 0 // Calculations with fallbacks const leveragedValue = positionValueUSD * leverageNum // P&L calculations with proper validation const profitTP1 = (entryPrice > 0 && tp1Price > 0 && leveragedValue > 0) ? ((tp1Price - entryPrice) / entryPrice) * leveragedValue * (formData.tp1Percentage / 100) : 0 const profitTP2 = (entryPrice > 0 && tp2Price > 0 && leveragedValue > 0) ? ((tp2Price - entryPrice) / entryPrice) * leveragedValue * (formData.tp2Percentage / 100) : 0 const lossAtSL = (entryPrice > 0 && slPrice > 0 && leveragedValue > 0) ? ((slPrice - entryPrice) / entryPrice) * leveragedValue : 0 useEffect(() => { if (tradeData) { setFormData((prev: FormData) => ({ ...prev, entry: tradeData.entry || '', tp1: tradeData.tp || '', sl: tradeData.sl || '' })) } }, [tradeData]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) try { await onExecute({ symbol: tradeData?.symbol, timeframe: tradeData?.timeframe, ...formData, positionSize, leveragedValue }) } catch (error) { console.error('Trade execution failed:', error) } finally { setLoading(false) } } if (!isOpen || !tradeData) return null return (

💰 Execute Trade

{/* Trade Details Section */}

📊 Trade Setup

{tradeData?.symbol}
{tradeData?.timeframe}
{/* Trading Coin Selection - Enhanced Visual Cards */}
{supportedCoins.map(coin => ( ))}
{/* Enhanced Position Size Section */}

💵 Position Size ({formData.tradingCoin})

{/* Position Size Input */}
setFormData(prev => ({...prev, positionValue: e.target.value}))} className="w-full bg-gray-800 border border-gray-600 rounded-lg px-3 py-2 text-white focus:border-blue-500 focus:outline-none" placeholder="1,000" />
≈ ${formatNumber(positionValueUSD)} USD
{/* Percentage Buttons */}
{[25, 50, 75, 100].map(percentage => ( ))}
{/* Leverage Section */}
setFormData(prev => ({...prev, leverage: parseInt(e.target.value)}))} className="w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer slider" />
1x 5x 10x
Leveraged Value: ${formatNumber(leveragedValue)}
{/* Enhanced Price Levels */}
setFormData(prev => ({...prev, entry: e.target.value}))} className="w-full bg-gray-800 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm focus:border-blue-500 focus:outline-none mb-4" placeholder="159.5" />
{/* Take Profit 1 with Profit Display */}
setFormData(prev => ({...prev, tp1: e.target.value}))} className="w-full bg-gray-800 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm focus:border-green-500 focus:outline-none" placeholder="160.5" /> {/* Profit Percentage Slider for TP1 */}
Profit % Profit: $1.50
setFormData(prev => ({...prev, tp1Percentage: parseInt(e.target.value)}))} className="w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer" style={{ background: `linear-gradient(to right, #10b981 0%, #10b981 ${formData.tp1Percentage}%, #374151 ${formData.tp1Percentage}%, #374151 100%)` }} />
10% 90%
{/* Take Profit 2 */}
setFormData(prev => ({...prev, tp2: e.target.value}))} className="w-full bg-gray-800 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm focus:border-green-500 focus:outline-none" placeholder="162" />
{/* Stop Loss */}
setFormData(prev => ({...prev, sl: e.target.value}))} className="w-full bg-gray-800 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm focus:border-red-500 focus:outline-none" placeholder="158.5" />
{/* Submit Buttons */}
) }