From 7197d28815cc39b9da5830f4c20b0c597c71579d Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sun, 12 Jun 2022 11:56:00 -0400 Subject: [PATCH] Chunk out the different routers and clean up feature logic --- resources/scripts/components/App.tsx | 26 ++++++++++++------- .../components/server/ServerConsole.tsx | 21 +++++---------- .../components/server/features/Features.tsx | 19 ++++++++++++++ .../components/server/features/index.ts | 16 +++++++----- resources/scripts/routers/routes.ts | 4 +-- 5 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 resources/scripts/components/server/features/Features.tsx diff --git a/resources/scripts/components/App.tsx b/resources/scripts/components/App.tsx index 9a3125456..47dbcdbab 100644 --- a/resources/scripts/components/App.tsx +++ b/resources/scripts/components/App.tsx @@ -1,11 +1,8 @@ -import React from 'react'; +import React, { lazy } from 'react'; import { hot } from 'react-hot-loader/root'; import { Route, Router, Switch } from 'react-router-dom'; import { StoreProvider } from 'easy-peasy'; import { store } from '@/state'; -import DashboardRouter from '@/routers/DashboardRouter'; -import ServerRouter from '@/routers/ServerRouter'; -import AuthenticationRouter from '@/routers/AuthenticationRouter'; import { SiteSettings } from '@/state/settings'; import ProgressBar from '@/components/elements/ProgressBar'; import { NotFound } from '@/components/elements/ScreenBlock'; @@ -16,6 +13,11 @@ import { setupInterceptors } from '@/api/interceptors'; import AuthenticatedRoute from '@/components/elements/AuthenticatedRoute'; import { ServerContext } from '@/state/server'; import '@/assets/tailwind.css'; +import Spinner from '@/components/elements/Spinner'; + +const DashboardRouter = lazy(() => import(/* webpackChunkName: "dash" */'@/routers/DashboardRouter')); +const ServerRouter = lazy(() => import('@/routers/ServerRouter')); +const AuthenticationRouter = lazy(() => import('@/routers/AuthenticationRouter')); interface ExtendedWindow extends Window { SiteConfiguration?: SiteSettings; @@ -63,15 +65,21 @@ const App = () => { - + + + - - - + + + + + - + + + diff --git a/resources/scripts/components/server/ServerConsole.tsx b/resources/scripts/components/server/ServerConsole.tsx index bc36dd33c..da87da775 100644 --- a/resources/scripts/components/server/ServerConsole.tsx +++ b/resources/scripts/components/server/ServerConsole.tsx @@ -1,4 +1,4 @@ -import React, { lazy, memo } from 'react'; +import React, { memo } from 'react'; import { ServerContext } from '@/state/server'; import Can from '@/components/elements/Can'; import ContentContainer from '@/components/elements/ContentContainer'; @@ -7,15 +7,14 @@ import ServerContentBlock from '@/components/elements/ServerContentBlock'; import ServerDetailsBlock from '@/components/server/ServerDetailsBlock'; import isEqual from 'react-fast-compare'; import PowerControls from '@/components/server/PowerControls'; -import { EulaModalFeature, JavaVersionModalFeature, GSLTokenModalFeature, PIDLimitModalFeature, SteamDiskSpaceFeature } from '@feature/index'; import ErrorBoundary from '@/components/elements/ErrorBoundary'; import Spinner from '@/components/elements/Spinner'; +import Features from '@feature/Features'; +import Console from '@/components/server/Console'; +import StatGraphs from '@/components/server/StatGraphs'; export type PowerAction = 'start' | 'stop' | 'restart' | 'kill'; -const ChunkedConsole = lazy(() => import(/* webpackChunkName: "console" */'@/components/server/Console')); -const ChunkedStatGraphs = lazy(() => import(/* webpackChunkName: "graphs" */'@/components/server/StatGraphs')); - const ServerConsole = () => { const isInstalling = ServerContext.useStoreState(state => state.server.data!.isInstalling); const isTransferring = ServerContext.useStoreState(state => state.server.data!.isTransferring); @@ -53,17 +52,11 @@ const ServerConsole = () => {
- + - + - - {eggFeatures.includes('eula') && } - {eggFeatures.includes('java_version') && } - {eggFeatures.includes('gsl_token') && } - {eggFeatures.includes('pid_limit') && } - {eggFeatures.includes('steam_disk_space') && } - +
); diff --git a/resources/scripts/components/server/features/Features.tsx b/resources/scripts/components/server/features/Features.tsx new file mode 100644 index 000000000..ee00aad07 --- /dev/null +++ b/resources/scripts/components/server/features/Features.tsx @@ -0,0 +1,19 @@ +import React, { useMemo } from 'react'; +import features from './index'; +import { getObjectKeys } from '@/helpers'; + +type ListItems = [ string, React.ComponentType ][]; + +export default ({ enabled }: { enabled: string[] }) => { + const mapped: ListItems = useMemo(() => { + return getObjectKeys(features) + .filter(key => enabled.map((v) => v.toLowerCase()).includes(key.toLowerCase())) + .reduce((arr, key) => [ ...arr, [ key, features[key] ] ], [] as ListItems); + }, [ enabled ]); + + return ( + + {mapped.map(([ key, Component ]) => )} + + ); +}; diff --git a/resources/scripts/components/server/features/index.ts b/resources/scripts/components/server/features/index.ts index 6dcba53e6..9b7ef9f76 100644 --- a/resources/scripts/components/server/features/index.ts +++ b/resources/scripts/components/server/features/index.ts @@ -1,4 +1,4 @@ -import { lazy } from 'react'; +import { ComponentType, lazy } from 'react'; /** * Custom features should be registered here as lazy components so that they do @@ -6,10 +6,12 @@ import { lazy } from 'react'; * whenever they are actually loaded for the client (which may be never, depending * on the feature and the egg). */ -const EulaModalFeature = lazy(() => import(/* webpackChunkName: "feature.eula" */'@feature/eula/EulaModalFeature')); -const JavaVersionModalFeature = lazy(() => import(/* webpackChunkName: "feature.java_version" */'@feature/JavaVersionModalFeature')); -const GSLTokenModalFeature = lazy(() => import(/* webpackChunkName: "feature.gsl_token" */'@feature/GSLTokenModalFeature')); -const PIDLimitModalFeature = lazy(() => import(/* webpackChunkName: "feature.pid_limit" */'@feature/PIDLimitModalFeature')); -const SteamDiskSpaceFeature = lazy(() => import(/* webpackChunkName: "feature.steam_disk_space" */'@feature/SteamDiskSpaceFeature')); +const features: Record = { + eula: lazy(() => import('@feature/eula/EulaModalFeature')), + java_version: lazy(() => import('@feature/JavaVersionModalFeature')), + gsl_token: lazy(() => import('@feature/GSLTokenModalFeature')), + pid_limit: lazy(() => import('@feature/PIDLimitModalFeature')), + steam_disk_space: lazy(() => import('@feature/SteamDiskSpaceFeature')), +}; -export { EulaModalFeature, JavaVersionModalFeature, GSLTokenModalFeature, PIDLimitModalFeature, SteamDiskSpaceFeature }; +export default features; diff --git a/resources/scripts/routers/routes.ts b/resources/scripts/routers/routes.ts index f03774ae7..e840604eb 100644 --- a/resources/scripts/routers/routes.ts +++ b/resources/scripts/routers/routes.ts @@ -6,11 +6,11 @@ import UsersContainer from '@/components/server/users/UsersContainer'; import BackupContainer from '@/components/server/backups/BackupContainer'; import NetworkContainer from '@/components/server/network/NetworkContainer'; import StartupContainer from '@/components/server/startup/StartupContainer'; +import FileManagerContainer from '@/components/server/files/FileManagerContainer'; +import SettingsContainer from '@/components/server/settings/SettingsContainer'; -const FileManagerContainer = lazy(() => import('@/components/server/files/FileManagerContainer')); const FileEditContainer = lazy(() => import('@/components/server/files/FileEditContainer')); const ScheduleEditContainer = lazy(() => import('@/components/server/schedules/ScheduleEditContainer')); -const SettingsContainer = lazy(() => import('@/components/server/settings/SettingsContainer')); interface ServerRouteDefinition { path: string;