shlink-web-client/src/servers/helpers/withSelectedServer.tsx

38 lines
1.1 KiB
TypeScript
Raw Normal View History

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-07-31 22:36:44 +03:00
import { Message } from '../../../shlink-frontend-kit/src';
2023-02-18 13:11:01 +03:00
import { NoMenuLayout } from '../../common/NoMenuLayout';
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
2022-02-06 22:07:18 +03:00
interface WithSelectedServerProps {
2020-08-29 14:51:53 +03:00
selectServer: (serverId: string) => void;
selectedServer: SelectedServer;
}
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
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
if (!selectedServer) {
return (
<NoMenuLayout>
<Message loading />
</NoMenuLayout>
);
}
2020-08-29 14:51:53 +03:00
if (isNotFoundServer(selectedServer)) {
return <ServerError />;
}
2020-08-29 14:51:53 +03:00
return <WrappedComponent {...props} />;
};
}