"use client" import React, { useState } from 'react' const layouts = (process.env.NEXT_PUBLIC_TRADINGVIEW_LAYOUTS || 'ai,Diy module').split(',').map(l => l.trim()) const timeframes = [ { label: '1m', value: '1' }, { label: '5m', value: '5' }, { label: '15m', value: '15' }, { label: '1h', value: '60' }, { label: '4h', value: '240' }, { label: '1d', value: 'D' }, { label: '1w', value: 'W' }, { label: '1M', value: 'M' }, ] const popularCoins = [ { name: 'Bitcoin', symbol: 'BTCUSD', icon: '₿', color: 'from-orange-400 to-orange-600' }, { name: 'Ethereum', symbol: 'ETHUSD', icon: 'Ξ', color: 'from-blue-400 to-blue-600' }, { name: 'Solana', symbol: 'SOLUSD', icon: '◎', color: 'from-purple-400 to-purple-600' }, { name: 'Sui', symbol: 'SUIUSD', icon: '🔷', color: 'from-cyan-400 to-cyan-600' }, { name: 'Avalanche', symbol: 'AVAXUSD', icon: '🔺', color: 'from-red-400 to-red-600' }, { name: 'Cardano', symbol: 'ADAUSD', icon: '♠', color: 'from-indigo-400 to-indigo-600' }, { name: 'Polygon', symbol: 'MATICUSD', icon: '🔷', color: 'from-violet-400 to-violet-600' }, { name: 'Chainlink', symbol: 'LINKUSD', icon: '🔗', color: 'from-blue-400 to-blue-600' }, ] export default function AIAnalysisPanel() { const [symbol, setSymbol] = useState('BTCUSD') const [selectedLayouts, setSelectedLayouts] = useState([layouts[0]]) const [timeframe, setTimeframe] = useState('60') const [loading, setLoading] = useState(false) const [result, setResult] = useState(null) const [error, setError] = useState(null) // Helper function to safely render any value const safeRender = (value: any): string => { if (typeof value === 'string') return value if (typeof value === 'number') return value.toString() if (Array.isArray(value)) return value.join(', ') if (typeof value === 'object' && value !== null) { return JSON.stringify(value) } return String(value) } const toggleLayout = (layout: string) => { setSelectedLayouts(prev => prev.includes(layout) ? prev.filter(l => l !== layout) : [...prev, layout] ) } const quickAnalyze = async (coinSymbol: string) => { setSymbol(coinSymbol) setSelectedLayouts([layouts[0]]) // Use first layout setLoading(true) setError(null) setResult(null) try { const res = await fetch('/api/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ symbol: coinSymbol, layouts: [layouts[0]], timeframe }) }) const data = await res.json() if (!res.ok) throw new Error(data.error || 'Unknown error') setResult(data) } catch (e: any) { setError(e.message) } setLoading(false) } async function handleAnalyze() { setLoading(true) setError(null) setResult(null) if (selectedLayouts.length === 0) { setError('Please select at least one layout') setLoading(false) return } try { const res = await fetch('/api/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ symbol, layouts: selectedLayouts, timeframe }) }) const data = await res.json() if (!res.ok) throw new Error(data.error || 'Unknown error') setResult(data) } catch (e: any) { setError(e.message) } setLoading(false) } return (

🤖 AI Chart Analysis

AI Powered
{/* Quick Coin Selection */}

Quick Analysis

Click any coin for instant analysis
{popularCoins.map(coin => ( ))}
{/* Advanced Controls */}

Advanced Analysis

{/* Symbol and Timeframe */}
setSymbol(e.target.value.toUpperCase())} placeholder="e.g., BTCUSD, ETHUSD" />
{/* Layout Selection */}
{layouts.map(layout => ( ))}
{selectedLayouts.length > 0 && (
Selected layouts: {selectedLayouts.join(', ')}
)}
{/* Analyze Button */}
{/* Results Section */} {error && (
⚠️

Analysis Error

{error}

)} {loading && (

AI Processing

Analyzing {symbol} on {timeframe} timeframe...

)} {result && (

Analysis Complete

{result.layoutsAnalyzed && (
Layouts: {result.layoutsAnalyzed.join(', ')}
)}
{/* Summary */}

Market Summary

{safeRender(result.summary)}

{/* Key Metrics */}

Market Sentiment

{safeRender(result.marketSentiment)}

Recommendation

{safeRender(result.recommendation)}

{result.confidence && (

{safeRender(result.confidence)}% confidence

)}
{/* Trading Levels */} {result.keyLevels && (

Resistance Levels

{result.keyLevels.resistance?.join(', ') || 'None identified'}

Support Levels

{result.keyLevels.support?.join(', ') || 'None identified'}

)} {/* Trading Setup */} {(result.entry || result.stopLoss || result.takeProfits) && (

Trading Setup

{result.entry && (
Entry Point

${safeRender(result.entry.price || result.entry)}

{result.entry.rationale && (

{safeRender(result.entry.rationale)}

)}
)} {result.stopLoss && (
Stop Loss

${safeRender(result.stopLoss.price || result.stopLoss)}

{result.stopLoss.rationale && (

{safeRender(result.stopLoss.rationale)}

)}
)} {result.takeProfits && (
Take Profit

{typeof result.takeProfits === 'object' ? Object.values(result.takeProfits).map(tp => `$${safeRender(tp)}`).join(', ') : `$${safeRender(result.takeProfits)}`}

)}
)}
)}
) }