mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Restructure async route code
This commit is contained in:
parent
20c33381f9
commit
2bc3bc8a93
5 changed files with 59 additions and 41 deletions
17
src/components/AsyncPage.tsx
Normal file
17
src/components/AsyncPage.tsx
Normal file
|
@ -0,0 +1,17 @@
|
|||
import loadable from '@loadable/component';
|
||||
|
||||
interface AsyncPageProps {
|
||||
/** The relative path to the page component in the routes directory. */
|
||||
page: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Page component that uses the loadable component library to load pages defined in the routes directory asynchronously
|
||||
* with code splitting.
|
||||
*/
|
||||
const AsyncPage = loadable(
|
||||
(props: AsyncPageProps) => import(/* webpackChunkName: "[request]" */ `../routes/${props.page}`),
|
||||
{ cacheKey: (props: AsyncPageProps) => props.page }
|
||||
);
|
||||
|
||||
export default AsyncPage;
|
10
src/routes/asyncRoutes/admin.ts
Normal file
10
src/routes/asyncRoutes/admin.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { AsyncRoute } from '.';
|
||||
|
||||
export const ASYNC_ADMIN_ROUTES: AsyncRoute[] = [
|
||||
{ path: 'usernew.html', page: 'user/usernew' },
|
||||
{ path: 'userprofiles.html', page: 'user/userprofiles' },
|
||||
{ path: 'useredit.html', page: 'user/useredit' },
|
||||
{ path: 'userlibraryaccess.html', page: 'user/userlibraryaccess' },
|
||||
{ path: 'userparentalcontrol.html', page: 'user/userparentalcontrol' },
|
||||
{ path: 'userpassword.html', page: 'user/userpassword' }
|
||||
];
|
19
src/routes/asyncRoutes/index.tsx
Normal file
19
src/routes/asyncRoutes/index.tsx
Normal file
|
@ -0,0 +1,19 @@
|
|||
import React from 'react';
|
||||
import { Route } from 'react-router-dom';
|
||||
|
||||
import AsyncPage from '../../components/AsyncPage';
|
||||
|
||||
export interface AsyncRoute {
|
||||
/** The URL path for this route. */
|
||||
path: string
|
||||
/** The relative path to the page component in the routes directory. */
|
||||
page: string
|
||||
}
|
||||
|
||||
export const toAsyncPageRoute = (route: AsyncRoute) => (
|
||||
<Route
|
||||
key={route.path}
|
||||
path={route.path}
|
||||
element={<AsyncPage page={route.page} />}
|
||||
/>
|
||||
);
|
8
src/routes/asyncRoutes/user.ts
Normal file
8
src/routes/asyncRoutes/user.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { AsyncRoute } from '.';
|
||||
|
||||
export const ASYNC_USER_ROUTES: AsyncRoute[] = [
|
||||
{ path: 'search.html', page: 'search' },
|
||||
{ path: 'userprofile.html', page: 'user/userprofile' },
|
||||
{ path: 'home.html', page: 'home' },
|
||||
{ path: 'movies.html', page: 'movies' }
|
||||
];
|
|
@ -1,61 +1,25 @@
|
|||
import loadable from '@loadable/component';
|
||||
import React from 'react';
|
||||
import { Navigate, Route, Routes } from 'react-router-dom';
|
||||
|
||||
import { toAsyncPageRoute } from './asyncRoutes';
|
||||
import { ASYNC_ADMIN_ROUTES } from './asyncRoutes/admin';
|
||||
import { ASYNC_USER_ROUTES } from './asyncRoutes/user';
|
||||
import ConnectionRequired from '../components/ConnectionRequired';
|
||||
import ServerContentPage from '../components/ServerContentPage';
|
||||
import { LEGACY_ADMIN_ROUTES, LEGACY_USER_ROUTES, toViewManagerPageRoute } from './legacyRoutes';
|
||||
|
||||
interface AsyncPageProps {
|
||||
page: string
|
||||
}
|
||||
|
||||
const AsyncPage = loadable(
|
||||
(props: AsyncPageProps) => import(/* webpackChunkName: "[request]" */ `./${props.page}`),
|
||||
{ cacheKey: (props: AsyncPageProps) => props.page }
|
||||
);
|
||||
|
||||
interface AsyncRoute {
|
||||
path: string
|
||||
page: string
|
||||
}
|
||||
|
||||
const toAsyncPageRoute = (route: AsyncRoute) => (
|
||||
<Route
|
||||
key={route.path}
|
||||
path={route.path}
|
||||
element={<AsyncPage page={route.page} />}
|
||||
/>
|
||||
);
|
||||
|
||||
const USER_ROUTES: AsyncRoute[] = [
|
||||
{ path: 'search.html', page: 'search' },
|
||||
{ path: 'userprofile.html', page: 'user/userprofile' },
|
||||
{ path: 'home.html', page: 'home' },
|
||||
{ path: 'movies.html', page: 'movies' }
|
||||
];
|
||||
|
||||
const ADMIN_ROUTES: AsyncRoute[] = [
|
||||
{ path: 'usernew.html', page: 'user/usernew' },
|
||||
{ path: 'userprofiles.html', page: 'user/userprofiles' },
|
||||
{ path: 'useredit.html', page: 'user/useredit' },
|
||||
{ path: 'userlibraryaccess.html', page: 'user/userlibraryaccess' },
|
||||
{ path: 'userparentalcontrol.html', page: 'user/userparentalcontrol' },
|
||||
{ path: 'userpassword.html', page: 'user/userpassword' }
|
||||
];
|
||||
|
||||
const AppRoutes = () => (
|
||||
<Routes>
|
||||
<Route path='/'>
|
||||
{/* User routes */}
|
||||
<Route path='/' element={<ConnectionRequired />}>
|
||||
{USER_ROUTES.map(toAsyncPageRoute)}
|
||||
{ASYNC_USER_ROUTES.map(toAsyncPageRoute)}
|
||||
{LEGACY_USER_ROUTES.map(toViewManagerPageRoute)}
|
||||
</Route>
|
||||
|
||||
{/* Admin routes */}
|
||||
<Route path='/' element={<ConnectionRequired isAdminRequired />}>
|
||||
{ADMIN_ROUTES.map(toAsyncPageRoute)}
|
||||
{ASYNC_ADMIN_ROUTES.map(toAsyncPageRoute)}
|
||||
{LEGACY_ADMIN_ROUTES.map(toViewManagerPageRoute)}
|
||||
|
||||
<Route path='configurationpage' element={
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue