import React, { useState } from 'react'; const TradeConfirmationModal = ({ isOpen, onClose, analysis, symbol, timeframe, onConfirm, onAbort }) => { const [chatInput, setChatInput] = useState(''); const [chatHistory, setChatHistory] = useState([]); const [loading, setLoading] = useState(false); const [recommendation, setRecommendation] = useState(null); const [abortReason, setAbortReason] = useState(''); const [showAbortInput, setShowAbortInput] = useState(false); // Get AI recommendation when modal opens React.useEffect(() => { if (isOpen && analysis && !recommendation) { getAIRecommendation(); } }, [isOpen, analysis]); const getAIRecommendation = async () => { setLoading(true); try { const response = await fetch('/api/trade-confirmation', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'analyze', analysis, symbol, timeframe }) }); const data = await response.json(); if (data.success) { setRecommendation(data.recommendation); } } catch (error) { console.error('Error getting AI recommendation:', error); } setLoading(false); }; const handleChat = async () => { if (!chatInput.trim()) return; const userMessage = chatInput; setChatHistory(prev => [...prev, { role: 'user', content: userMessage }]); setChatInput(''); setLoading(true); try { const response = await fetch('/api/trade-confirmation', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'chat', userInput: userMessage }) }); const data = await response.json(); if (data.success) { setChatHistory(prev => [...prev, { role: 'assistant', content: data.response }]); } } catch (error) { console.error('Error in chat:', error); } setLoading(false); }; const handleConfirm = async () => { try { const response = await fetch('/api/trade-confirmation', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'confirm', analysis, symbol, timeframe }) }); const data = await response.json(); if (data.success) { onConfirm(data); onClose(); } } catch (error) { console.error('Error confirming trade:', error); } }; const handleAbort = async () => { if (!abortReason.trim()) { alert('Please provide a reason for aborting the trade'); return; } try { const response = await fetch('/api/trade-confirmation', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'abort', userInput: abortReason, analysis, symbol, timeframe }) }); const data = await response.json(); if (data.success) { onAbort(data, abortReason); onClose(); } } catch (error) { console.error('Error aborting trade:', error); } }; if (!isOpen) return null; return (
{/* Header */}

🎯 Trade Confirmation Required

{symbol} • {timeframe}m timeframe • Review analysis and confirm execution

{/* Left Column - Analysis & Recommendation */}
{/* Current Analysis */}

📊 Current Analysis

{analysis && (
Trend
{analysis.trend} ({Math.round((analysis.strength || 0) * 100)}%)
Confidence
{analysis.confidence}%
Support/Resistance
Support: {analysis.support?.join(', ')}
Resistance: {analysis.resistance?.join(', ')}
Technical Indicators
{analysis.indicators?.rsi &&
RSI: {analysis.indicators.rsi}
} {analysis.indicators?.macd &&
MACD: {analysis.indicators.macd}
} {analysis.indicators?.volume &&
Volume: {analysis.indicators.volume}
}
)}
{/* AI Recommendation */}

🤖 AI Recommendation

{loading && !recommendation ? (
🔄
Analyzing market conditions...
) : recommendation ? (
                    {recommendation}
                  
) : (
No recommendation available
)}
{/* Right Column - Chat & Actions */}
{/* Chat with AI */}

💬 Ask AI Assistant

{/* Chat History */}
{chatHistory.length === 0 ? (
Ask questions about this trade analysis...
) : ( chatHistory.map((message, index) => (
{message.role === 'user' ? 'You' : 'AI Assistant'}
{message.content}
)) )}
{/* Chat Input */}
setChatInput(e.target.value)} placeholder="Ask about the analysis..." className="flex-1 bg-gray-800 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm" onKeyPress={(e) => e.key === 'Enter' && handleChat()} />
{/* Decision Actions */}

⚡ Execute Decision

{!showAbortInput ? (
) : (