2020-07-10 04:00:03 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-07-10 02:56:46 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faNetworkWired } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import styled from 'styled-components/macro';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import useSWR from 'swr';
|
|
|
|
import getServerAllocations from '@/api/server/network/getServerAllocations';
|
|
|
|
import { Allocation } from '@/api/server/getServer';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-07-10 04:00:03 +00:00
|
|
|
import { Textarea } from '@/components/elements/Input';
|
|
|
|
import setServerAllocationNotes from '@/api/server/network/setServerAllocationNotes';
|
|
|
|
import { debounce } from 'debounce';
|
|
|
|
import InputSpinner from '@/components/elements/InputSpinner';
|
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';
|
|
|
|
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
|
2020-07-10 02:56:46 +00:00
|
|
|
|
|
|
|
const Code = styled.code`${tw`font-mono py-1 px-2 bg-neutral-900 rounded text-sm block`}`;
|
|
|
|
const Label = styled.label`${tw`uppercase text-xs mt-1 text-neutral-400 block px-1 select-none transition-colors duration-150`}`;
|
|
|
|
|
|
|
|
const NetworkContainer = () => {
|
2020-08-26 05:09:54 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
|
|
|
|
|
2020-07-10 03:00:05 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2020-07-10 04:00:03 +00:00
|
|
|
const [ loading, setLoading ] = useState<false | number>(false);
|
|
|
|
const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), { initialData: allocations });
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
const setPrimaryAllocation = (id: number) => {
|
2020-07-10 02:56:46 +00:00
|
|
|
clearFlashes('server:network');
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
const initial = data;
|
|
|
|
mutate(data?.map(a => a.id === id ? { ...a, isDefault: true } : { ...a, isDefault: false }), false);
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2020-07-10 04:00:03 +00:00
|
|
|
setPrimaryServerAllocation(uuid, id)
|
2020-07-10 03:36:08 +00:00
|
|
|
.catch(error => {
|
|
|
|
clearAndAddHttpError({ key: 'server:network', error });
|
|
|
|
mutate(initial, false);
|
|
|
|
});
|
2020-07-10 02:56:46 +00:00
|
|
|
};
|
|
|
|
|
2020-07-10 04:00:03 +00:00
|
|
|
const setAllocationNotes = debounce((id: number, notes: string) => {
|
|
|
|
setLoading(id);
|
|
|
|
clearFlashes('server:network');
|
|
|
|
|
|
|
|
setServerAllocationNotes(uuid, id, notes)
|
|
|
|
.then(() => mutate(data?.map(a => a.id === id ? { ...a, notes } : a), false))
|
|
|
|
.catch(error => {
|
|
|
|
clearAndAddHttpError({ key: 'server:network', error });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, 750);
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (error) {
|
2020-07-10 03:00:05 +00:00
|
|
|
clearAndAddHttpError({ key: 'server:network', error });
|
2020-07-10 02:56:46 +00:00
|
|
|
}
|
|
|
|
}, [ error ]);
|
|
|
|
|
|
|
|
return (
|
2020-08-23 02:05:43 +00:00
|
|
|
<ServerContentBlock showFlashKey={'server:network'} title={'Network'}>
|
2020-07-10 02:56:46 +00:00
|
|
|
{!data ?
|
|
|
|
<Spinner size={'large'} centered/>
|
|
|
|
:
|
2020-07-10 04:00:03 +00:00
|
|
|
data.map(({ id, ip, port, alias, notes, isDefault }, index) => (
|
2020-09-13 17:49:57 +00:00
|
|
|
<GreyRowBox
|
|
|
|
$hoverable={false}
|
|
|
|
key={`${ip}:${port}`}
|
|
|
|
css={index > 0 ? tw`mt-2 overflow-x-auto` : tw`overflow-x-auto`}
|
|
|
|
>
|
2020-09-13 17:02:25 +00:00
|
|
|
<div css={tw`hidden md:block pl-4 pr-6 text-neutral-400`}>
|
2020-07-10 02:56:46 +00:00
|
|
|
<FontAwesomeIcon icon={faNetworkWired}/>
|
|
|
|
</div>
|
|
|
|
<div css={tw`mr-4`}>
|
|
|
|
<Code>{alias || ip}</Code>
|
|
|
|
<Label>IP Address</Label>
|
|
|
|
</div>
|
|
|
|
<div>
|
2020-07-10 15:37:55 +00:00
|
|
|
<Code>{port}</Code>
|
2020-07-10 02:56:46 +00:00
|
|
|
<Label>Port</Label>
|
|
|
|
</div>
|
2020-09-13 17:02:25 +00:00
|
|
|
<div css={tw`px-8 flex-none sm:flex-1 self-start`}>
|
2020-07-10 04:00:03 +00:00
|
|
|
<InputSpinner visible={loading === id}>
|
|
|
|
<Textarea
|
|
|
|
css={tw`bg-neutral-800 hover:border-neutral-600 border-transparent`}
|
|
|
|
placeholder={'Notes'}
|
|
|
|
defaultValue={notes || undefined}
|
|
|
|
onChange={e => setAllocationNotes(id, e.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
</InputSpinner>
|
|
|
|
</div>
|
2020-09-13 17:49:57 +00:00
|
|
|
<div css={tw`w-32 text-right pr-4 sm:pr-0`}>
|
2020-07-10 02:56:46 +00:00
|
|
|
{isDefault ?
|
|
|
|
<span css={tw`bg-green-500 py-1 px-2 rounded text-green-50 text-xs`}>
|
|
|
|
Primary
|
|
|
|
</span>
|
|
|
|
:
|
|
|
|
<Can action={'allocations.update'}>
|
|
|
|
<Button
|
|
|
|
isSecondary
|
|
|
|
size={'xsmall'}
|
|
|
|
color={'primary'}
|
2020-07-10 03:36:08 +00:00
|
|
|
onClick={() => setPrimaryAllocation(id)}
|
2020-07-10 02:56:46 +00:00
|
|
|
>
|
|
|
|
Make Primary
|
|
|
|
</Button>
|
|
|
|
</Can>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</GreyRowBox>
|
|
|
|
))
|
|
|
|
}
|
2020-08-23 02:05:43 +00:00
|
|
|
</ServerContentBlock>
|
2020-07-10 02:56:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NetworkContainer;
|