Spaces:
Runtime error
Runtime error
File size: 2,372 Bytes
50ca514 | 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 | import { useEffect, useState } from "react";
import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
} from "react-router-dom";
import Profile from "@/pages/profile";
import Chat from "@/pages/chat";
import Auth from "@/pages/auth";
import PracticeZone from "@/pages/practice-zone"; // Add this import
import apiClient from "@/lib/api-client";
import { GET_USERINFO_ROUTE } from "@/lib/constants";
import { useAppStore } from "@/store";
const PrivateRoute = ({ children }) => {
const { userInfo } = useAppStore();
const isAuthenticated = !!userInfo;
return isAuthenticated ? children : <Navigate to="/auth" />;
};
const AuthRoute = ({ children }) => {
const { userInfo } = useAppStore();
const isAuthenticated = !!userInfo;
return isAuthenticated ? <Navigate to="/chat" /> : children;
};
function App() {
const { userInfo, setUserInfo } = useAppStore();
const [loading, setLoading] = useState(true);
useEffect(() => {
const getUserData = async () => {
try {
const response = await apiClient.get(GET_USERINFO_ROUTE, {
withCredentials: true,
});
if (response.status === 200 && response.data.id) {
setUserInfo(response.data);
} else {
setUserInfo(undefined);
}
} catch (error) {
setUserInfo(undefined);
} finally {
setLoading(false);
}
};
if (!userInfo) {
getUserData();
} else {
setLoading(false);
}
}, [userInfo, setUserInfo]);
if (loading) {
return <div>Loading...</div>;
}
return (
<Router>
<Routes>
<Route
path="/auth"
element={
<AuthRoute>
<Auth />
</AuthRoute>
}
/>
<Route
path="/chat"
element={
<PrivateRoute>
<Chat />
</PrivateRoute>
}
/>
<Route
path="/profile"
element={
<PrivateRoute>
<Profile />
</PrivateRoute>
}
/>
<Route
path="/practice-zone"
element={
<PrivateRoute>
<PracticeZone />
</PrivateRoute>
}
/>
<Route path="*" element={<Navigate to="/auth" />} />
</Routes>
</Router>
);
}
export default App; |