Router and Route Management
Overview
The dashboard uses the @tanstack/react-router library for routing. This is a modern, type-safe routing solution that provides a declarative API for defining routes, with support for features like nested routes, loaders, and more.
Architecture
The router is follows these key principles:
- Configuration-based: Routes are created based on the
AppConfigthat's passed to the router - Lazy-loaded: Each route is lazy-loaded using dynamic imports to optimize performance
- Data-fetching: Routes use loaders to prefetch necessary data while components can use Tanstack Query for secondary data fetching. Preloading loader data relies on Tanstack Query's as well in order to support local caching. The loader refreshes the data if it is stale.
- Nested routing: Routes are organized hierarchically.
Data Fetching with Loaders
Routes use loaders to prefetch data before rendering. This ensures that components have necessary data available when they mount. The implementation leverages React Query's queryClient for caching:
loader: () =>
queryClient.ensureQueryData(profileQuery()),
later the component can use the useQuery hook to access the data:
const { data: profile } = useQuery(profileQuery());
Adding New Routes
To add a new route to the dashboard:
- Create a new component for the route in an appropriate directory.
- Export a named
Routecomponent from the module (usingcreateLazyRoute). - Import the new route in the router configuration file and complete the route configuration (e.g.,
router.tsx). - Define loaders for any data requirements.
Authorization and Protection
By default, the whole dashboard is protected and the user must be logged in to access it. But some routes (like the profile page) require additional authorization checks. For these routes, you can use the beforeLoad hook to check if the user has the necessary permissions.
beforeLoad: async () => {
const twoStep = await fetchTwoStep();
if ( twoStep.two_step_reauthorization_required ) {
const currentPath = window.location.pathname;
const loginUrl = `/me/reauth-required?redirect_to=${ encodeURIComponent( currentPath ) }`;
window.location.href = loginUrl;
}
},