-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathApp.jsx
More file actions
92 lines (81 loc) · 2.89 KB
/
Copy pathApp.jsx
File metadata and controls
92 lines (81 loc) · 2.89 KB
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
import { lazy, Suspense } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { useAuth } from './context/AuthContext';
import MainLayout from './layouts/MainLayout';
// Lazy load pages
const LoginPage = lazy(() => import('./pages/LoginPage'));
const RegisterPage = lazy(() => import('./pages/RegisterPage'));
const DashboardPage = lazy(() => import('./pages/DashboardPage'));
const VehiclesPage = lazy(() => import('./pages/VehiclesPage'));
const DriversPage = lazy(() => import('./pages/DriversPage'));
const TripsPage = lazy(() => import('./pages/TripsPage'));
const MaintenancePage = lazy(() => import('./pages/MaintenancePage'));
const ExpensesPage = lazy(() => import('./pages/ExpensesPage'));
const AnalyticsPage = lazy(() => import('./pages/AnalyticsPage'));
const CompliancePage = lazy(() => import('./pages/CompliancePage'));
// Loading fallback
function PageLoader() {
return (
<div className="flex items-center justify-center h-[60vh]">
<div className="w-8 h-8 border-3 border-surface-200 border-t-surface-900 rounded-full animate-spin" />
</div>
);
}
// Protected route wrapper
function ProtectedRoute({ children }) {
const { user, loading } = useAuth();
if (loading) return <PageLoader />;
if (!user) return <Navigate to="/login" replace />;
return children;
}
// Guest route wrapper (redirect if already logged in)
function GuestRoute({ children }) {
const { user, loading } = useAuth();
if (loading) return <PageLoader />;
if (user) return <Navigate to="/" replace />;
return children;
}
export default function App() {
return (
<Suspense fallback={<PageLoader />}>
<Routes>
{/* Public */}
<Route
path="/login"
element={
<GuestRoute>
<LoginPage />
</GuestRoute>
}
/>
<Route
path="/register"
element={
<Suspense fallback={<PageLoader />}>
<RegisterPage />
</Suspense>
}
/>
{/* Protected */}
<Route
element={
<ProtectedRoute>
<MainLayout />
</ProtectedRoute>
}
>
<Route index element={<DashboardPage />} />
<Route path="vehicles" element={<VehiclesPage />} />
<Route path="drivers" element={<DriversPage />} />
<Route path="trips" element={<TripsPage />} />
<Route path="maintenance" element={<MaintenancePage />} />
<Route path="expenses" element={<ExpensesPage />} />
<Route path="analytics" element={<AnalyticsPage />} />
<Route path="compliance" element={<CompliancePage />} />
</Route>
{/* Catch-all */}
<Route path="*" element={<Navigate to="/" replace />} />
</Routes>
</Suspense>
);
}