2022-11-25 20:25:03 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
2020-07-10 02:56:46 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2022-06-20 19:28:27 +00:00
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
2020-08-23 02:05:43 +00:00
|
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
2020-08-26 05:09:54 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2020-09-25 02:20:19 +00:00
|
|
|
import AllocationRow from '@/components/server/network/AllocationRow';
|
2020-09-28 15:50:34 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
2020-10-31 21:10:53 +00:00
|
|
|
import createServerAllocation from '@/api/server/network/createServerAllocation';
|
2020-09-28 15:50:34 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-10-31 21:10:53 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2020-11-01 04:22:44 +00:00
|
|
|
import getServerAllocations from '@/api/swr/getServerAllocations';
|
2020-11-01 05:30:03 +00:00
|
|
|
import isEqual from 'react-fast-compare';
|
2021-04-21 03:48:40 +00:00
|
|
|
import { useDeepCompareEffect } from '@/plugins/useDeepCompareEffect';
|
2020-07-10 02:56:46 +00:00
|
|
|
|
|
|
|
const NetworkContainer = () => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const allocationLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.allocations);
|
|
|
|
const allocations = ServerContext.useStoreState(state => state.server.data!.allocations, isEqual);
|
|
|
|
const setServerFromState = ServerContext.useStoreActions(actions => actions.server.setServerFromState);
|
2020-08-26 05:09:54 +00:00
|
|
|
|
2022-06-20 19:28:27 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlashKey('server:network');
|
2020-11-01 05:30:03 +00:00
|
|
|
const { data, error, mutate } = getServerAllocations();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-04-21 03:48:40 +00:00
|
|
|
mutate(allocations);
|
2020-11-01 05:30:03 +00:00
|
|
|
}, []);
|
2020-09-25 02:20:19 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-20 19:28:27 +00:00
|
|
|
clearAndAddHttpError(error);
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [error]);
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2021-04-21 03:48:40 +00:00
|
|
|
useDeepCompareEffect(() => {
|
|
|
|
if (!data) return;
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
setServerFromState(state => ({ ...state, allocations: data }));
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [data]);
|
2021-04-21 03:48:40 +00:00
|
|
|
|
2020-10-31 21:10:53 +00:00
|
|
|
const onCreateAllocation = () => {
|
2022-06-20 19:28:27 +00:00
|
|
|
clearFlashes();
|
2020-09-28 15:50:34 +00:00
|
|
|
|
2020-10-31 21:10:53 +00:00
|
|
|
setLoading(true);
|
|
|
|
createServerAllocation(uuid)
|
2022-11-25 20:25:03 +00:00
|
|
|
.then(allocation => {
|
|
|
|
setServerFromState(s => ({ ...s, allocations: s.allocations.concat(allocation) }));
|
2020-11-09 01:12:07 +00:00
|
|
|
return mutate(data?.concat(allocation), false);
|
|
|
|
})
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => clearAndAddHttpError(error))
|
2020-10-31 21:10:53 +00:00
|
|
|
.then(() => setLoading(false));
|
2020-09-28 15:50:34 +00:00
|
|
|
};
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
return (
|
2020-08-23 02:05:43 +00:00
|
|
|
<ServerContentBlock showFlashKey={'server:network'} title={'Network'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{!data ? (
|
|
|
|
<Spinner size={'large'} centered />
|
|
|
|
) : (
|
2020-10-31 21:10:53 +00:00
|
|
|
<>
|
2022-11-25 20:25:03 +00:00
|
|
|
{data.map(allocation => (
|
2022-06-26 19:13:52 +00:00
|
|
|
<AllocationRow key={`${allocation.ip}:${allocation.port}`} allocation={allocation} />
|
|
|
|
))}
|
|
|
|
{allocationLimit > 0 && (
|
2022-05-07 19:05:28 +00:00
|
|
|
<Can action={'allocation.create'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<SpinnerOverlay visible={loading} />
|
2022-05-07 19:05:28 +00:00
|
|
|
<div css={tw`mt-6 sm:flex items-center justify-end`}>
|
|
|
|
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
|
|
|
|
You are currently using {data.length} of {allocationLimit} allowed allocations for
|
|
|
|
this server.
|
|
|
|
</p>
|
2022-06-26 19:13:52 +00:00
|
|
|
{allocationLimit > data.length && (
|
2022-05-07 19:05:28 +00:00
|
|
|
<Button css={tw`w-full sm:w-auto`} color={'primary'} onClick={onCreateAllocation}>
|
|
|
|
Create Allocation
|
|
|
|
</Button>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2022-05-07 19:05:28 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-10-31 21:10:53 +00:00
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-08-23 02:05:43 +00:00
|
|
|
</ServerContentBlock>
|
2020-07-10 02:56:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NetworkContainer;
|