2020-12-16 23:55:44 +00:00
|
|
|
import TransferListener from '@/components/server/TransferListener';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { Fragment, useEffect, useState } from 'react';
|
|
|
|
import { NavLink, Route, Routes, useParams } from 'react-router-dom';
|
2019-06-29 05:17:29 +00:00
|
|
|
import NavigationBar from '@/components/NavigationBar';
|
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';
|
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-11-25 20:25:03 +00:00
|
|
|
function ServerRouter() {
|
|
|
|
const params = useParams<'id'>();
|
2022-05-28 17:32:35 +00:00
|
|
|
const location = useLocation();
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
2022-06-26 19:13:52 +00:00
|
|
|
const [error, setError] = useState('');
|
2020-08-26 04:25:31 +00:00
|
|
|
|
2022-11-25 20:25:03 +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);
|
2022-06-12 15:36:55 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
clearServerState();
|
|
|
|
},
|
2022-11-25 20:25:03 +00:00
|
|
|
[],
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
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-11-25 20:25:03 +00:00
|
|
|
if (params.id === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getServer(params.id).catch(error => {
|
2022-06-26 19:13:52 +00:00
|
|
|
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-11-25 20:25:03 +00:00
|
|
|
}, [params.id]);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
return (
|
2022-11-25 20:25:03 +00:00
|
|
|
<Fragment key={'server-router'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<NavigationBar />
|
|
|
|
{!uuid || !id ? (
|
|
|
|
error ? (
|
|
|
|
<ServerError message={error} />
|
|
|
|
) : (
|
2022-12-21 21:27:50 +00:00
|
|
|
<Spinner size="large" centered />
|
2022-06-26 19:13:52 +00:00
|
|
|
)
|
|
|
|
) : (
|
2020-04-12 23:19:43 +00:00
|
|
|
<>
|
2022-11-25 20:25:03 +00:00
|
|
|
<SubNavigation>
|
|
|
|
<div>
|
|
|
|
{routes.server
|
|
|
|
.filter(route => route.path !== undefined)
|
|
|
|
.map(route =>
|
|
|
|
route.permission ? (
|
|
|
|
<Can key={route.path} action={route.permission} matchAny>
|
2022-12-21 21:27:50 +00:00
|
|
|
<NavLink to={`/server/${id}/${route.path ?? ''}`.replace(/\/$/, '')} end>
|
2022-06-12 15:36:55 +00:00
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
2022-11-25 20:25:03 +00:00
|
|
|
</Can>
|
|
|
|
) : (
|
2022-12-21 21:27:50 +00:00
|
|
|
<NavLink
|
|
|
|
key={route.path}
|
|
|
|
to={`/server/${id}/${route.path ?? ''}`.replace(/\/$/, '')}
|
|
|
|
end
|
|
|
|
>
|
2022-11-25 20:25:03 +00:00
|
|
|
{route.name}
|
|
|
|
</NavLink>
|
|
|
|
),
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2022-11-25 20:25:03 +00:00
|
|
|
{rootAdmin && (
|
2022-12-21 21:27:50 +00:00
|
|
|
<NavLink to={`/admin/servers/${serverId}`}>
|
2022-11-25 20:25:03 +00:00
|
|
|
<FontAwesomeIcon icon={faExternalLinkAlt} />
|
2022-12-21 21:27:50 +00:00
|
|
|
</NavLink>
|
2022-11-25 20:25:03 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</SubNavigation>
|
2022-06-26 19:13:52 +00:00
|
|
|
<InstallListener />
|
|
|
|
<TransferListener />
|
|
|
|
<WebsocketHandler />
|
2022-11-25 20:25:03 +00:00
|
|
|
{inConflictState && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${id}/`))) ? (
|
2022-06-26 19:13:52 +00:00
|
|
|
<ConflictStateRenderer />
|
|
|
|
) : (
|
2020-10-23 04:18:46 +00:00
|
|
|
<ErrorBoundary>
|
2022-11-25 20:25:03 +00:00
|
|
|
<Routes location={location}>
|
|
|
|
{routes.server.map(({ route, permission, component: Component }) => (
|
|
|
|
<Route
|
|
|
|
key={route}
|
|
|
|
path={route}
|
|
|
|
element={
|
|
|
|
<PermissionRoute permission={permission}>
|
|
|
|
<Spinner.Suspense>
|
|
|
|
<Component />
|
|
|
|
</Spinner.Suspense>
|
|
|
|
</PermissionRoute>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
|
|
|
<Route path="*" element={<NotFound />} />
|
|
|
|
</Routes>
|
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
|
|
|
)}
|
2022-11-25 20:25:03 +00:00
|
|
|
</Fragment>
|
2019-06-29 23:14:32 +00:00
|
|
|
);
|
2022-11-25 20:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ServerRouter;
|