diff --git a/components/AIAnalysisPanel.tsx b/components/AIAnalysisPanel.tsx index 8afe2ed..e2a772c 100644 --- a/components/AIAnalysisPanel.tsx +++ b/components/AIAnalysisPanel.tsx @@ -121,11 +121,29 @@ export default function AIAnalysisPanel({ onAnalysisComplete }: AIAnalysisPanelP console.log(`🔍 Analysis complete for ${sessionId}`) es.close() setEventSource(null) + // Reset UI state when analysis completes + setLoading(false) + setProgress(null) } else if (progressData.type === 'connected') { console.log(`🔍 EventSource connected for ${sessionId}`) } else { // Update progress state immediately setProgress(progressData) + // Check if analysis is complete based on steps + if (progressData.steps && progressData.steps.length > 0) { + const allCompleted = progressData.steps.every((step: any) => + step.status === 'completed' || step.status === 'error' + ) + if (allCompleted) { + console.log(`🔍 All steps completed for ${sessionId}, resetting UI state`) + setTimeout(() => { + setLoading(false) + setProgress(null) + es.close() + setEventSource(null) + }, 2000) // Give 2 seconds to show final state + } + } } } catch (error) { console.error('Error parsing progress data:', error) @@ -271,6 +289,10 @@ export default function AIAnalysisPanel({ onAnalysisComplete }: AIAnalysisPanelP setResult(data) + // For single timeframe analysis, don't set loading to false yet + // Let the progress tracking handle UI state reset + console.log(`🔍 API call completed for ${sessionId}, progress tracking will handle UI reset`) + // Call the callback with analysis result if provided if (onAnalysisComplete && data.analysis) { onAnalysisComplete(data.analysis, analysisSymbol) @@ -344,6 +366,10 @@ export default function AIAnalysisPanel({ onAnalysisComplete }: AIAnalysisPanelP const errorMessage = err instanceof Error ? err.message : 'Failed to perform analysis' setError(errorMessage) + // Reset loading state immediately on error + setLoading(false) + setProgress(null) + // Mark current active step as error setProgress(prev => { if (!prev) return null @@ -361,7 +387,11 @@ export default function AIAnalysisPanel({ onAnalysisComplete }: AIAnalysisPanelP return prev }) } finally { - setLoading(false) + // Only reset loading for multi-timeframe analysis + // Single timeframe analysis loading state is managed by progress tracking + if (analysisTimeframes.length > 1) { + setLoading(false) + } } } diff --git a/lib/ai-analysis.ts b/lib/ai-analysis.ts index 1558cf3..a4c6043 100644 --- a/lib/ai-analysis.ts +++ b/lib/ai-analysis.ts @@ -645,8 +645,8 @@ Analyze all provided screenshots comprehensively and return only the JSON respon if (sessionId) { progressTracker.updateStep(sessionId, 'analysis', 'completed', 'AI analysis completed successfully!') - // Mark session as complete - setTimeout(() => progressTracker.deleteSession(sessionId), 1000) + // Mark session as complete with longer delay to ensure UI receives updates + setTimeout(() => progressTracker.deleteSession(sessionId), 3000) } return {