Spaces:
Sleeping
Sleeping
Update frontend/src/App.jsx
Browse files- frontend/src/App.jsx +37 -17
frontend/src/App.jsx
CHANGED
|
@@ -2,17 +2,18 @@ import React, { useState, useEffect } from 'react';
|
|
| 2 |
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
| 3 |
import { onAuthStateChanged } from 'firebase/auth';
|
| 4 |
import { auth } from './services/firebase';
|
| 5 |
-
|
| 6 |
import Header from './components/layout/Header';
|
| 7 |
import Login from './components/auth/Login';
|
| 8 |
import SignUp from './components/auth/SignUp';
|
| 9 |
import ChatInterface from './components/chat/ChatInterface';
|
| 10 |
import History from './components/chat/History';
|
|
|
|
| 11 |
import './index.css';
|
| 12 |
|
| 13 |
function App() {
|
| 14 |
const [user, setUser] = useState(null);
|
| 15 |
const [loading, setLoading] = useState(true);
|
|
|
|
| 16 |
|
| 17 |
useEffect(() => {
|
| 18 |
const unsubscribe = onAuthStateChanged(auth, (currentUser) => {
|
|
@@ -22,6 +23,17 @@ function App() {
|
|
| 22 |
return () => unsubscribe();
|
| 23 |
}, []);
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if (loading) {
|
| 26 |
return <div>Loading...</div>; // Or a proper spinner component
|
| 27 |
}
|
|
@@ -30,22 +42,30 @@ function App() {
|
|
| 30 |
<Router>
|
| 31 |
<div className="app-wrapper">
|
| 32 |
<Header user={user} />
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
<
|
| 42 |
-
path="/
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
</div>
|
| 50 |
</Router>
|
| 51 |
);
|
|
|
|
| 2 |
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
| 3 |
import { onAuthStateChanged } from 'firebase/auth';
|
| 4 |
import { auth } from './services/firebase';
|
|
|
|
| 5 |
import Header from './components/layout/Header';
|
| 6 |
import Login from './components/auth/Login';
|
| 7 |
import SignUp from './components/auth/SignUp';
|
| 8 |
import ChatInterface from './components/chat/ChatInterface';
|
| 9 |
import History from './components/chat/History';
|
| 10 |
+
import Sidebar from './components/layout/Sidebar'; // Assuming Sidebar is in components/layout
|
| 11 |
import './index.css';
|
| 12 |
|
| 13 |
function App() {
|
| 14 |
const [user, setUser] = useState(null);
|
| 15 |
const [loading, setLoading] = useState(true);
|
| 16 |
+
const [currentChatId, setCurrentChatId] = useState(null); // State to manage current chat session
|
| 17 |
|
| 18 |
useEffect(() => {
|
| 19 |
const unsubscribe = onAuthStateChanged(auth, (currentUser) => {
|
|
|
|
| 23 |
return () => unsubscribe();
|
| 24 |
}, []);
|
| 25 |
|
| 26 |
+
// Function to start a new chat (or handle existing chat selection)
|
| 27 |
+
const startNewChat = () => {
|
| 28 |
+
setCurrentChatId(null); // Resets to a new chat session
|
| 29 |
+
// In a real app, you might generate a new chatId here or navigate
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
// Function to load a specific chat from history
|
| 33 |
+
const loadChat = (chatId) => {
|
| 34 |
+
setCurrentChatId(chatId);
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
if (loading) {
|
| 38 |
return <div>Loading...</div>; // Or a proper spinner component
|
| 39 |
}
|
|
|
|
| 42 |
<Router>
|
| 43 |
<div className="app-wrapper">
|
| 44 |
<Header user={user} />
|
| 45 |
+
|
| 46 |
+
{/* This div will contain both the Sidebar and the main content area (chat/history) */}
|
| 47 |
+
<div className="content-area">
|
| 48 |
+
{/* Render Sidebar only if user is logged in */}
|
| 49 |
+
{user && <Sidebar startNewChat={startNewChat} loadChat={loadChat} />}
|
| 50 |
+
|
| 51 |
+
{/* The main content area where chat or history will be rendered */}
|
| 52 |
+
<main className="main-content-flex"> {/* New class for flex container of routes */}
|
| 53 |
+
<Routes>
|
| 54 |
+
<Route path="/login" element={<Login />} />
|
| 55 |
+
<Route path="/signup" element={<SignUp />} />
|
| 56 |
+
<Route
|
| 57 |
+
path="/chat"
|
| 58 |
+
element={user ? <ChatInterface currentChatId={currentChatId} /> : <Navigate to="/login" />}
|
| 59 |
+
/>
|
| 60 |
+
<Route
|
| 61 |
+
path="/history"
|
| 62 |
+
element={user ? <History loadChat={loadChat} /> : <Navigate to="/login" />}
|
| 63 |
+
/>
|
| 64 |
+
{/* Redirect root to chat if logged in, otherwise to login */}
|
| 65 |
+
<Route path="/" element={user ? <Navigate to="/chat" /> : <Navigate to="/login" />} />
|
| 66 |
+
</Routes>
|
| 67 |
+
</main>
|
| 68 |
+
</div>
|
| 69 |
</div>
|
| 70 |
</Router>
|
| 71 |
);
|