1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/router/AsyncRoute.tsx

68 lines
1.9 KiB
TypeScript
Raw Normal View History

import loadable, { LoadableComponent } from '@loadable/component';
2022-11-01 16:41:09 -04:00
import React from 'react';
import { Route } from 'react-router-dom';
2023-05-01 16:50:41 -04:00
export enum AsyncRouteType {
Stable,
Experimental
}
2022-11-01 16:41:09 -04:00
export interface AsyncRoute {
/** The URL path for this route. */
path: string
/**
* 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
}
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-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 }
);
export const toAsyncPageRoute = ({ path, page, element, type = AsyncRouteType.Stable }: AsyncRoute) => {
const Element = element
|| (
2023-05-01 16:50:41 -04:00
type === AsyncRouteType.Experimental ?
ExperimentalAsyncPage :
StableAsyncPage
);
return (
<Route
key={path}
path={path}
element={<Element page={page ?? path} />}
/>
);
};
export function toAsyncPageRouteConfig({ path, page, element, type = AsyncRouteType.Stable }: AsyncRoute) {
const Element = element || (
type === AsyncRouteType.Experimental ?
ExperimentalAsyncPage :
StableAsyncPage
);
return {
path,
element: <Element page={page ?? path} />
};
}