File size: 7,006 Bytes
5400cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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<Session[]>([
    { id: 1, title: 'Session 1', messages: [] }
  ])
  const [activeSessionId, setActiveSessionId] = useState(1)
  const activeSession = sessions.find(s => s.id === activeSessionId) || sessions[0]
  const videoRef = useRef<HTMLVideoElement>(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 (
    <>
      <video
        ref={videoRef}
        className="background-video"
        autoPlay
        loop
        muted
        playsInline
        src="https://i.imgur.com/RCoLmZ9.mp4"
      />

      <div className="app-container">
        {sidebarOpen && (
          <aside className="sidebar">
            <div className="sidebar-header">
              <img
                src="/image.png"
                alt="Collapse Sidebar"
                className="sidebar-icon"
                onClick={() => setSidebarOpen(false)}
              />
              <h1 className="site-title">FinTrack</h1>
              <img
                src="/newchat.png"
                alt="New Chat"
                className="sidebar-icon"
                onClick={handleNewChat}
              />
            </div>

            <div className="history-list">
              {sessions.map(session => (
                <div
                  key={session.id}
                  className={`history-item ${session.id === activeSessionId ? 'active' : ''}`}
                  onClick={() => setActiveSessionId(session.id)}
                >
                  {session.title}
                </div>
              ))}
            </div>
          </aside>
        )}

        {!sidebarOpen && (
          <img
            src="/opensidebar.png"
            alt="Open Sidebar"
            className="open-sidebar-button"
            onClick={() => setSidebarOpen(true)}
          />
        )}

        <main className="main-panel">
          <div className="top-ui-wrapper">
            <div className="top-navbar">
              <img
                src="/Cropped_Image.png"
                className="nav-logo"
                alt="Logo"
                onClick={() => navigate('/')}
              />
              <span className="nav-link" onClick={() => navigate('/about')}>About</span>
              <span className="nav-link" onClick={() => navigate('/about#founders')}>Founders</span>
            </div>

            <div className="auth-buttons">
              {!isAuthenticated ? (
                <>
                  <span className="auth-link" onClick={() => loginWithRedirect()}>Login</span>
                  <span
                    className="auth-link"
                    onClick={() =>
                      loginWithRedirect({ authorizationParams: { screen_hint: 'signup' } })
                    }
                  >
                    Sign Up
                  </span>
                </>
              ) : (
                <span className="auth-link" onClick={() => logout({ logoutParams: { returnTo: window.location.origin } })}>
                  Log Out
                </span>
              )}
            </div>
          </div>

          {showPrompt && (
            <div className="prompt-banner">
              <h2 className="prompt-text fade-in">How can I help you?</h2>
              <p className="suggestion-text">{currentSuggestion}</p>
            </div>
          )}

          <div className="chat-area">
            {activeSession.messages.map((msg, i) => (
              <MessageBubble key={i} role={msg.role} text={msg.text} />
            ))}
          </div>

          <div className="chat-box-wrapper">
            <ChatInput onSend={handleSend} />
          </div>
        </main>
      </div>
    </>
  )
}

export default App