2024-07-15 11:04:56 -04:00
|
|
|
import type { RouteObject } from 'react-router-dom';
|
2022-11-01 16:41:09 -04:00
|
|
|
|
2023-05-01 16:50:41 -04:00
|
|
|
export enum AsyncRouteType {
|
|
|
|
Stable,
|
2023-10-06 20:09:19 -07:00
|
|
|
Experimental,
|
2024-08-21 02:54:09 -04:00
|
|
|
Dashboard
|
2023-05-01 16:50:41 -04:00
|
|
|
}
|
2022-11-01 16:41:09 -04:00
|
|
|
|
|
|
|
export interface AsyncRoute {
|
|
|
|
/** The URL path for this route. */
|
|
|
|
path: string
|
2023-09-27 02:07:40 -04:00
|
|
|
/**
|
|
|
|
* The relative path to the page component in the routes directory.
|
|
|
|
* Will fallback to using the `path` value if not specified.
|
|
|
|
*/
|
|
|
|
page?: string
|
|
|
|
/** The page type used to load the correct page element. */
|
2023-05-01 16:50:41 -04:00
|
|
|
type?: AsyncRouteType
|
|
|
|
}
|
|
|
|
|
2024-11-18 16:55:45 -05:00
|
|
|
const importRoute = (page: string, type: AsyncRouteType) => {
|
2024-07-15 11:04:56 -04:00
|
|
|
switch (type) {
|
|
|
|
case AsyncRouteType.Dashboard:
|
|
|
|
return import(/* webpackChunkName: "[request]" */ `../../apps/dashboard/routes/${page}`);
|
|
|
|
case AsyncRouteType.Experimental:
|
|
|
|
return import(/* webpackChunkName: "[request]" */ `../../apps/experimental/routes/${page}`);
|
|
|
|
case AsyncRouteType.Stable:
|
|
|
|
return import(/* webpackChunkName: "[request]" */ `../../apps/stable/routes/${page}`);
|
2023-10-06 20:09:19 -07:00
|
|
|
}
|
2024-07-15 11:04:56 -04:00
|
|
|
};
|
|
|
|
|
2024-07-16 14:48:56 -04:00
|
|
|
export const toAsyncPageRoute = ({
|
2024-07-15 11:04:56 -04:00
|
|
|
path,
|
2024-07-16 14:48:56 -04:00
|
|
|
page,
|
|
|
|
type = AsyncRouteType.Stable
|
|
|
|
}: AsyncRoute): RouteObject => {
|
|
|
|
return {
|
|
|
|
path,
|
|
|
|
lazy: async () => {
|
2024-11-20 17:56:59 -05:00
|
|
|
const {
|
|
|
|
// If there is a default export, use it as the Component for compatibility
|
|
|
|
default: Component,
|
|
|
|
...route
|
|
|
|
} = await importRoute(page ?? path, type);
|
2024-11-18 16:55:45 -05:00
|
|
|
|
2024-11-20 17:56:59 -05:00
|
|
|
return {
|
|
|
|
Component,
|
|
|
|
...route
|
|
|
|
};
|
2024-07-16 14:48:56 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|