All files / src/components app-router.tsx

78.37% Statements 29/37
50% Branches 3/6
56.25% Functions 9/16
78.37% Lines 29/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                                        2x       2x       2x       2x       2x       2x         2x                 2x   2x 9x   2x     2x   10x     10x 9x             10x   10x   9x             10x   9x                 10x   9x                 10x   9x                 10x   9x                 10x   9x                 10x                                                                                                                  
import { Suspense, useTransition, useEffect, useMemo, memo } from 'react';
import type { ReactNode } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { Header } from '@/components/header';
import { Sidebar } from '@/components/sidebar';
import { ToastContainer } from '@/components/toast';
import { authService } from '@/features/auth/services/auth.service';
import { ProtectedRoute } from '@/features/auth/components/protected-route';
import { useRoutePrefetching } from '@/hooks/use-route-prefetching';
import { PageSkeleton } from '@/components/loading-fallback';
import { createLazyComponent } from '@/lib/route-prefetcher';
 
import {
  DASHBOARD_ROUTES,
  WALLET_ROUTES,
  STATS_ROUTES,
  AUTH_ROUTES,
  PROFILE_ROUTES,
} from '@/constants/route';
 
const DashboardPage = createLazyComponent(
  () => import('@/features/dashboard/dashboard-page'),
  DASHBOARD_ROUTES.OVERVIEW
);
const WalletPage = createLazyComponent(
  () => import('@/features/wallet/wallet-page'),
  WALLET_ROUTES.WALLET
);
const TransactionPage = createLazyComponent(
  () => import('@/features/transaction/transaction-page'),
  WALLET_ROUTES.TRANSACTIONS
);
const StatisticsPage = createLazyComponent(
  () => import('@/features/statistics/statistics-page'),
  STATS_ROUTES.STATISTICS
);
const SettingPage = createLazyComponent(
  () => import('@/features/setting/setting-page'),
  PROFILE_ROUTES.SETTINGS
);
const AuthPage = createLazyComponent(
  () => import('@/features/auth/auth-page'),
  AUTH_ROUTES.LOGIN
);
 
const AuthenticatedLayout = memo<{ children: ReactNode }>(({ children }) => (
  <div className="h-screen flex flex-col bg-background">
    <Header />
    <div className="flex flex-1 overflow-hidden">
      <Sidebar />
      <main className="flex-1 bg-background">{children}</main>
    </div>
  </div>
));
AuthenticatedLayout.displayName = 'AuthenticatedLayout';
 
const PublicLayout = memo<{ children: ReactNode }>(({ children }) => (
  <div className="h-screen bg-background">{children}</div>
));
PublicLayout.displayName = 'PublicLayout';
 
// AppRouter component that runs inside Router context
export const AppRouter = () => {
  // Initialize route prefetching
  useRoutePrefetching();
 
  // Enable performance monitoring in development only
  useEffect(() => {
    Iif (process.env.NODE_ENV === 'development') {
      // Dynamic import only in development to avoid including in production bundle
      import('@/hooks/use-route-performance');
    }
  }, []);
 
  // React 19 transition hook for smoother navigation
  const [isPending] = useTransition();
 
  const publicRouteElement = useMemo(
    () => (
      <PublicLayout>
        <AuthPage />
      </PublicLayout>
    ),
    []
  );
 
  const dashboardRouteElement = useMemo(
    () => (
      <ProtectedRoute>
        <AuthenticatedLayout>
          <DashboardPage />
        </AuthenticatedLayout>
      </ProtectedRoute>
    ),
    []
  );
 
  const walletRouteElement = useMemo(
    () => (
      <ProtectedRoute>
        <AuthenticatedLayout>
          <WalletPage />
        </AuthenticatedLayout>
      </ProtectedRoute>
    ),
    []
  );
 
  const transactionRouteElement = useMemo(
    () => (
      <ProtectedRoute>
        <AuthenticatedLayout>
          <TransactionPage />
        </AuthenticatedLayout>
      </ProtectedRoute>
    ),
    []
  );
 
  const statisticsRouteElement = useMemo(
    () => (
      <ProtectedRoute>
        <AuthenticatedLayout>
          <StatisticsPage />
        </AuthenticatedLayout>
      </ProtectedRoute>
    ),
    []
  );
 
  const settingsRouteElement = useMemo(
    () => (
      <ProtectedRoute>
        <AuthenticatedLayout>
          <SettingPage />
        </AuthenticatedLayout>
      </ProtectedRoute>
    ),
    []
  );
 
  return (
    <>
      {/* Toast Container for global notifications */}
      <ToastContainer />
 
      {/* Show pending state during transitions */}
      {isPending && (
        <div className="fixed top-0 left-0 right-0 z-50 h-1 bg-primary/20">
          <div className="h-full bg-primary animate-pulse" />
        </div>
      )}
 
      {/* React 19 improved Suspense with better error boundaries */}
      <Suspense fallback={<PageSkeleton />}>
        <Routes>
          {/* Public routes */}
          <Route path={AUTH_ROUTES.LOGIN} element={publicRouteElement} />
 
          {/* Protected routes - using memoized elements for better performance */}
          <Route
            path={DASHBOARD_ROUTES.OVERVIEW}
            element={dashboardRouteElement}
          />
          <Route path={WALLET_ROUTES.WALLET} element={walletRouteElement} />
          <Route
            path={WALLET_ROUTES.TRANSACTIONS}
            element={transactionRouteElement}
          />
          <Route
            path={STATS_ROUTES.STATISTICS}
            element={statisticsRouteElement}
          />
          <Route
            path={PROFILE_ROUTES.SETTINGS}
            element={settingsRouteElement}
          />
 
          {/* Default redirects */}
          <Route
            path={DASHBOARD_ROUTES.HOME}
            element={
              authService.isAuthenticated() ? (
                <Navigate to={DASHBOARD_ROUTES.OVERVIEW} replace />
              ) : (
                <Navigate to={AUTH_ROUTES.LOGIN} replace />
              )
            }
          />
          <Route
            path="*"
            element={<Navigate to={DASHBOARD_ROUTES.OVERVIEW} replace />}
          />
        </Routes>
      </Suspense>
    </>
  );
};