import { AppLayout, type SidebarItem } from "@/components/AppLayout"; import { CalendarCheck, Home, Loader2, MapPin, Receipt, Settings, User, } from "lucide-react"; import { useEffect, useRef } from "react"; import { Navigate, Outlet, Route, Routes, useLocation, useNavigate, } from "react-router-dom"; import useAuth from "./hooks/useAuth"; import EDRFreightLandingPage from "./pages/EDRFreightLandingPage"; import MyPortalPage from "./pages/MyPortalPage"; import ProfilePage from "./pages/ProfilePage"; import MySignaturePage from "./pages/MySignaturePage"; import SettingsPage from "./pages/SettingsPage"; import LoginPage from "./pages/accounts/LoginPage"; import OnboardingPage from "./pages/accounts/OnboardingPage"; import SetPasswordPage from "./pages/accounts/SetPasswordPage"; import SignupPage from "./pages/accounts/SignupPage"; import VerificationOtpPage from "./pages/accounts/VerificationOtpPage"; import BillingPage from "./pages/billing/BillingPage"; import BookingContractPage from "./pages/bookings/BookingContractPage"; import BookingDetailPage from "./pages/bookings/BookingDetailPage"; import EditBookingPage from "./pages/bookings/EditBookingPage"; import MyBookings from "./pages/bookings/MyBookings"; import NewBookingPage from "./pages/bookings/NewBookingPage"; import CheckPaymentPage from "./pages/payments/CheckPaymentPage"; import TrackingPage from "./pages/tracking/TrackingPage"; function FullScreenSpinner() { return (
); } function LogoutHandler() { const { logout } = useAuth(); const navigate = useNavigate(); const hasRun = useRef(false); useEffect(() => { if (hasRun.current) return; hasRun.current = true; logout().then(() => navigate("/login", { replace: true })); }, []); return ; } /** Blocks unauthenticated users; renders children only with a valid session. */ function RequireAuth() { const { isPending, isAuthenticated } = useAuth(); const location = useLocation(); if (isPending) return ; if (!isAuthenticated) return ; return ; } /** * Sends authenticated users without a company to onboarding. * Only redirects on a confirmed "no company" response — never on a * transient query error. */ function RequireCompany() { const { customerQuery } = useAuth(); if (customerQuery.isPending) return ; return ; } /** Keeps already-onboarded users out of the onboarding flow. */ function RequireNoCompany() { const { customerQuery } = useAuth(); if (customerQuery.isPending) return ; if (customerQuery.data) return ; return ; } /** Keeps authenticated users off the login/signup pages. */ function RedirectIfAuthed() { const { isPending, isAuthenticated } = useAuth(); if (isPending) return ; if (isAuthenticated) return ; return ; } /** Landing page for visitors; authenticated users go straight to the portal. */ function LandingRoute() { const { isPending, isAuthenticated } = useAuth(); if (isPending) return ; if (isAuthenticated) return ; return ; } const sidebarItems: SidebarItem[] = [ { label: "Home", href: "/portal", icon: }, { label: "My Bookings", href: "/bookings", icon: , }, { label: "Tracking", href: "/tracking", icon: , }, { label: "Billing", href: "/billing", icon: , }, { section: "Account", label: "Profile", href: "/profile", icon: , }, { section: "Account", label: "Settings", href: "/settings", icon: , }, ]; const App = () => { const navigate = useNavigate(); const location = useLocation(); const { user } = useAuth(); const displayName = user?.name?.en || user?.username || user?.email || "User"; const userEmail = user?.email; return ( {/* Public routes */} } /> } /> } /> {/* Auth pages — inaccessible once logged in */} }> } /> } /> {/* Signup-flow pages; reached while a session already exists */} } /> } /> }> }> } /> }> } > } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> ); }; export default App;