11import { Navigate , BrowserRouter as Router , Routes , Route , useLocation , useNavigate } from 'react-router-dom' ;
22import { Toaster } from 'react-hot-toast' ;
3- import { useEffect } from 'react' ;
3+ import { useEffect , Suspense , lazy } from 'react' ;
44import { setNavigateFunction } from './lib/axios' ;
55import Home from './components/Home' ;
66import LoginPage from './components/Login' ;
@@ -13,24 +13,27 @@ import ProfilePage from './components/ProfilePage';
1313import ResetPassword from './components/ResetPassword.tsx' ;
1414import ForgotPassword from './components/ForgotPassword' ;
1515import EditProfile from './components/ProfilePage/profileEdit' ;
16- import AddChallenge from './components/Admin/addChallenges' ;
17- import UserDashboard from './components/Admin/Users' ;
1816import Challenges from './components/Challenges' ;
19- import { Sidebar } from './components/Admin/sidebar' ;
2017import Footer from './components/footer' ;
2118import { Navbar } from './components/Navbar' ;
2219import SolutionPage from './components/Challenges/solutionPage' ;
2320import NotFoundPage from './components/Error404' ;
24- import AdminLogin from './components/Admin/index' ;
25- import Dashboard from './components/Admin/Dashboard.tsx' ;
26- import { useAdminStore } from './context/AdminContext.tsx' ;
27- import AdminChallenges from './components/Admin/AdminChallenges.tsx' ;
2821import RouteSEO from './components/RouteSEO' ;
2922import ProblemSet from './components/ProblemSet' ;
3023import CategoryProblems from './components/ProblemSet/CategoryProblems' ;
3124import { ServerErrorRoute , NetworkErrorRoute , UnauthorizedRoute } from './components/ErrorRoutes' ;
3225import { ForbiddenPage } from './components/EnhancedErrorPages' ;
33- import ChallengeDetail from './components/Admin/ChallengeDetails.tsx' ;
26+ import { useAdminStore } from './context/AdminContext.tsx' ;
27+ import LoadingSpinner from './components/LoadingSpinner' ;
28+
29+ // Lazy load Admin components
30+ const AddChallenge = lazy ( ( ) => import ( './components/Admin/addChallenges' ) ) ;
31+ const UserDashboard = lazy ( ( ) => import ( './components/Admin/Users' ) ) ;
32+ const Sidebar = lazy ( ( ) => import ( './components/Admin/sidebar' ) . then ( module => ( { default : module . Sidebar } ) ) ) ;
33+ const AdminLogin = lazy ( ( ) => import ( './components/Admin/index' ) ) ;
34+ const Dashboard = lazy ( ( ) => import ( './components/Admin/Dashboard.tsx' ) ) ;
35+ const AdminChallenges = lazy ( ( ) => import ( './components/Admin/AdminChallenges.tsx' ) ) ;
36+ const ChallengeDetail = lazy ( ( ) => import ( './components/Admin/ChallengeDetails.tsx' ) ) ;
3437
3538function UserApp ( ) {
3639 return (
@@ -73,37 +76,43 @@ function AdminApp() {
7376 if ( ! token ) {
7477 return (
7578 < div className = "flex-grow mt-16 md:mt-0" >
76- < Routes >
77- < Route path = "/codingclubadmin" element = { < AdminLogin /> } />
78- < Route path = "*" element = { < Navigate to = "/codingclubadmin" replace /> } />
79- </ Routes >
79+ < Suspense fallback = { < LoadingSpinner /> } >
80+ < Routes >
81+ < Route path = "/codingclubadmin" element = { < AdminLogin /> } />
82+ < Route path = "*" element = { < Navigate to = "/codingclubadmin" replace /> } />
83+ </ Routes >
84+ </ Suspense >
8085 < Toaster position = "bottom-right" toastOptions = { { duration : 2000 } } />
8186 </ div >
8287 ) ;
8388 }
8489 return (
8590 < div className = "flex min-h-screen" >
86- < div className = "w-64 min-h-screen fixed left-0 top-0 bg-gray-800 text-white hidden md:block" >
87- < Sidebar />
88- </ div >
91+ < Suspense fallback = { < LoadingSpinner /> } >
92+ < div className = "w-64 min-h-screen fixed left-0 top-0 bg-gray-800 text-white hidden md:block" >
93+ < Sidebar />
94+ </ div >
95+ </ Suspense >
8996 < div className = "flex-grow md:ml-64 pl-0 md:pl-4 mt-16 md:mt-0" >
9097 < Toaster position = "bottom-right" toastOptions = { { duration : 2000 } } />
91- < Routes >
92- { /* <Route path="/codingclubadmin" element={<AdminLogin />} /> */ }
93- < Route path = "/codingclubadmin" element = { < Dashboard /> } />
94- < Route path = "/codingclubadmin/users" element = { < UserDashboard /> } />
95- < Route path = "/codingclubadmin/challenges" element = { < AdminChallenges /> } />
96- < Route path = "/codingclubadmin/challenges/:id" element = { < ChallengeDetail /> } />
97- < Route path = "/codingclubadmin/users/profile/:username" element = { < ProfilePage /> } />
98- < Route path = "/codingclubadmin/addchallenge" element = { < AddChallenge /> } />
99- < Route path = "/codingclubadmin/leaderboard" element = { < Leaderboard /> } />
100- { /* Enhanced Error Pages for Admin */ }
101- < Route path = "/codingclubadmin/server-error" element = { < ServerErrorRoute /> } />
102- < Route path = "/codingclubadmin/network-error" element = { < NetworkErrorRoute /> } />
103- < Route path = "/codingclubadmin/unauthorized" element = { < UnauthorizedRoute /> } />
104- < Route path = "/codingclubadmin/forbidden" element = { < ForbiddenPage /> } />
105- < Route path = "/codingclubadmin/*" element = { < NotFoundPage /> } />
106- </ Routes >
98+ < Suspense fallback = { < LoadingSpinner /> } >
99+ < Routes >
100+ { /* <Route path="/codingclubadmin" element={<AdminLogin />} /> */ }
101+ < Route path = "/codingclubadmin" element = { < Dashboard /> } />
102+ < Route path = "/codingclubadmin/users" element = { < UserDashboard /> } />
103+ < Route path = "/codingclubadmin/challenges" element = { < AdminChallenges /> } />
104+ < Route path = "/codingclubadmin/challenges/:id" element = { < ChallengeDetail /> } />
105+ < Route path = "/codingclubadmin/users/profile/:username" element = { < ProfilePage /> } />
106+ < Route path = "/codingclubadmin/addchallenge" element = { < AddChallenge /> } />
107+ < Route path = "/codingclubadmin/leaderboard" element = { < Leaderboard /> } />
108+ { /* Enhanced Error Pages for Admin */ }
109+ < Route path = "/codingclubadmin/server-error" element = { < ServerErrorRoute /> } />
110+ < Route path = "/codingclubadmin/network-error" element = { < NetworkErrorRoute /> } />
111+ < Route path = "/codingclubadmin/unauthorized" element = { < UnauthorizedRoute /> } />
112+ < Route path = "/codingclubadmin/forbidden" element = { < ForbiddenPage /> } />
113+ < Route path = "/codingclubadmin/*" element = { < NotFoundPage /> } />
114+ </ Routes >
115+ </ Suspense >
107116 </ div >
108117 </ div >
109118 ) ;
0 commit comments