2020-11-14 00:44:26 +03:00
|
|
|
import { FC, useEffect } from 'react';
|
2022-02-06 22:07:18 +03:00
|
|
|
import { useParams } from 'react-router-dom';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { Message } from '../../utils/Message';
|
2020-08-29 19:51:03 +03:00
|
|
|
import { isNotFoundServer, SelectedServer } from '../data';
|
2021-12-30 12:02:31 +03:00
|
|
|
import { NoMenuLayout } from '../../common/NoMenuLayout';
|
2020-08-29 14:51:53 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
interface WithSelectedServerProps {
|
2020-08-29 14:51:53 +03:00
|
|
|
selectServer: (serverId: string) => void;
|
|
|
|
selectedServer: SelectedServer;
|
|
|
|
}
|
|
|
|
|
2020-08-29 21:20:45 +03:00
|
|
|
export function withSelectedServer<T = {}>(WrappedComponent: FC<WithSelectedServerProps & T>, ServerError: FC) {
|
|
|
|
return (props: WithSelectedServerProps & T) => {
|
2022-02-06 22:07:18 +03:00
|
|
|
const params = useParams<{ serverId: string }>();
|
|
|
|
const { selectServer, selectedServer } = props;
|
2020-08-29 14:51:53 +03:00
|
|
|
|
2020-08-29 21:20:45 +03:00
|
|
|
useEffect(() => {
|
2022-02-06 22:07:18 +03:00
|
|
|
params.serverId && selectServer(params.serverId);
|
2022-03-26 14:17:42 +03:00
|
|
|
}, [params.serverId]);
|
2020-08-29 14:51:53 +03:00
|
|
|
|
2020-08-29 21:20:45 +03:00
|
|
|
if (!selectedServer) {
|
2020-09-06 13:47:14 +03:00
|
|
|
return (
|
2020-12-12 14:04:20 +03:00
|
|
|
<NoMenuLayout>
|
2020-12-21 11:22:13 +03:00
|
|
|
<Message loading />
|
2020-12-12 14:04:20 +03:00
|
|
|
</NoMenuLayout>
|
2020-09-06 13:47:14 +03:00
|
|
|
);
|
2020-08-29 21:20:45 +03:00
|
|
|
}
|
2020-08-29 14:51:53 +03:00
|
|
|
|
2020-08-29 21:20:45 +03:00
|
|
|
if (isNotFoundServer(selectedServer)) {
|
|
|
|
return <ServerError />;
|
|
|
|
}
|
2020-08-29 14:51:53 +03:00
|
|
|
|
2020-08-29 21:20:45 +03:00
|
|
|
return <WrappedComponent {...props} />;
|
|
|
|
};
|
|
|
|
}
|