2020-12-16 16:55:44 -07:00
import TransferListener from '@/components/server/TransferListener' ;
2020-04-17 11:07:32 -07:00
import React , { useEffect , useState } from 'react' ;
2020-10-12 20:59:11 -07:00
import { NavLink , Route , RouteComponentProps , Switch } from 'react-router-dom' ;
2019-06-28 22:17:29 -07:00
import NavigationBar from '@/components/NavigationBar' ;
import ServerConsole from '@/components/server/ServerConsole' ;
import TransitionRouter from '@/TransitionRouter' ;
2019-06-29 16:14:32 -07:00
import WebsocketHandler from '@/components/server/WebsocketHandler' ;
2019-07-09 21:25:57 -07:00
import { ServerContext } from '@/state/server' ;
2019-07-09 22:00:29 -07:00
import DatabasesContainer from '@/components/server/databases/DatabasesContainer' ;
2019-07-27 20:23:44 -07:00
import FileManagerContainer from '@/components/server/files/FileManagerContainer' ;
import { CSSTransition } from 'react-transition-group' ;
2019-10-19 15:31:02 -07:00
import FileEditContainer from '@/components/server/files/FileEditContainer' ;
2019-12-07 15:58:37 -08:00
import SettingsContainer from '@/components/server/settings/SettingsContainer' ;
2020-02-08 15:23:08 -08:00
import ScheduleContainer from '@/components/server/schedules/ScheduleContainer' ;
2020-03-17 23:33:53 -07:00
import ScheduleEditContainer from '@/components/server/schedules/ScheduleEditContainer' ;
2020-03-25 21:58:37 -07:00
import UsersContainer from '@/components/server/users/UsersContainer' ;
2020-03-28 17:25:04 -07:00
import Can from '@/components/elements/Can' ;
2020-04-04 10:59:25 -07:00
import BackupContainer from '@/components/server/backups/BackupContainer' ;
2020-04-12 16:19:43 -07:00
import Spinner from '@/components/elements/Spinner' ;
2021-01-30 18:01:32 -08:00
import ScreenBlock , { NotFound , ServerError } from '@/components/elements/ScreenBlock' ;
2020-04-17 11:07:32 -07:00
import { httpErrorToHuman } from '@/api/http' ;
2020-04-26 13:21:39 -07:00
import { useStoreState } from 'easy-peasy' ;
2020-07-04 15:40:41 -07:00
import SubNavigation from '@/components/elements/SubNavigation' ;
2020-07-09 19:56:46 -07:00
import NetworkContainer from '@/components/server/network/NetworkContainer' ;
2020-07-29 22:02:00 -07:00
import InstallListener from '@/components/server/InstallListener' ;
2020-08-22 15:43:28 -07:00
import StartupContainer from '@/components/server/startup/StartupContainer' ;
2020-10-22 21:18:46 -07:00
import ErrorBoundary from '@/components/elements/ErrorBoundary' ;
2020-11-02 00:14:02 -05:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
import { faExternalLinkAlt } from '@fortawesome/free-solid-svg-icons' ;
2021-01-01 17:21:13 +00:00
import RequireServerPermission from '@/hoc/RequireServerPermission' ;
2021-01-30 18:01:32 -08:00
import ServerInstallSvg from '@/assets/images/server_installing.svg' ;
import ServerRestoreSvg from '@/assets/images/server_restore.svg' ;
2021-02-17 20:58:47 -08:00
import ServerErrorSvg from '@/assets/images/server_error.svg' ;
2021-01-30 18:01:32 -08:00
const ConflictStateRenderer = () => {
const status = ServerContext . useStoreState ( state => state . server . data ? . status || null );
const isTransferring = ServerContext . useStoreState ( state => state . server . data ? . isTransferring || false );
return (
status === 'installing' || status === 'install_failed' ?
< ScreenBlock
title = { 'Running Installer' }
image = { ServerInstallSvg }
message = { 'Your server should be ready soon, please try again in a few minutes.' }
/>
:
2021-02-17 20:58:47 -08:00
status === 'suspended' ?
< ScreenBlock
title = { 'Server Suspended' }
image = { ServerErrorSvg }
message = { 'This server is suspended and cannot be accessed.' }
/>
:
< ScreenBlock
title = { isTransferring ? 'Transferring' : 'Restoring from Backup' }
image = { ServerRestoreSvg }
message = { isTransferring ? 'Your server is being transfered to a new node, please check back later.' : 'Your server is currently being restored from a backup, please check back in a few minutes.' }
/>
2021-01-30 18:01:32 -08:00
);
};
2019-06-28 22:17:29 -07:00
2019-07-09 21:25:57 -07:00
const ServerRouter = ({ match , location } : RouteComponentProps < { id : string } > ) => {
2020-10-03 19:36:26 -07:00
const rootAdmin = useStoreState ( state => state . user . data ! . rootAdmin );
2020-04-17 11:07:32 -07:00
const [ error , setError ] = useState ( '' );
2020-08-25 21:25:31 -07:00
const id = ServerContext . useStoreState ( state => state . server . data ? . id );
const uuid = ServerContext . useStoreState ( state => state . server . data ? . uuid );
2021-01-30 18:01:32 -08:00
const inConflictState = ServerContext . useStoreState ( state => state . server . inConflictState );
2020-11-08 13:18:15 -08:00
const serverId = ServerContext . useStoreState ( state => state . server . data ? . internalId );
2019-07-09 21:25:57 -07:00
const getServer = ServerContext . useStoreActions ( actions => actions . server . getServer );
const clearServerState = ServerContext . useStoreActions ( actions => actions . clearServerState );
2019-06-29 16:14:32 -07:00
2020-04-12 16:19:43 -07:00
useEffect (() => () => {
clearServerState ();
}, []);
useEffect (() => {
2020-04-17 11:07:32 -07:00
setError ( '' );
2020-12-16 09:34:47 -07:00
2020-04-17 11:07:32 -07:00
getServer ( match . params . id )
. catch ( error => {
2021-01-30 18:01:32 -08:00
console . error ( error );
setError ( httpErrorToHuman ( error ));
2020-04-17 11:07:32 -07:00
});
2019-06-29 16:14:32 -07:00
2020-04-12 16:19:43 -07:00
return () => {
clearServerState ();
};
}, [ match . params . id ]);
2019-06-29 16:14:32 -07:00
return (
2020-04-10 10:11:15 -07:00
< React.Fragment key = { 'server-router' }>
2019-06-29 16:14:32 -07:00
< NavigationBar />
2020-08-25 21:25:31 -07:00
{( ! uuid || ! id ) ?
2020-04-26 13:21:39 -07:00
error ?
< ServerError message = { error }/>
2020-04-17 11:07:32 -07:00
:
2020-07-04 18:19:46 -07:00
< Spinner size = { 'large' } centered />
2020-04-12 16:19:43 -07:00
:
<>
2020-07-05 13:56:04 -07:00
< CSSTransition timeout = { 150 } classNames = { 'fade' } appear in >
2020-07-04 15:40:41 -07:00
< SubNavigation >
< div >
2020-04-12 16:19:43 -07:00
< NavLink to = { ` ${ match . url } ` } exact > Console </ NavLink >
< Can action = { 'file.*' }>
< NavLink to = { ` ${ match . url } /files` }> File Manager </ NavLink >
</ Can >
< Can action = { 'database.*' }>
< NavLink to = { ` ${ match . url } /databases` }> Databases </ NavLink >
</ Can >
< Can action = { 'schedule.*' }>
< NavLink to = { ` ${ match . url } /schedules` }> Schedules </ NavLink >
</ Can >
< Can action = { 'user.*' }>
< NavLink to = { ` ${ match . url } /users` }> Users </ NavLink >
</ Can >
< Can action = { 'backup.*' }>
< NavLink to = { ` ${ match . url } /backups` }> Backups </ NavLink >
</ Can >
2020-11-02 00:49:07 -05:00
< Can action = { 'allocation.*' }>
2020-07-09 19:56:46 -07:00
< NavLink to = { ` ${ match . url } /network` }> Network </ NavLink >
</ Can >
2020-08-22 15:43:28 -07:00
< Can action = { 'startup.*' }>
< NavLink to = { ` ${ match . url } /startup` }> Startup </ NavLink >
</ Can >
2020-07-04 18:30:50 -07:00
< Can action = {[ 'settings.*' , 'file.sftp' ]} matchAny >
2020-04-12 16:19:43 -07:00
< NavLink to = { ` ${ match . url } /settings` }> Settings </ NavLink >
</ Can >
2020-11-02 00:14:02 -05:00
{ rootAdmin &&
2020-11-08 13:18:15 -08:00
< a href = { '/admin/servers/view/' + serverId } rel = "noreferrer" target = { '_blank' }>
< FontAwesomeIcon icon = { faExternalLinkAlt }/>
</ a >
2020-11-02 00:14:02 -05:00
}
2020-04-12 16:19:43 -07:00
</ div >
2020-07-04 15:40:41 -07:00
</ SubNavigation >
2020-04-12 16:19:43 -07:00
</ CSSTransition >
2020-07-29 22:02:00 -07:00
< InstallListener />
2020-12-16 16:55:44 -07:00
< TransferListener />
2020-07-29 22:02:00 -07:00
< WebsocketHandler />
2021-01-30 18:01:32 -08:00
{( inConflictState && ( ! rootAdmin || ( rootAdmin && ! location . pathname . endsWith ( `/server/ ${ id } ` )))) ?
< ConflictStateRenderer />
2020-04-26 13:21:39 -07:00
:
2020-10-22 21:18:46 -07:00
< ErrorBoundary >
2020-04-26 13:21:39 -07:00
< TransitionRouter >
< Switch location = { location }>
< Route path = { ` ${ match . path } ` } component = { ServerConsole } exact />
2021-01-01 17:21:13 +00:00
< Route path = { ` ${ match . path } /files` } exact >
< RequireServerPermission permissions = { 'file.*' }>
2021-01-30 18:01:32 -08:00
< FileManagerContainer />
2021-01-01 17:21:13 +00:00
</ RequireServerPermission >
</ Route >
< Route path = { ` ${ match . path } /files/:action(edit|new)` } exact >
2021-05-16 12:35:49 -07:00
< Spinner.Suspense >
2021-01-30 18:01:32 -08:00
< FileEditContainer />
2021-05-16 12:35:49 -07:00
</ Spinner.Suspense >
2021-01-01 17:21:13 +00:00
</ Route >
< Route path = { ` ${ match . path } /databases` } exact >
< RequireServerPermission permissions = { 'database.*' }>
2021-01-30 18:01:32 -08:00
< DatabasesContainer />
2021-01-01 17:21:13 +00:00
</ RequireServerPermission >
</ Route >
< Route path = { ` ${ match . path } /schedules` } exact >
< RequireServerPermission permissions = { 'schedule.*' }>
2021-01-30 18:01:32 -08:00
< ScheduleContainer />
2021-01-01 17:21:13 +00:00
</ RequireServerPermission >
</ Route >
< Route path = { ` ${ match . path } /schedules/:id` } exact >
< ScheduleEditContainer />
</ Route >
< Route path = { ` ${ match . path } /users` } exact >
< RequireServerPermission permissions = { 'user.*' }>
2021-01-30 18:01:32 -08:00
< UsersContainer />
2021-01-01 17:21:13 +00:00
</ RequireServerPermission >
</ Route >
< Route path = { ` ${ match . path } /backups` } exact >
< RequireServerPermission permissions = { 'backup.*' }>
2021-01-30 18:01:32 -08:00
< BackupContainer />
2021-01-01 17:21:13 +00:00
</ RequireServerPermission >
</ Route >
< Route path = { ` ${ match . path } /network` } exact >
< RequireServerPermission permissions = { 'allocation.*' }>
2021-01-30 18:01:32 -08:00
< NetworkContainer />
2021-01-01 17:21:13 +00:00
</ RequireServerPermission >
</ Route >
2020-08-25 21:25:31 -07:00
< Route path = { ` ${ match . path } /startup` } component = { StartupContainer } exact />
2020-04-26 13:21:39 -07:00
< Route path = { ` ${ match . path } /settings` } component = { SettingsContainer } exact />
< Route path = { '*' } component = { NotFound }/>
</ Switch >
</ TransitionRouter >
2020-10-22 21:18:46 -07:00
</ ErrorBoundary >
2020-04-26 13:21:39 -07:00
}
2020-04-12 16:19:43 -07:00
</>
}
2019-06-29 16:14:32 -07:00
</ React.Fragment >
);
};
2019-07-09 21:25:57 -07:00
export default ( props : RouteComponentProps < any >) => (
< ServerContext.Provider >
< ServerRouter { ...props }/>
</ ServerContext.Provider >
);