2020-12-16 23:55:44 +00:00
|
|
|
import TransferListener from '@/components/server/TransferListener';
|
2020-04-17 18:07:32 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-05-28 17:32:35 +00:00
|
|
|
import { NavLink, Route, Switch, useRouteMatch } from 'react-router-dom';
|
2019-06-29 05:17:29 +00:00
|
|
|
import NavigationBar from '@/components/NavigationBar';
|
|
|
|
import TransitionRouter from '@/TransitionRouter';
|
2019-06-29 23:14:32 +00:00
|
|
|
import WebsocketHandler from '@/components/server/WebsocketHandler';
|
2019-07-10 04:25:57 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2019-07-28 03:23:44 +00:00
|
|
|
import { CSSTransition } from 'react-transition-group';
|
2020-03-29 00:25:04 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-04-12 23:19:43 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2022-06-12 15:36:55 +00:00
|
|
|
import { NotFound, ServerError } from '@/components/elements/ScreenBlock';
|
2020-04-17 18:07:32 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-04-26 20:21:39 +00:00
|
|
|
import { useStoreState } from 'easy-peasy';
|
2020-07-04 22:40:41 +00:00
|
|
|
import SubNavigation from '@/components/elements/SubNavigation';
|
2020-07-30 05:02:00 +00:00
|
|
|
import InstallListener from '@/components/server/InstallListener';
|
2020-10-23 04:18:46 +00:00
|
|
|
import ErrorBoundary from '@/components/elements/ErrorBoundary';
|
2020-11-02 05:14:02 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faExternalLinkAlt } from '@fortawesome/free-solid-svg-icons';
|
2022-05-28 17:32:35 +00:00
|
|
|
import { useLocation } from 'react-router';
|
2022-06-12 15:36:55 +00:00
|
|
|
import ConflictStateRenderer from '@/components/server/ConflictStateRenderer';
|
|
|
|
import PermissionRoute from '@/components/elements/PermissionRoute';
|
|
|
|
import routes from '@/routers/routes';
|
2019-06-29 05:17:29 +00:00
|
|
|
|
2022-05-28 17:32:35 +00:00
|
|
|
export default () => {
|
|
|
|
const match = useRouteMatch<{ id: string }>();
|
|
|
|
const location = useLocation();
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const rootAdmin = useStoreState((state) => state.user.data!.rootAdmin);
|
|
|
|
const [error, setError] = useState('');
|
2020-08-26 04:25:31 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const id = ServerContext.useStoreState((state) => state.server.data?.id);
|
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid);
|
|
|
|
const inConflictState = ServerContext.useStoreState((state) => state.server.inConflictState);
|
|
|
|
const serverId = ServerContext.useStoreState((state) => state.server.data?.internalId);
|
|
|
|
const getServer = ServerContext.useStoreActions((actions) => actions.server.getServer);
|
|
|
|
const clearServerState = ServerContext.useStoreActions((actions) => actions.clearServerState);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2022-06-12 15:36:55 +00:00
|
|
|
const to = (value: string, url = false) => {
|
|
|
|
return `${(url ? match.url : match.path).replace(/\/*$/, '')}/${value.replace(/^\/+/, '')}`;
|
|
|
|
};
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
clearServerState();
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2020-04-12 23:19:43 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-04-17 18:07:32 +00:00
|
|
|
setError('');
|
2020-12-16 16:34:47 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
getServer(match.params.id).catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
setError(httpErrorToHuman(error));
|
|
|
|
});
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2020-04-12 23:19:43 +00:00
|
|
|
return () => {
|
|
|
|
clearServerState();
|
|
|
|
};
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [match.params.id]);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
return (
|
2020-04-10 17:11:15 +00:00
|
|
|
<React.Fragment key={'server-router'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<NavigationBar />
|
|
|
|
{!uuid || !id ? (
|
|
|
|
error ? (
|
|
|
|
<ServerError message={error} />
|
|
|
|
) : (
|
|
|
|
<Spinner size={'large'} centered />
|
|
|
|
)
|
|
|
|
) : (
|
2020-04-12 23:19:43 +00:00
|
|
|
<>
|
2020-07-05 20:56:04 +00:00
|
|
|
<CSSTransition timeout={150} classNames={'fade'} appear in>
|
2020-07-04 22:40:41 +00:00
|
|
|
<SubNavigation>
|
|
|
|
<div>
|
2022-06-26 19:13:52 +00:00
|
|
|
{routes.server
|
|
|
|
.filter((route) => !!route.name)
|
|
|
|
.map((route) =>
|
|
|
|
route.permission ? (
|
|
|
|
<Can key={route.path} action={route.permission} matchAny>
|
|
|
|
<NavLink to={to(route.path, true)} exact={route.exact}>
|
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
|
|
|
</Can>
|
|
|
|
) : (
|
|
|
|
<NavLink key={route.path} to={to(route.path, true)} exact={route.exact}>
|
2022-06-12 15:36:55 +00:00
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
2022-06-26 19:13:52 +00:00
|
|
|
)
|
|
|
|
)}
|
|
|
|
{rootAdmin && (
|
2022-06-12 15:36:55 +00:00
|
|
|
// eslint-disable-next-line react/jsx-no-target-blank
|
|
|
|
<a href={`/admin/servers/view/${serverId}`} target={'_blank'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon icon={faExternalLinkAlt} />
|
2022-06-12 15:36:55 +00:00
|
|
|
</a>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-04-12 23:19:43 +00:00
|
|
|
</div>
|
2020-07-04 22:40:41 +00:00
|
|
|
</SubNavigation>
|
2020-04-12 23:19:43 +00:00
|
|
|
</CSSTransition>
|
2022-06-26 19:13:52 +00:00
|
|
|
<InstallListener />
|
|
|
|
<TransferListener />
|
|
|
|
<WebsocketHandler />
|
|
|
|
{inConflictState && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${id}`))) ? (
|
|
|
|
<ConflictStateRenderer />
|
|
|
|
) : (
|
2020-10-23 04:18:46 +00:00
|
|
|
<ErrorBoundary>
|
2020-04-26 20:21:39 +00:00
|
|
|
<TransitionRouter>
|
|
|
|
<Switch location={location}>
|
2022-06-12 15:36:55 +00:00
|
|
|
{routes.server.map(({ path, permission, component: Component }) => (
|
|
|
|
<PermissionRoute key={path} permission={permission} path={to(path)} exact>
|
|
|
|
<Spinner.Suspense>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Component />
|
2022-06-12 15:36:55 +00:00
|
|
|
</Spinner.Suspense>
|
|
|
|
</PermissionRoute>
|
|
|
|
))}
|
2022-06-26 19:13:52 +00:00
|
|
|
<Route path={'*'} component={NotFound} />
|
2020-04-26 20:21:39 +00:00
|
|
|
</Switch>
|
|
|
|
</TransitionRouter>
|
2020-10-23 04:18:46 +00:00
|
|
|
</ErrorBoundary>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-04-12 23:19:43 +00:00
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2019-06-29 23:14:32 +00:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
};
|