'use client' import React, { useState, useEffect } from 'react' export default function PendingOrdersPanel() { const [pendingOrders, setPendingOrders] = useState([]) const [loading, setLoading] = useState(true) useEffect(() => { fetchPendingOrders() // Refresh orders every 5 seconds to check for fills const interval = setInterval(fetchPendingOrders, 5000) return () => clearInterval(interval) }, []) const fetchPendingOrders = async () => { try { const response = await fetch('/api/trading/orders') const data = await response.json() if (data.success) { setPendingOrders(data.orders || []) // Check if any orders are ready to fill const ordersToFill = data.orders.filter(order => { if (order.status !== 'PENDING' || !order.currentPrice) return false return ( (order.side === 'BUY' && order.currentPrice <= order.limitPrice) || (order.side === 'SELL' && order.currentPrice >= order.limitPrice) ) }) // Auto-fill orders that have reached their target price for (const order of ordersToFill) { try { await fetch('/api/trading/orders', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'fill', orderId: order.id, fillPrice: order.currentPrice }) }) console.log(`🎯 Auto-filled limit order: ${order.id}`) } catch (error) { console.error('Failed to auto-fill order:', error) } } } } catch (error) { console.error('Failed to fetch pending orders:', error) } finally { setLoading(false) } } const cancelOrder = async (orderId) => { try { const response = await fetch('/api/trading/orders', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'cancel', orderId: orderId }) }) if (response.ok) { fetchPendingOrders() // Refresh orders } } catch (error) { console.error('Failed to cancel order:', error) } } const getOrderStatus = (order) => { if (!order.currentPrice) return 'Monitoring...' const distance = Math.abs(order.currentPrice - order.limitPrice) const percentageAway = (distance / order.limitPrice) * 100 if (order.side === 'BUY') { if (order.currentPrice <= order.limitPrice) { return { text: 'Ready to Fill!', color: 'text-green-400', urgent: true } } else { return { text: `$${(order.currentPrice - order.limitPrice).toFixed(4)} above target`, color: 'text-yellow-400', urgent: false } } } else { if (order.currentPrice >= order.limitPrice) { return { text: 'Ready to Fill!', color: 'text-green-400', urgent: true } } else { return { text: `$${(order.limitPrice - order.currentPrice).toFixed(4)} below target`, color: 'text-yellow-400', urgent: false } } } } const formatCurrency = (amount) => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 4 }).format(amount) } if (loading) { return (

Pending Orders

Loading orders...
) } return (

Pending Orders

{pendingOrders.length} order{pendingOrders.length !== 1 ? 's' : ''}
{pendingOrders.length === 0 ? (
📋 No pending orders
Limit orders will appear here when created
) : (
{pendingOrders.map((order) => { const status = getOrderStatus(order) return (
{order.symbol} {order.side} LIMIT {order.tradingMode === 'PERP' && ( PERP )}
{status.urgent && ( 🎯 READY )}
Amount
{order.amount}
Limit Price
{formatCurrency(order.limitPrice)}
Current Price
{order.currentPrice ? formatCurrency(order.currentPrice) : 'Loading...'}
Status
{status.text}
{/* Stop Loss / Take Profit */} {(order.stopLoss || order.takeProfit) && (
{order.stopLoss && (
🛑 SL: {formatCurrency(order.stopLoss)}
)} {order.takeProfit && (
🎯 TP: {formatCurrency(order.takeProfit)}
)}
)} {/* Order Info */}
Created: {new Date(order.timestamp).toLocaleString()} {order.expiresAt && ( • Expires: {new Date(order.expiresAt).toLocaleString()} )}
) })}
)}
) }