Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/routers/ServerRouter.tsx
7458 views
1
import TransferListener from '@/components/server/TransferListener';
2
import React, { useEffect, useState } from 'react';
3
import { NavLink, Route, Switch, useRouteMatch } from 'react-router-dom';
4
import NavigationBar from '@/components/NavigationBar';
5
import TransitionRouter from '@/TransitionRouter';
6
import WebsocketHandler from '@/components/server/WebsocketHandler';
7
import { ServerContext } from '@/state/server';
8
import { CSSTransition } from 'react-transition-group';
9
import Can from '@/components/elements/Can';
10
import Spinner from '@/components/elements/Spinner';
11
import { NotFound, ServerError } from '@/components/elements/ScreenBlock';
12
import { httpErrorToHuman } from '@/api/http';
13
import { useStoreState } from 'easy-peasy';
14
import SubNavigation from '@/components/elements/SubNavigation';
15
import InstallListener from '@/components/server/InstallListener';
16
import ErrorBoundary from '@/components/elements/ErrorBoundary';
17
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
18
import { faExternalLinkAlt } from '@fortawesome/free-solid-svg-icons';
19
import { useLocation } from 'react-router';
20
import ConflictStateRenderer from '@/components/server/ConflictStateRenderer';
21
import PermissionRoute from '@/components/elements/PermissionRoute';
22
import routes from '@/routers/routes';
23
24
export default () => {
25
const match = useRouteMatch<{ id: string }>();
26
const location = useLocation();
27
28
const rootAdmin = useStoreState((state) => state.user.data!.rootAdmin);
29
const [error, setError] = useState('');
30
31
const id = ServerContext.useStoreState((state) => state.server.data?.id);
32
const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid);
33
const inConflictState = ServerContext.useStoreState((state) => state.server.inConflictState);
34
const serverId = ServerContext.useStoreState((state) => state.server.data?.internalId);
35
const getServer = ServerContext.useStoreActions((actions) => actions.server.getServer);
36
const clearServerState = ServerContext.useStoreActions((actions) => actions.clearServerState);
37
38
const to = (value: string, url = false) => {
39
if (value === '/') {
40
return url ? match.url : match.path;
41
}
42
return `${(url ? match.url : match.path).replace(/\/*$/, '')}/${value.replace(/^\/+/, '')}`;
43
};
44
45
useEffect(
46
() => () => {
47
clearServerState();
48
},
49
[]
50
);
51
52
useEffect(() => {
53
setError('');
54
55
getServer(match.params.id).catch((error) => {
56
console.error(error);
57
setError(httpErrorToHuman(error));
58
});
59
60
return () => {
61
clearServerState();
62
};
63
}, [match.params.id]);
64
65
return (
66
<React.Fragment key={'server-router'}>
67
<NavigationBar />
68
{!uuid || !id ? (
69
error ? (
70
<ServerError message={error} />
71
) : (
72
<Spinner size={'large'} centered />
73
)
74
) : (
75
<>
76
<CSSTransition timeout={150} classNames={'fade'} appear in>
77
<SubNavigation>
78
<div>
79
{routes.server
80
.filter((route) => !!route.name)
81
.map((route) =>
82
route.permission ? (
83
<Can key={route.path} action={route.permission} matchAny>
84
<NavLink to={to(route.path, true)} exact={route.exact}>
85
{route.name}
86
</NavLink>
87
</Can>
88
) : (
89
<NavLink key={route.path} to={to(route.path, true)} exact={route.exact}>
90
{route.name}
91
</NavLink>
92
)
93
)}
94
{rootAdmin && (
95
// eslint-disable-next-line react/jsx-no-target-blank
96
<a href={`/admin/servers/view/${serverId}`} target={'_blank'}>
97
<FontAwesomeIcon icon={faExternalLinkAlt} />
98
</a>
99
)}
100
</div>
101
</SubNavigation>
102
</CSSTransition>
103
<InstallListener />
104
<TransferListener />
105
<WebsocketHandler />
106
{inConflictState && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${id}`))) ? (
107
<ConflictStateRenderer />
108
) : (
109
<ErrorBoundary>
110
<TransitionRouter>
111
<Switch location={location}>
112
{routes.server.map(({ path, permission, component: Component }) => (
113
<PermissionRoute key={path} permission={permission} path={to(path)} exact>
114
<Spinner.Suspense>
115
<Component />
116
</Spinner.Suspense>
117
</PermissionRoute>
118
))}
119
<Route path={'*'} component={NotFound} />
120
</Switch>
121
</TransitionRouter>
122
</ErrorBoundary>
123
)}
124
</>
125
)}
126
</React.Fragment>
127
);
128
};
129
130