'use client' import React, { useState } from 'react' interface CompactTradingPanelProps { symbol: string currentPrice: number onTrade?: (tradeData: any) => void } export default function CompactTradingPanel({ symbol = 'SOL', currentPrice = 166.21, onTrade }: CompactTradingPanelProps) { const [side, setSide] = useState<'LONG' | 'SHORT'>('LONG') const [orderType, setOrderType] = useState<'MARKET' | 'LIMIT'>('MARKET') const [amount, setAmount] = useState('') const [price, setPrice] = useState(currentPrice.toString()) const [leverage, setLeverage] = useState(1) const [stopLoss, setStopLoss] = useState('') const [takeProfit, setTakeProfit] = useState('') const [loading, setLoading] = useState(false) // Update price when currentPrice changes React.useEffect(() => { if (orderType === 'MARKET') { setPrice(currentPrice.toString()) } }, [currentPrice, orderType]) const calculateLiquidationPrice = () => { const entryPrice = parseFloat(price) || currentPrice const leverage_ratio = leverage || 1 if (side === 'LONG') { return entryPrice * (1 - 1 / leverage_ratio) } else { return entryPrice * (1 + 1 / leverage_ratio) } } const calculatePositionSize = () => { const amt = parseFloat(amount) || 0 const entryPrice = parseFloat(price) || currentPrice return amt * entryPrice } const handleTrade = async () => { if (!amount || parseFloat(amount) <= 0) { alert('Please enter a valid amount') return } setLoading(true) try { const tradeData = { symbol, side: side === 'LONG' ? 'BUY' : 'SELL', amount: parseFloat(amount), price: orderType === 'MARKET' ? currentPrice : parseFloat(price), type: orderType.toLowerCase(), leverage, stopLoss: stopLoss ? parseFloat(stopLoss) : undefined, takeProfit: takeProfit ? parseFloat(takeProfit) : undefined, tradingMode: 'PERP' } onTrade?.(tradeData) } catch (error) { console.error('Trade execution failed:', error) } finally { setLoading(false) } } const leverageOptions = [1, 2, 3, 5, 10, 20, 25, 50, 100] return (