import { useState, useEffect, lazy, Suspense } from 'react'; import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import { QueryClient, QueryClientProvider } from 'react-query'; import { OrderProvider } from './contexts/OrderContext'; import { AuthProvider } from './contexts/AuthContext'; import { AdminAuthProvider } from './contexts/AdminAuthContext'; 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 ALL pages and components for faster initial load const LandingPage = lazy(() => import('./pages/LandingPage').then(m => ({ default: m.LandingPage }))); const NewOrderPage = lazy(() => import('./pages/NewOrderPage').then(m => ({ default: m.NewOrderPage }))); const LoginPage = lazy(() => import('./pages/LoginPage').then(m => ({ default: m.LoginPage }))); const PaymentSuccessPage = lazy(() => import('./pages/PaymentSuccessPage').then(m => ({ default: m.PaymentSuccessPage }))); 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 Sidebar = lazy(() => import('./components/layout/Sidebar').then(m => ({ default: m.Sidebar }))); const Header = lazy(() => import('./components/layout/Header').then(m => ({ default: m.Header }))); const Footer = lazy(() => import('./components/layout/Footer').then(m => ({ default: m.Footer }))); const Chatbot = lazy(() => import('./components/support/Chatbot').then(m => ({ default: m.Chatbot }))); const RequireAuth = lazy(() => import('./components/auth/RequireAuth')); const RequireRole = lazy(() => import('./components/auth/RequireRole')); const AdminRoute = lazy(() => import('./components/auth/AdminRoute').then(m => ({ default: m.AdminRoute }))); 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;