2023-09-27 02:07:40 -04:00
|
|
|
import loadable, { LoadableComponent } from '@loadable/component';
|
2022-11-01 16:41:09 -04:00
|
|
|
import React from 'react';
|
|
|
|
|
2023-05-01 16:50:41 -04:00
|
|
|
export enum AsyncRouteType {
|
|
|
|
Stable,
|
2023-10-06 20:09:19 -07:00
|
|
|
Experimental,
|
|
|
|
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 element to render. */
|
|
|
|
element?: LoadableComponent<AsyncPageProps>
|
|
|
|
/** The page type used to load the correct page element. */
|
2023-05-01 16:50:41 -04:00
|
|
|
type?: AsyncRouteType
|
|
|
|
}
|
|
|
|
|
2023-09-27 02:07:40 -04:00
|
|
|
export interface AsyncPageProps {
|
2023-05-01 16:50:41 -04:00
|
|
|
/** The relative path to the page component in the routes directory. */
|
|
|
|
page: string
|
2022-11-01 16:41:09 -04:00
|
|
|
}
|
|
|
|
|
2023-10-06 20:09:19 -07:00
|
|
|
const DashboardAsyncPage = loadable(
|
|
|
|
(props: { page: string }) => import(/* webpackChunkName: "[request]" */ `../../apps/dashboard/routes/${props.page}`),
|
|
|
|
{ cacheKey: (props: AsyncPageProps) => props.page }
|
|
|
|
);
|
|
|
|
|
2023-05-01 16:50:41 -04:00
|
|
|
const ExperimentalAsyncPage = loadable(
|
|
|
|
(props: { page: string }) => import(/* webpackChunkName: "[request]" */ `../../apps/experimental/routes/${props.page}`),
|
|
|
|
{ cacheKey: (props: AsyncPageProps) => props.page }
|
|
|
|
);
|
|
|
|
|
|
|
|
const StableAsyncPage = loadable(
|
|
|
|
(props: { page: string }) => import(/* webpackChunkName: "[request]" */ `../../apps/stable/routes/${props.page}`),
|
|
|
|
{ cacheKey: (props: AsyncPageProps) => props.page }
|
|
|
|
);
|
|
|
|
|
2023-10-06 20:12:32 -07:00
|
|
|
export function toAsyncPageRoute({ path, page, element, type = AsyncRouteType.Stable }: AsyncRoute) {
|
2023-10-06 20:09:19 -07:00
|
|
|
let Element = element;
|
|
|
|
if (!Element) {
|
|
|
|
switch (type) {
|
|
|
|
case AsyncRouteType.Dashboard:
|
|
|
|
Element = DashboardAsyncPage;
|
|
|
|
break;
|
|
|
|
case AsyncRouteType.Experimental:
|
|
|
|
Element = ExperimentalAsyncPage;
|
|
|
|
break;
|
|
|
|
case AsyncRouteType.Stable:
|
|
|
|
default:
|
|
|
|
Element = StableAsyncPage;
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 20:26:00 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
path,
|
|
|
|
element: <Element page={page ?? path} />
|
|
|
|
};
|
|
|
|
}
|