import { useEffect, lazy, Suspense } from 'react'; import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'; import { useAuthStore } from '@/stores/auth'; import Layout from '@/components/Layout'; import { PageLoader } from '@/components/LoadingSpinner'; import ErrorBoundary from '@/components/ErrorBoundary'; import { ToastContainer, toast } from '@/components/Toast'; import { api } from '@/lib/api'; const LoginPage = lazy(() => import('@/pages/LoginPage')); const DashboardPage = lazy(() => import('@/pages/DashboardPage')); const ClientsPage = lazy(() => import('@/pages/ClientsPage')); const ClientDetailPage = lazy(() => import('@/pages/ClientDetailPage')); const EventsPage = lazy(() => import('@/pages/EventsPage')); const EmailsPage = lazy(() => import('@/pages/EmailsPage')); const SettingsPage = lazy(() => import('@/pages/SettingsPage')); const AdminPage = lazy(() => import('@/pages/AdminPage')); const NetworkPage = lazy(() => import('@/pages/NetworkPage')); const ReportsPage = lazy(() => import('@/pages/ReportsPage')); const TemplatesPage = lazy(() => import('@/pages/TemplatesPage')); const SegmentsPage = lazy(() => import('@/pages/SegmentsPage')); const InvitePage = lazy(() => import('@/pages/InvitePage')); const ForgotPasswordPage = lazy(() => import('@/pages/ForgotPasswordPage')); const ResetPasswordPage = lazy(() => import('@/pages/ResetPasswordPage')); const AuditLogPage = lazy(() => import('@/pages/AuditLogPage')); const TagsPage = lazy(() => import('@/pages/TagsPage')); function ProtectedRoute({ children }: { children: React.ReactNode }) { const { isAuthenticated, isLoading } = useAuthStore(); if (isLoading) return ; if (!isAuthenticated) return ; return <>{children}; } // Setup global API error interceptor api.setErrorHandler((status, message) => { if (status === 401) { toast.error('Session expired. Please log in again.'); } else if (status === 403) { toast.error('Access denied: ' + message); } else if (status >= 500) { toast.error('Server error: ' + message); } }); function PageErrorBoundary({ children }: { children: React.ReactNode }) { return {children}; } export default function App() { const { checkSession, isAuthenticated } = useAuthStore(); useEffect(() => { checkSession(); }, [checkSession]); return ( }> : } /> } /> } /> } /> }> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> ); }