import { RouteObject } from 'react-router-dom'; import { lazy, Suspense } from 'react'; import MainLayout from '@/layouts/MainLayout'; import settingsRoutes from './settings-routes'; import adminCenterRoutes from './admin-center-routes'; import { useAuthService } from '@/hooks/useAuth'; import { Navigate, useLocation } from 'react-router-dom'; import { SuspenseFallback } from '@/components/suspense-fallback/suspense-fallback'; // Lazy load page components for better code splitting const HomePage = lazy(() => import('@/pages/home/home-page')); const ProjectList = lazy(() => import('@/pages/projects/project-list')); const Schedule = lazy(() => import('@/pages/schedule/schedule')); const ProjectTemplateEditView = lazy( () => import('@/pages/settings/project-templates/projectTemplateEditView/ProjectTemplateEditView') ); const LicenseExpired = lazy(() => import('@/pages/license-expired/license-expired')); const ProjectView = lazy(() => import('@/pages/projects/projectView/project-view')); const Unauthorized = lazy(() => import('@/pages/unauthorized/unauthorized')); const GanttDemoPage = lazy(() => import('@/pages/GanttDemoPage')); // Define AdminGuard component with defensive programming const AdminGuard = ({ children }: { children: React.ReactNode }) => { const authService = useAuthService(); const location = useLocation(); try { // Defensive checks to ensure authService and its methods exist if ( !authService || typeof authService.isAuthenticated !== 'function' || typeof authService.isOwnerOrAdmin !== 'function' ) { // If auth service is not ready, render children (don't block) return <>{children}; } if (!authService.isAuthenticated()) { return ; } if (!authService.isOwnerOrAdmin()) { return ; } return <>{children}; } catch (error) { console.error('Error in AdminGuard (main-routes):', error); // On error, render children to prevent complete blocking return <>{children}; } }; const mainRoutes: RouteObject[] = [ { path: '/worklenz', element: , children: [ { index: true, element: }, { path: 'home', element: ( }> ), }, { path: 'projects', element: ( }> ), }, { path: 'schedule', element: ( }> ), }, { path: `projects/:projectId`, element: ( }> ), }, { path: `settings/project-templates/edit/:templateId/:templateName`, element: ( }> ), }, { path: 'unauthorized', element: ( }> ), }, { path: 'gantt-demo', element: ( }> ), }, ...settingsRoutes, ...adminCenterRoutes, ], }, ]; // License expired route should be separate to avoid being wrapped in LicenseExpiryGuard export const licenseExpiredRoute: RouteObject = { path: '/worklenz', element: , children: [ { path: 'license-expired', element: ( }> ), }, ], }; export default mainRoutes;