Files
pterodactyl-panel/resources/scripts/components/server/network/NetworkContainer.tsx
T

112 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-09-28 11:50:34 -04:00
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-25 22:09:54 -07:00
import { ServerContext } from '@/state/server';
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
2020-09-24 19:20:19 -07:00
import AllocationRow from '@/components/server/network/AllocationRow';
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
2020-09-28 11:50:34 -04:00
import Button from '@/components/elements/Button';
import newServerAllocation from '@/api/server/network/newServerAllocation';
import tw from 'twin.macro';
import GreyRowBox from '@/components/elements/GreyRowBox';
const NetworkContainer = () => {
2020-08-25 22:09:54 -07:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
2020-09-28 16:14:14 -04:00
const allocationLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.allocations);
2020-08-25 22:09:54 -07:00
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
2020-09-28 11:50:34 -04:00
const [ addingAllocation, setAddingAllocation ] = useState(false);
2020-08-25 22:09:54 -07:00
const { clearFlashes, clearAndAddHttpError } = useFlash();
2020-09-24 19:20:19 -07:00
const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), {
initialData: allocations,
revalidateOnFocus: false,
});
useEffect(() => {
if (error) {
clearAndAddHttpError({ key: 'server:network', error });
}
}, [ error ]);
2020-09-24 19:20:19 -07:00
const setPrimaryAllocation = useCallback((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-09-24 19:20:19 -07:00
}, []);
2020-09-28 11:50:34 -04:00
const getNewAllocation = () => {
clearFlashes('server:network');
setAddingAllocation(true);
2020-09-28 16:14:14 -04:00
const initial = data;
2020-09-28 11:50:34 -04:00
newServerAllocation(uuid)
.then(allocation => {
2020-09-28 16:14:14 -04:00
mutate(data?.concat(allocation), false);
2020-09-28 11:50:34 -04:00
setAddingAllocation(false);
})
.catch(error => {
clearAndAddHttpError({ key: 'server:network', error });
2020-09-28 16:14:14 -04:00
mutate(initial, false);
2020-09-28 11:50:34 -04:00
setAddingAllocation(false);
});
};
2020-09-24 19:20:19 -07:00
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-09-24 19:20:19 -07:00
data.map(allocation => (
<AllocationRow
key={`${allocation.ip}:${allocation.port}`}
allocation={allocation}
onSetPrimary={setPrimaryAllocation}
onNotesChanged={onNotesAdded}
/>
))
}
2020-09-28 16:14:14 -04:00
{allocationLimit > data!.length ?
<GreyRowBox
$hoverable={false}
css={tw`mt-2 overflow-x-auto flex items-center justify-center`}
>
{addingAllocation ?
<Spinner size={'base'} centered/>
:
<Button
color={'primary'}
isSecondary
onClick={() => getNewAllocation()}
css={tw`my-2`}
>
Add New Allocation
</Button>
}
</GreyRowBox>
:
<p css={tw`mt-2 text-center text-sm text-neutral-400`}>
You have reached the max number of allocations allowed for your server.
</p>
}
</ServerContentBlock>
);
};
export default NetworkContainer;