misc_pterodactyl-panel/resources/scripts/components/App.tsx

96 lines
3.7 KiB
TypeScript
Raw Normal View History

import React, { lazy } from 'react';
import { hot } from 'react-hot-loader/root';
2022-02-05 17:08:43 +00:00
import { Route, Router, Switch } from 'react-router-dom';
import { StoreProvider } from 'easy-peasy';
import { store } from '@/state';
2019-12-16 02:05:44 +00:00
import { SiteSettings } from '@/state/settings';
import ProgressBar from '@/components/elements/ProgressBar';
2021-01-31 02:01:32 +00:00
import { NotFound } from '@/components/elements/ScreenBlock';
import tw from 'twin.macro';
import GlobalStylesheet from '@/assets/css/GlobalStylesheet';
import { history } from '@/components/history';
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: "dashboard" */'@/routers/DashboardRouter'));
const ServerRouter = lazy(() => import(/* webpackChunkName: "server" */'@/routers/ServerRouter'));
const AuthenticationRouter = lazy(() => import(/* webpackChunkName: "auth" */'@/routers/AuthenticationRouter'));
2019-12-16 02:05:44 +00:00
interface ExtendedWindow extends Window {
SiteConfiguration?: SiteSettings;
PterodactylUser?: {
uuid: string;
username: string;
email: string;
/* eslint-disable camelcase */
root_admin: boolean;
use_totp: boolean;
language: string;
updated_at: string;
created_at: string;
/* eslint-enable camelcase */
};
}
setupInterceptors(history);
const App = () => {
2019-12-16 02:05:44 +00:00
const { PterodactylUser, SiteConfiguration } = (window as ExtendedWindow);
if (PterodactylUser && !store.getState().user.data) {
store.getActions().user.setUserData({
2019-12-16 02:05:44 +00:00
uuid: PterodactylUser.uuid,
username: PterodactylUser.username,
email: PterodactylUser.email,
language: PterodactylUser.language,
rootAdmin: PterodactylUser.root_admin,
useTotp: PterodactylUser.use_totp,
createdAt: new Date(PterodactylUser.created_at),
updatedAt: new Date(PterodactylUser.updated_at),
});
}
2019-12-16 02:05:44 +00:00
if (!store.getState().settings.data) {
store.getActions().settings.setSettings(SiteConfiguration!);
}
return (
<>
<GlobalStylesheet/>
<StoreProvider store={store}>
2020-12-27 19:18:33 +00:00
<ProgressBar/>
<div css={tw`mx-auto w-auto`}>
<Router history={history}>
<Switch>
<Route path={'/auth'}>
<Spinner.Suspense>
<AuthenticationRouter/>
</Spinner.Suspense>
</Route>
<AuthenticatedRoute path={'/server/:id'}>
<Spinner.Suspense>
<ServerContext.Provider>
<ServerRouter/>
</ServerContext.Provider>
</Spinner.Suspense>
</AuthenticatedRoute>
<AuthenticatedRoute path={'/'}>
<Spinner.Suspense>
<DashboardRouter/>
</Spinner.Suspense>
</AuthenticatedRoute>
<Route path={'*'}>
<NotFound/>
</Route>
2020-12-27 19:18:33 +00:00
</Switch>
</Router>
</div>
</StoreProvider>
</>
);
};
export default hot(App);