"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' }, ] export default function AIAnalysisPanel() { const [symbol, setSymbol] = useState('BTCUSD') const [layout, setLayout] = useState(layouts[0]) const [timeframe, setTimeframe] = useState('60') const [loading, setLoading] = useState(false) const [result, setResult] = useState(null) const [error, setError] = useState(null) async function handleAnalyze() { setLoading(true) setError(null) setResult(null) try { const res = await fetch('/api/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ symbol, layout, 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

setSymbol(e.target.value)} placeholder="Symbol (e.g. BTCUSD)" />
{error && (
{error.includes('frame was detached') ? ( <> TradingView chart could not be loaded. Please check your symbol and layout, or try again.
(Technical: {error}) ) : error.includes('layout not found') ? ( <> TradingView layout not found. Please select a valid layout.
(Technical: {error}) ) : ( error )}
)} {loading && (
Analyzing chart...
)} {result && (
Summary: {result.summary}
Sentiment: {result.marketSentiment}
Recommendation: {result.recommendation} ({result.confidence}%)
Support: {result.keyLevels?.support?.join(', ')}
Resistance: {result.keyLevels?.resistance?.join(', ')}
Reasoning: {result.reasoning}
)}
) }