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();
|
|
|
|
|
2020-10-04 02:36:26 +00:00
|
|
|
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
2020-04-17 18:07:32 +00:00
|
|
|
const [ error, setError ] = useState('');
|
2020-08-26 04:25:31 +00:00
|
|
|
|
|
|
|
const id = ServerContext.useStoreState(state => state.server.data?.id);
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data?.uuid);
|
2021-01-31 02:01:32 +00:00
|
|
|
const inConflictState = ServerContext.useStoreState(state => state.server.inConflictState);
|
2020-11-08 21:18:15 +00:00
|
|
|
const serverId = ServerContext.useStoreState(state => state.server.data?.internalId);
|
2019-07-10 04:25:57 +00:00
|
|
|
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(/^\/+/, '')}`;
|
|
|
|
};
|
|
|
|
|
2020-04-12 23:19:43 +00:00
|
|
|
useEffect(() => () => {
|
|
|
|
clearServerState();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-04-17 18:07:32 +00:00
|
|
|
setError('');
|
2020-12-16 16:34:47 +00:00
|
|
|
|
2020-04-17 18:07:32 +00:00
|
|
|
getServer(match.params.id)
|
|
|
|
.catch(error => {
|
2021-01-31 02:01:32 +00:00
|
|
|
console.error(error);
|
|
|
|
setError(httpErrorToHuman(error));
|
2020-04-17 18:07:32 +00:00
|
|
|
});
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2020-04-12 23:19:43 +00:00
|
|
|
return () => {
|
|
|
|
clearServerState();
|
|
|
|
};
|
|
|
|
}, [ 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'}>
|
2019-06-29 23:14:32 +00:00
|
|
|
<NavigationBar/>
|
2020-08-26 04:25:31 +00:00
|
|
|
{(!uuid || !id) ?
|
2020-04-26 20:21:39 +00:00
|
|
|
error ?
|
|
|
|
<ServerError message={error}/>
|
2020-04-17 18:07:32 +00:00
|
|
|
:
|
2020-07-05 01:19:46 +00:00
|
|
|
<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-12 15:36:55 +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}>
|
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
|
|
|
))}
|
2020-11-02 05:14:02 +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'}>
|
|
|
|
<FontAwesomeIcon icon={faExternalLinkAlt}/>
|
|
|
|
</a>
|
2020-11-02 05:14:02 +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>
|
2020-07-30 05:02:00 +00:00
|
|
|
<InstallListener/>
|
2020-12-16 23:55:44 +00:00
|
|
|
<TransferListener/>
|
2020-07-30 05:02:00 +00:00
|
|
|
<WebsocketHandler/>
|
2021-01-31 02:01:32 +00:00
|
|
|
{(inConflictState && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${id}`)))) ?
|
|
|
|
<ConflictStateRenderer/>
|
2020-04-26 20:21:39 +00:00
|
|
|
:
|
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>
|
|
|
|
<Component/>
|
|
|
|
</Spinner.Suspense>
|
|
|
|
</PermissionRoute>
|
|
|
|
))}
|
2020-04-26 20:21:39 +00:00
|
|
|
<Route path={'*'} component={NotFound}/>
|
|
|
|
</Switch>
|
|
|
|
</TransitionRouter>
|
2020-10-23 04:18:46 +00:00
|
|
|
</ErrorBoundary>
|
2020-04-26 20:21:39 +00:00
|
|
|
}
|
2020-04-12 23:19:43 +00:00
|
|
|
</>
|
|
|
|
}
|
2019-06-29 23:14:32 +00:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
};
|