2023-08-04 23:59:33 +03:00
|
|
|
import { Message } from '@shlinkio/shlink-frontend-kit';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { FC } from 'react';
|
|
|
|
import { useEffect } from 'react';
|
2022-02-06 22:07:18 +03:00
|
|
|
import { useParams } from 'react-router-dom';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { NoMenuLayout } from '../../common/NoMenuLayout';
|
2023-09-05 10:08:42 +03:00
|
|
|
import type { FCWithDeps } from '../../container/utils';
|
|
|
|
import { useDependencies } from '../../container/utils';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { SelectedServer } from '../data';
|
|
|
|
import { isNotFoundServer } from '../data';
|
2020-08-29 14:51:53 +03:00
|
|
|
|
2023-09-05 10:08:42 +03:00
|
|
|
export type WithSelectedServerProps = {
|
2020-08-29 14:51:53 +03:00
|
|
|
selectServer: (serverId: string) => void;
|
|
|
|
selectedServer: SelectedServer;
|
2023-09-05 10:08:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
type WithSelectedServerPropsDeps = {
|
|
|
|
ServerError: FC;
|
|
|
|
};
|
2020-08-29 14:51:53 +03:00
|
|
|
|
2023-09-05 10:08:42 +03:00
|
|
|
export function withSelectedServer<T = {}>(
|
|
|
|
WrappedComponent: FCWithDeps<WithSelectedServerProps & T, WithSelectedServerPropsDeps>,
|
|
|
|
) {
|
|
|
|
const ComponentWrapper: FCWithDeps<WithSelectedServerProps & T, WithSelectedServerPropsDeps> = (props) => {
|
|
|
|
const { ServerError } = useDependencies(ComponentWrapper);
|
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);
|
2023-09-02 20:48:23 +03:00
|
|
|
}, [params.serverId, selectServer]);
|
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} />;
|
|
|
|
};
|
2023-09-05 10:08:42 +03:00
|
|
|
return ComponentWrapper;
|
2020-08-29 21:20:45 +03:00
|
|
|
}
|