'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 (
{/* Header */}

{symbol}/USDC

${currentPrice.toFixed(2)}
{/* Long/Short Toggle */}
{/* Order Type */}
{/* Leverage Slider */}
{leverage}x
setLeverage(parseInt(e.target.value))} className="w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer slider" />
{leverageOptions.map(lev => ( {lev}x ))}
{/* Amount Input */}
setAmount(e.target.value)} placeholder="0.00" className="w-full p-3 bg-gray-800 border border-gray-700 rounded-lg text-white placeholder-gray-400 focus:border-blue-500 focus:outline-none" />
{/* Price Input (for limit orders) */} {orderType === 'LIMIT' && (
setPrice(e.target.value)} placeholder="0.00" className="w-full p-3 bg-gray-800 border border-gray-700 rounded-lg text-white placeholder-gray-400 focus:border-blue-500 focus:outline-none" />
)} {/* Stop Loss & Take Profit */}
setStopLoss(e.target.value)} placeholder="Optional" className="w-full p-2 bg-gray-800 border border-gray-700 rounded text-white placeholder-gray-400 text-sm focus:border-red-500 focus:outline-none" />
setTakeProfit(e.target.value)} placeholder="Optional" className="w-full p-2 bg-gray-800 border border-gray-700 rounded text-white placeholder-gray-400 text-sm focus:border-green-500 focus:outline-none" />
{/* Position Info */}
Position Size: ${calculatePositionSize().toFixed(2)}
Liquidation Price: ${calculateLiquidationPrice().toFixed(2)}
Available: $5,000.00
{/* Trade Button */} {/* Quick Actions */}
) }