Spaces:
Sleeping
Sleeping
File size: 5,420 Bytes
de852c2 c8cd296 de852c2 c8cd296 de852c2 | 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 | import React, { useState, useEffect, useCallback, useRef } from 'react';
import { ThemeProvider } from './contexts/ThemeContext';
import { AppConfigProvider } from './contexts/AppConfigContext';
import HomePage from './pages/HomePage';
import ChatPage from './pages/ChatPage';
import AuthPage from './pages/AuthPage';
import CanvasPage from './pages/CanvasPage';
import UserGuide from './components/UserGuide';
import {
readStoredAuth,
persistAuth,
clearStoredAuth,
getApiBaseUrl,
} from './utils/authStorage';
import './styles/components.css';
function App() {
const initialAuthRef = useRef(null);
if (initialAuthRef.current === null) {
initialAuthRef.current = readStoredAuth();
}
const initialAuth = initialAuthRef.current;
const [currentView, setCurrentView] = useState(initialAuth ? 'chat' : 'home');
const [isAuthenticated, setIsAuthenticated] = useState(Boolean(initialAuth));
const [user, setUser] = useState(initialAuth?.user ?? null);
const [authToken, setAuthToken] = useState(initialAuth?.token ?? null);
const [authBootstrapping, setAuthBootstrapping] = useState(Boolean(initialAuth));
const clearAuthState = useCallback(() => {
clearStoredAuth();
setUser(null);
setAuthToken(null);
setIsAuthenticated(false);
setCurrentView('home');
}, []);
// Re-validate stored credentials on startup so stale tokens are cleared
// and profile data stays fresh after refresh.
useEffect(() => {
if (!initialAuth) {
return;
}
let cancelled = false;
const validateStoredSession = async () => {
try {
const response = await fetch(`${getApiBaseUrl()}/auth/me`, {
headers: { Authorization: `Bearer ${initialAuth.token}` },
});
if (cancelled) {
return;
}
if (response.ok) {
const freshUser = await response.json();
persistAuth(freshUser, initialAuth.token);
setUser(freshUser);
setAuthToken(initialAuth.token);
setIsAuthenticated(true);
return;
}
if (response.status === 401 || response.status === 403) {
clearAuthState();
}
} catch {
// Keep the cached session on transient network errors so refresh
// does not force re-login when the API is briefly unavailable.
} finally {
if (!cancelled) {
setAuthBootstrapping(false);
}
}
};
validateStoredSession();
return () => {
cancelled = true;
};
}, [initialAuth, clearAuthState]);
const sessionReady = isAuthenticated && !authBootstrapping;
const navigateToAuth = () => {
if (authBootstrapping) {
return;
}
setCurrentView('auth');
};
const navigateToCanvas = (canvasView) => {
if (['insights', 'workspace', 'deliverables'].includes(canvasView)) {
localStorage.setItem('canvas-view-v2', canvasView);
}
setCurrentView('canvas');
};
const navigateToChat = () => {
setCurrentView('chat');
};
const navigateToHome = () => {
setCurrentView('home');
};
const handleAuthSuccess = (userData, token) => {
persistAuth(userData, token);
setUser(userData);
setAuthToken(token);
setIsAuthenticated(true);
setAuthBootstrapping(false);
setCurrentView('chat');
};
const handleGuestStart = async () => {
if (authBootstrapping) {
return;
}
try {
const response = await fetch(`${getApiBaseUrl()}/auth/guest`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
});
const data = await response.json();
if (response.ok) {
handleAuthSuccess(data.user, data.access_token);
} else {
setCurrentView('auth');
}
} catch {
setCurrentView('auth');
}
};
const handleSignOut = () => {
clearAuthState();
};
return (
<AppConfigProvider>
<ThemeProvider>
<div className="App">
{authBootstrapping && (
<div className="auth-bootstrap-overlay" aria-live="polite">
Restoring your session...
</div>
)}
{currentView === 'home' && (
<HomePage
onNavigateToHome={navigateToHome}
onNavigateToChat={sessionReady ? navigateToChat : navigateToAuth}
onNavigateToCanvas={sessionReady ? navigateToCanvas : navigateToAuth}
onTryAsGuest={sessionReady ? navigateToChat : handleGuestStart}
isAuthenticated={sessionReady}
/>
)}
{currentView === 'auth' && !sessionReady && (
<AuthPage onAuthSuccess={handleAuthSuccess} />
)}
{currentView === 'canvas' && sessionReady && (
<CanvasPage
user={user}
authToken={authToken}
onNavigateToHome={navigateToHome}
onNavigateToChat={navigateToChat}
onSignOut={handleSignOut}
/>
)}
{currentView === 'chat' && sessionReady && (
<ChatPage
user={user}
authToken={authToken}
onNavigateToHome={navigateToHome}
onNavigateToCanvas={navigateToCanvas}
onSignOut={handleSignOut}
/>
)}
<UserGuide />
</div>
</ThemeProvider>
</AppConfigProvider>
);
}
export default App;
|