misc_pterodactyl-panel/resources/scripts/routers/ServerRouter.tsx

145 lines
7.1 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import { NavLink, Route, RouteComponentProps, Switch } from 'react-router-dom';
import NavigationBar from '@/components/NavigationBar';
import ServerConsole from '@/components/server/ServerConsole';
import TransitionRouter from '@/TransitionRouter';
import WebsocketHandler from '@/components/server/WebsocketHandler';
import { ServerContext } from '@/state/server';
import DatabasesContainer from '@/components/server/databases/DatabasesContainer';
2019-07-28 03:23:44 +00:00
import FileManagerContainer from '@/components/server/files/FileManagerContainer';
import { CSSTransition } from 'react-transition-group';
import SuspenseSpinner from '@/components/elements/SuspenseSpinner';
2019-10-19 22:31:02 +00:00
import FileEditContainer from '@/components/server/files/FileEditContainer';
2019-12-07 23:58:37 +00:00
import SettingsContainer from '@/components/server/settings/SettingsContainer';
2020-02-08 23:23:08 +00:00
import ScheduleContainer from '@/components/server/schedules/ScheduleContainer';
2020-03-18 06:33:53 +00:00
import ScheduleEditContainer from '@/components/server/schedules/ScheduleEditContainer';
import UsersContainer from '@/components/server/users/UsersContainer';
import Can from '@/components/elements/Can';
import BackupContainer from '@/components/server/backups/BackupContainer';
import Spinner from '@/components/elements/Spinner';
import ServerError from '@/components/screens/ServerError';
import { httpErrorToHuman } from '@/api/http';
import NotFound from '@/components/screens/NotFound';
import { useStoreState } from 'easy-peasy';
import useServer from '@/plugins/useServer';
2020-04-17 21:43:03 +00:00
import ScreenBlock from '@/components/screens/ScreenBlock';
import SubNavigation from '@/components/elements/SubNavigation';
const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) => {
const { rootAdmin } = useStoreState(state => state.user.data!);
const [ error, setError ] = useState('');
const [ installing, setInstalling ] = useState(false);
const server = useServer();
const getServer = ServerContext.useStoreActions(actions => actions.server.getServer);
const clearServerState = ServerContext.useStoreActions(actions => actions.clearServerState);
useEffect(() => () => {
clearServerState();
}, []);
useEffect(() => {
setInstalling(server?.isInstalling !== false);
}, [ server?.isInstalling ]);
useEffect(() => {
setError('');
setInstalling(false);
getServer(match.params.id)
.catch(error => {
if (error.response?.status === 409) {
setInstalling(true);
} else {
console.error(error);
setError(httpErrorToHuman(error));
}
});
return () => {
clearServerState();
};
}, [ match.params.id ]);
return (
2020-04-10 17:11:15 +00:00
<React.Fragment key={'server-router'}>
<NavigationBar/>
{!server ?
error ?
<ServerError message={error}/>
:
<Spinner size={'large'} centered/>
:
<>
2020-07-05 01:30:50 +00:00
<CSSTransition timeout={250} classNames={'fade'} appear in>
<SubNavigation>
<div>
<NavLink to={`${match.url}`} exact>Console</NavLink>
<Can action={'file.*'}>
<NavLink to={`${match.url}/files`}>File Manager</NavLink>
</Can>
<Can action={'database.*'}>
<NavLink to={`${match.url}/databases`}>Databases</NavLink>
</Can>
<Can action={'schedule.*'}>
<NavLink to={`${match.url}/schedules`}>Schedules</NavLink>
</Can>
<Can action={'user.*'}>
<NavLink to={`${match.url}/users`}>Users</NavLink>
</Can>
<Can action={'backup.*'}>
<NavLink to={`${match.url}/backups`}>Backups</NavLink>
</Can>
2020-07-05 01:30:50 +00:00
<Can action={[ 'settings.*', 'file.sftp' ]} matchAny>
<NavLink to={`${match.url}/settings`}>Settings</NavLink>
</Can>
</div>
</SubNavigation>
</CSSTransition>
{(installing && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${server.id}`)))) ?
<ScreenBlock
title={'Your server is installing.'}
image={'/assets/svgs/server_installing.svg'}
message={'Please check back in a few minutes.'}
/>
:
<>
<WebsocketHandler/>
<TransitionRouter>
<Switch location={location}>
<Route path={`${match.path}`} component={ServerConsole} exact/>
<Route path={`${match.path}/files`} component={FileManagerContainer} exact/>
<Route
path={`${match.path}/files/:action(edit|new)`}
render={props => (
<SuspenseSpinner>
<FileEditContainer {...props as any}/>
</SuspenseSpinner>
)}
exact
/>
<Route path={`${match.path}/databases`} component={DatabasesContainer} exact/>
<Route path={`${match.path}/schedules`} component={ScheduleContainer} exact/>
<Route
path={`${match.path}/schedules/:id`}
component={ScheduleEditContainer}
exact
/>
<Route path={`${match.path}/users`} component={UsersContainer} exact/>
<Route path={`${match.path}/backups`} component={BackupContainer} exact/>
<Route path={`${match.path}/settings`} component={SettingsContainer} exact/>
<Route path={'*'} component={NotFound}/>
</Switch>
</TransitionRouter>
</>
}
</>
}
</React.Fragment>
);
};
export default (props: RouteComponentProps<any>) => (
<ServerContext.Provider>
<ServerRouter {...props}/>
</ServerContext.Provider>
);