misc_pterodactyl-panel/resources/scripts/components/server/network/NetworkContainer.tsx

100 lines
4.1 KiB
TypeScript
Raw Normal View History

import React, { useCallback, useEffect, useState } from 'react';
import useSWR from 'swr';
import getServerAllocations from '@/api/server/network/getServerAllocations';
import { Allocation } from '@/api/server/getServer';
import Spinner from '@/components/elements/Spinner';
import useFlash from '@/plugins/useFlash';
import ServerContentBlock from '@/components/elements/ServerContentBlock';
2020-08-26 05:09:54 +00:00
import { ServerContext } from '@/state/server';
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
import AllocationRow from '@/components/server/network/AllocationRow';
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
import Button from '@/components/elements/Button';
2020-10-31 21:10:53 +00:00
import createServerAllocation from '@/api/server/network/createServerAllocation';
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';
const NetworkContainer = () => {
2020-10-31 21:10:53 +00:00
const [ loading, setLoading ] = useState(false);
2020-08-26 05:09:54 +00:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const allocationLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.allocations);
2020-08-26 05:09:54 +00:00
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
const { clearFlashes, clearAndAddHttpError } = useFlash();
const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), {
initialData: allocations,
revalidateOnFocus: false,
});
useEffect(() => {
if (error) {
clearAndAddHttpError({ key: 'server:network', error });
}
}, [ error ]);
2020-10-31 21:10:53 +00:00
const setPrimaryAllocation = (id: number) => {
clearFlashes('server:network');
const initial = data;
mutate(data?.map(a => a.id === id ? { ...a, isDefault: true } : { ...a, isDefault: false }), false);
setPrimaryServerAllocation(uuid, id)
.catch(error => {
clearAndAddHttpError({ key: 'server:network', error });
mutate(initial, false);
});
2020-10-31 21:10:53 +00:00
};
2020-10-31 21:10:53 +00:00
const onCreateAllocation = () => {
clearFlashes('server:network');
2020-10-31 21:10:53 +00:00
setLoading(true);
createServerAllocation(uuid)
.then(allocation => mutate(data?.concat(allocation), false))
.catch(error => clearAndAddHttpError({ key: 'server:network', error }))
.then(() => setLoading(false));
};
const onNotesAdded = useCallback((id: number, notes: string) => {
mutate(data?.map(a => a.id === id ? { ...a, notes } : a), false);
}, []);
return (
<ServerContentBlock showFlashKey={'server:network'} title={'Network'}>
{!data ?
<Spinner size={'large'} centered/>
:
2020-10-31 21:10:53 +00:00
<>
{
data.map(allocation => (
<AllocationRow
key={`${allocation.ip}:${allocation.port}`}
allocation={allocation}
onSetPrimary={setPrimaryAllocation}
onNotesChanged={onNotesAdded}
/>
))
}
2020-10-31 21:10:53 +00:00
<Can action={'allocation.create'}>
<SpinnerOverlay visible={loading}/>
<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>
{allocationLimit > data.length &&
<Button css={tw`w-full sm:w-auto`} color={'primary'} onClick={onCreateAllocation}>
Create Allocation
</Button>
}
</div>
</Can>
</>
}
</ServerContentBlock>
);
};
export default NetworkContainer;