forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerRouter.tsx
More file actions
128 lines (119 loc) · 5.81 KB
/
ServerRouter.tsx
File metadata and controls
128 lines (119 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import TransferListener from '@/components/server/TransferListener';
import React, { useEffect, useState } from 'react';
import { NavLink, Route, Switch, useRouteMatch } from 'react-router-dom';
import NavigationBar from '@/components/NavigationBar';
import TransitionRouter from '@/TransitionRouter';
import WebsocketHandler from '@/components/server/WebsocketHandler';
import { ServerContext } from '@/state/server';
import { CSSTransition } from 'react-transition-group';
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';
import InstallListener from '@/components/server/InstallListener';
import ErrorBoundary from '@/components/elements/ErrorBoundary';
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';
export default () => {
const match = useRouteMatch<{ id: string }>();
const location = useLocation();
const rootAdmin = useStoreState((state) => state.user.data!.rootAdmin);
const [error, setError] = useState('');
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);
const to = (value: string, url = false) => {
if (value === '/') {
return url ? match.url : match.path;
}
return `${(url ? match.url : match.path).replace(/\/*$/, '')}/${value.replace(/^\/+/, '')}`;
};
useEffect(
() => () => {
clearServerState();
},
[]
);
useEffect(() => {
setError('');
getServer(match.params.id).catch((error) => {
console.error(error);
setError(httpErrorToHuman(error));
});
return () => {
clearServerState();
};
}, [match.params.id]);
return (
<React.Fragment key={'server-router'}>
<NavigationBar />
{!uuid || !id ? (
error ? (
<ServerError message={error} />
) : (
<Spinner size={'large'} centered />
)
) : (
<>
<CSSTransition timeout={150} classNames={'fade'} appear in>
<SubNavigation>
<div>
{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>
)
)}
{rootAdmin && (
// eslint-disable-next-line react/jsx-no-target-blank
<a href={`/admin/servers/view/${serverId}`} target={'_blank'}>
<FontAwesomeIcon icon={faExternalLinkAlt} />
</a>
)}
</div>
</SubNavigation>
</CSSTransition>
<InstallListener />
<TransferListener />
<WebsocketHandler />
{inConflictState && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${id}`))) ? (
<ConflictStateRenderer />
) : (
<ErrorBoundary>
<TransitionRouter>
<Switch location={location}>
{routes.server.map(({ path, permission, component: Component }) => (
<PermissionRoute key={path} permission={permission} path={to(path)} exact>
<Spinner.Suspense>
<Component />
</Spinner.Suspense>
</PermissionRoute>
))}
<Route path={'*'} component={NotFound} />
</Switch>
</TransitionRouter>
</ErrorBoundary>
)}
</>
)}
</React.Fragment>
);
};