import { useState, useEffect, lazy, Suspense } from 'react'; import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import { QueryClient, QueryClientProvider } from 'react-query'; import { Sidebar } from './components/layout/Sidebar'; import { Header } from './components/layout/Header'; import { Footer } from './components/layout/Footer'; import { LandingPage } from './pages/LandingPage'; import { NewOrderPage } from './pages/NewOrderPage'; import { LoginPage } from './pages/LoginPage'; import { PaymentSuccessPage } from './pages/PaymentSuccessPage'; import { Chatbot } from './components/support/Chatbot'; import { OrderProvider } from './contexts/OrderContext'; import { AuthProvider } from './contexts/AuthContext'; import { AdminAuthProvider } from './contexts/AdminAuthContext'; import RequireAuth from './components/auth/RequireAuth'; import RequireRole from './components/auth/RequireRole'; import { AdminRoute } from './components/auth/AdminRoute'; import { PaymentProvider } from './contexts/PaymentContext'; import { ReviewProvider } from './contexts/ReviewContext'; import { LoyaltyProvider } from './contexts/LoyaltyContext'; import { ScrollToTop } from './components/utils/ScrollToTop'; import { usePWA } from './hooks/usePWA'; import { useNotifications } from './hooks/useNotifications'; import { preloadCriticalAssets } from './lib/preload'; // Lazy load less critical pages for better initial load performance const AllOrdersPage = lazy(() => import('./pages/AllOrdersPage').then(m => ({ default: m.AllOrdersPage }))); const AnalyticsDashboard = lazy(() => import('./pages/AnalyticsDashboard').then(m => ({ default: m.AnalyticsDashboard }))); const ScheduledOrdersPage = lazy(() => import('./pages/ScheduledOrdersPage').then(m => ({ default: m.ScheduledOrdersPage }))); const OrderDetailPage = lazy(() => import('./pages/OrderDetailPage').then(m => ({ default: m.OrderDetailPage }))); const TestimonialsPage = lazy(() => import('./pages/TestimonialsPage').then(m => ({ default: m.TestimonialsPage }))); const AdminLogin = lazy(() => import('./pages/admin/AdminLogin').then(m => ({ default: m.AdminLogin }))); const AdminDashboard = lazy(() => import('./pages/admin/AdminDashboard').then(m => ({ default: m.AdminDashboard }))); const AdminTestimonials = lazy(() => import('./pages/admin/AdminTestimonials').then(m => ({ default: m.AdminTestimonials }))); const AdminAllOrders = lazy(() => import('./pages/admin/AdminAllOrders').then(m => ({ default: m.AdminAllOrders }))); const queryClient = new QueryClient(); // Loading spinner component for lazy-loaded routes function LoadingFallback() { return (

Loading...

); } // Using RequireAuth component from components/auth interface AppProps { onLoad?: () => void; } function AppContent({ onLoad }: AppProps) { const [isSidebarOpen, setIsSidebarOpen] = useState(false); const [isChatbotOpen, setIsChatbotOpen] = useState(false); const { canInstall, installApp } = usePWA(); const { requestPermission } = useNotifications(); // Signal app load completion useEffect(() => { onLoad?.(); }, [onLoad]); // Auto-request notification permission for better UX useEffect(() => { if ('Notification' in window && Notification.permission === 'default') { requestPermission(); } // Preload critical assets for faster loading preloadCriticalAssets(); }, [requestPermission]); return ( } /> } /> {/* Public Landing Page */} } /> {/* Admin Routes */} }> } /> }> } /> }> } /> }> } /> {/* Protected App Routes */}
{/* PWA Install Banner */} {canInstall && (
)}
setIsSidebarOpen(false)} />
{/* Minimal spacing for maximum content area */}
}> } /> } /> } /> } /> } /> } />
} />
{/* AI Chatbot */} setIsChatbotOpen(!isChatbotOpen)} /> } />
); } function App({ onLoad }: AppProps = {}) { useEffect(() => { // Call onLoad when app is ready const timer = setTimeout(() => { onLoad?.(); }, 500); // Small delay to ensure everything is mounted return () => clearTimeout(timer); }, [onLoad]); return ( ); } export default App;