"use client" import React, { useState, useEffect } from 'react' interface SessionInfo { isAuthenticated: boolean hasSavedCookies: boolean hasSavedStorage: boolean cookiesCount: number currentUrl: string browserActive: boolean connectionStatus: 'connected' | 'disconnected' | 'unknown' | 'error' lastChecked: string dockerEnv?: boolean environment?: string } export default function SessionStatus() { const [sessionInfo, setSessionInfo] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [refreshing, setRefreshing] = useState(false) const fetchSessionStatus = async () => { try { setError(null) const response = await fetch('/api/session-status', { cache: 'no-cache' // Important for Docker environment }) const data = await response.json() if (data.success) { setSessionInfo(data.session) } else { setError(data.error || 'Failed to fetch session status') setSessionInfo(data.session || null) } } catch (e) { setError(e instanceof Error ? e.message : 'Network error') setSessionInfo(null) } finally { setLoading(false) } } const handleSessionAction = async (action: string) => { try { setRefreshing(true) setError(null) const response = await fetch('/api/session-status', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action }), cache: 'no-cache' // Important for Docker environment }) const data = await response.json() if (data.success) { // Refresh session status after action await fetchSessionStatus() } else { setError(data.error || `Failed to ${action} session`) } } catch (e) { setError(e instanceof Error ? e.message : `Failed to ${action} session`) } finally { setRefreshing(false) } } useEffect(() => { fetchSessionStatus() // Auto-refresh more frequently in Docker environment (30s vs 60s) const refreshInterval = 30000 // Start with 30 seconds for all environments const interval = setInterval(fetchSessionStatus, refreshInterval) return () => clearInterval(interval) }, []) const getConnectionStatus = () => { if (!sessionInfo) return { color: 'bg-gray-500', text: 'Unknown', icon: '❓' } if (sessionInfo.isAuthenticated && sessionInfo.connectionStatus === 'connected') { return { color: 'bg-green-500', text: 'Connected & Authenticated', icon: '✅' } } else if (sessionInfo.hasSavedCookies || sessionInfo.hasSavedStorage) { return { color: 'bg-yellow-500', text: 'Session Available', icon: '🟡' } } else if (sessionInfo.connectionStatus === 'connected') { return { color: 'bg-blue-500', text: 'Connected (Not Authenticated)', icon: '🔵' } } else { return { color: 'bg-red-500', text: 'Disconnected', icon: '🔴' } } } const status = getConnectionStatus() return (

🌐 Session Status

{/* Connection Status */}

{status.text}

{sessionInfo?.dockerEnv ? 'Docker Environment' : 'Local Environment'} {sessionInfo?.environment && ` • ${sessionInfo.environment}`}

{status.icon}
{/* Session Details */}
Browser Status
{loading ? 'Checking...' : sessionInfo?.browserActive ? 'Active' : 'Inactive'}
Cookies
{loading ? '...' : sessionInfo?.cookiesCount || 0} stored
{sessionInfo?.currentUrl && (
Current URL
{sessionInfo.currentUrl}
)} {sessionInfo?.lastChecked && (
Last updated: {new Date(sessionInfo.lastChecked).toLocaleString()}
)}
{/* Error Display */} {error && (
⚠️

Connection Error

{error}

)} {/* Action Buttons */}
) } } else if (sessionInfo.hasSavedCookies || sessionInfo.hasSavedStorage) { return `Session Available${dockerSuffix}` } else { return `Not Logged In${dockerSuffix}` } } const getDetailedStatus = () => { if (!sessionInfo) return [] return [ { label: 'Authenticated', value: sessionInfo.isAuthenticated ? '✅' : '❌' }, { label: 'Connection', value: sessionInfo.connectionStatus === 'connected' ? '✅' : sessionInfo.connectionStatus === 'disconnected' ? '🔌' : sessionInfo.connectionStatus === 'error' ? '❌' : '❓' }, { label: 'Browser Active', value: sessionInfo.browserActive ? '✅' : '❌' }, { label: 'Saved Cookies', value: sessionInfo.hasSavedCookies ? `✅ (${sessionInfo.cookiesCount})` : '❌' }, { label: 'Saved Storage', value: sessionInfo.hasSavedStorage ? '✅' : '❌' }, { label: 'Environment', value: sessionInfo.dockerEnv ? '🐳 Docker' : '💻 Local' }, ] } return (

TradingView Session

{/* Status Indicator */}
{getStatusText()}
{/* Detailed Status */} {sessionInfo && (
{getDetailedStatus().map((item, index) => (
{item.label}: {item.value}
))}
Last Checked: {new Date(sessionInfo.lastChecked).toLocaleTimeString()}
)} {/* Error Display */} {error && (

{error}

)} {/* Action Buttons */}
{/* Usage Instructions */} {sessionInfo && !sessionInfo.isAuthenticated && (

💡 To establish session: Run the analysis or screenshot capture once to trigger manual login, then future requests will use the saved session and avoid captchas. {sessionInfo.dockerEnv && ( <>
🐳 Docker: Session data is persisted in the container volume for reuse across restarts. )}

)} {/* Docker Environment Info */} {sessionInfo?.dockerEnv && (

🐳 Docker Environment: Running in containerized mode. Session persistence is enabled via volume mount at /.tradingview-session

)}
) }