import React, { useEffect, useState } from 'react'; import { NavLink, Route, RouteComponentProps, Switch } from 'react-router-dom'; import NavigationBar from '@/components/NavigationBar'; import ServerConsole from '@/components/server/ServerConsole'; import TransitionRouter from '@/TransitionRouter'; import WebsocketHandler from '@/components/server/WebsocketHandler'; import { ServerContext } from '@/state/server'; import DatabasesContainer from '@/components/server/databases/DatabasesContainer'; import FileManagerContainer from '@/components/server/files/FileManagerContainer'; import { CSSTransition } from 'react-transition-group'; import SuspenseSpinner from '@/components/elements/SuspenseSpinner'; import FileEditContainer from '@/components/server/files/FileEditContainer'; import SettingsContainer from '@/components/server/settings/SettingsContainer'; import ScheduleContainer from '@/components/server/schedules/ScheduleContainer'; import ScheduleEditContainer from '@/components/server/schedules/ScheduleEditContainer'; import UsersContainer from '@/components/server/users/UsersContainer'; import Can from '@/components/elements/Can'; import BackupContainer from '@/components/server/backups/BackupContainer'; import Spinner from '@/components/elements/Spinner'; import ServerError from '@/components/screens/ServerError'; import { httpErrorToHuman } from '@/api/http'; import NotFound from '@/components/screens/NotFound'; import { useStoreState } from 'easy-peasy'; import useServer from '@/plugins/useServer'; import ScreenBlock from '@/components/screens/ScreenBlock'; const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) => { const { rootAdmin } = useStoreState(state => state.user.data!); const [ error, setError ] = useState(''); const [ installing, setInstalling ] = useState(false); const server = useServer(); const getServer = ServerContext.useStoreActions(actions => actions.server.getServer); const clearServerState = ServerContext.useStoreActions(actions => actions.clearServerState); useEffect(() => () => { clearServerState(); }, []); useEffect(() => { setInstalling(server?.isInstalling !== false); }, [ server?.isInstalling ]); useEffect(() => { setError(''); setInstalling(false); getServer(match.params.id) .catch(error => { if (error.response?.status === 409) { setInstalling(true); } else { console.error(error); setError(httpErrorToHuman(error)); } }); return () => { clearServerState(); }; }, [ match.params.id ]); return ( {!server ? error ? : : <> Console File Manager Databases Schedules Users Backups Settings {(installing && (!rootAdmin || (rootAdmin && !location.pathname.endsWith(`/server/${server.id}`)))) ? : <> ( )} exact /> > } > } ); }; export default (props: RouteComponentProps) => ( );