import { useState, useEffect, useRef } from 'react' import { useAuth0 } from '@auth0/auth0-react' import { useNavigate } from 'react-router-dom' import ChatInput from './components/chatinput' import MessageBubble from './components/MessageBubble' import './styles/App.css' type Message = { role: 'user' | 'bot'; text: string } type Session = { id: number; title: string; messages: Message[] } function App() { const { loginWithRedirect, logout, isAuthenticated } = useAuth0() const navigate = useNavigate() const [sidebarOpen, setSidebarOpen] = useState(true) const [showPrompt, setShowPrompt] = useState(true) const [sessions, setSessions] = useState([ { id: 1, title: 'Session 1', messages: [] } ]) const [activeSessionId, setActiveSessionId] = useState(1) const activeSession = sessions.find(s => s.id === activeSessionId) || sessions[0] const videoRef = useRef(null) const handleSend = async (text: string) => { const userMessage: Message = { role: 'user', text } setSessions(prev => prev.map(session => session.id === activeSessionId ? { ...session, messages: [...session.messages, userMessage] } : session ) ) setShowPrompt(false) try { const res = await fetch('https://fintrack-backend-8dji.onrender.com/generate-strategy', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_input: text }) }); const data = await res.json() const botText = data.strategy_code || data.error || 'Error: Empty response.' const botMessage: Message = { role: 'bot', text: botText } setSessions(prev => prev.map(session => session.id === activeSessionId ? { ...session, messages: [...session.messages, botMessage] } : session ) ) } catch (err) { const errorMessage: Message = { role: 'bot', text: '⚠️ Error: Failed to connect to server.' } setSessions(prev => prev.map(session => session.id === activeSessionId ? { ...session, messages: [...session.messages, errorMessage] } : session ) ) } } const handleNewChat = () => { const newId = sessions.length + 1 const newSession: Session = { id: newId, title: `Session ${newId}`, messages: [] } setSessions(prev => [newSession, ...prev]) setActiveSessionId(newId) setShowPrompt(true) } const suggestions = [ 'Enter a stock trading strategy...', 'Backtest a moving average crossover...', 'Analyze S&P 500 signals...', 'Try a momentum-based portfolio...', 'Run a mean reversion strategy...', 'Backtest Bollinger Band breakouts...' ] const [currentSuggestion, setCurrentSuggestion] = useState(suggestions[0]) useEffect(() => { const interval = setInterval(() => { setCurrentSuggestion(prev => { let next = prev while (next === prev) { next = suggestions[Math.floor(Math.random() * suggestions.length)] } return next }) }, 3000) return () => clearInterval(interval) }, []) useEffect(() => { const handleVisibility = () => { if (videoRef.current) { if (document.visibilityState === 'visible') { videoRef.current.play().catch(() => {}) } else { videoRef.current.pause() } } } document.addEventListener('visibilitychange', handleVisibility) return () => { document.removeEventListener('visibilitychange', handleVisibility) } }, []) return ( <>