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

132 lines
5.6 KiB
TypeScript
Raw Permalink Normal View History

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';
import NavigationBar from '@/components/NavigationBar';
import WebsocketHandler from '@/components/server/WebsocketHandler';
import { ServerContext } from '@/state/server';
import Can from '@/components/elements/Can';
import Spinner from '@/components/elements/Spinner';
import { NotFound, ServerError } from '@/components/elements/ScreenBlock';
import { httpErrorToHuman } from '@/api/http';
import { useStoreState } from 'easy-peasy';
import SubNavigation from '@/components/elements/SubNavigation';
2020-07-30 05:02:00 +00:00
import InstallListener from '@/components/server/InstallListener';
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';
import { useLocation } from 'react-router';
import ConflictStateRenderer from '@/components/server/ConflictStateRenderer';
import PermissionRoute from '@/components/elements/PermissionRoute';
import routes from '@/routers/routes';
2022-11-25 20:25:03 +00:00
function ServerRouter() {
const params = useParams<'id'>();
const location = useLocation();
2022-11-25 20:25:03 +00:00
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
const [error, setError] = useState('');
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);
useEffect(
() => () => {
clearServerState();
},
2022-11-25 20:25:03 +00:00
[],
);
useEffect(() => {
setError('');
2022-11-25 20:25:03 +00:00
if (params.id === undefined) {
return;
}
getServer(params.id).catch(error => {
console.error(error);
setError(httpErrorToHuman(error));
});
return () => {
clearServerState();
};
2022-11-25 20:25:03 +00:00
}, [params.id]);
return (
2022-11-25 20:25:03 +00:00
<Fragment key={'server-router'}>
<NavigationBar />
{!uuid || !id ? (
error ? (
<ServerError message={error} />
) : (
<Spinner size="large" centered />
)
) : (
<>
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>
<NavLink to={`/server/${id}/${route.path ?? ''}`.replace(/\/$/, '')} end>
{route.name}
</NavLink>
2022-11-25 20:25:03 +00:00
</Can>
) : (
<NavLink
key={route.path}
to={`/server/${id}/${route.path ?? ''}`.replace(/\/$/, '')}
end
>
2022-11-25 20:25:03 +00:00
{route.name}
</NavLink>
),
)}
2022-11-25 20:25:03 +00:00
{rootAdmin && (
<NavLink to={`/admin/servers/${serverId}`}>
2022-11-25 20:25:03 +00:00
<FontAwesomeIcon icon={faExternalLinkAlt} />
</NavLink>
2022-11-25 20:25:03 +00:00
)}
</div>
</SubNavigation>
<InstallListener />
<TransferListener />
<WebsocketHandler />
2022-11-25 20:25:03 +00:00
{inConflictState && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${id}/`))) ? (
<ConflictStateRenderer />
) : (
<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>
</ErrorBoundary>
)}
</>
)}
2022-11-25 20:25:03 +00:00
</Fragment>
);
2022-11-25 20:25:03 +00:00
}
export default ServerRouter;