2020-11-01 05:30:03 +00:00
|
|
|
import React, { memo, useCallback, useState } from 'react';
|
2020-09-25 02:20:19 +00:00
|
|
|
import isEqual from 'react-fast-compare';
|
2021-07-22 17:15:27 +00:00
|
|
|
import tw, { styled } from 'twin.macro';
|
2020-09-25 02:20:19 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faNetworkWired } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import InputSpinner from '@/components/elements/InputSpinner';
|
|
|
|
import { Textarea } from '@/components/elements/Input';
|
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
|
|
|
import { Allocation } from '@/api/server/getServer';
|
|
|
|
import { debounce } from 'debounce';
|
|
|
|
import setServerAllocationNotes from '@/api/server/network/setServerAllocationNotes';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2020-10-31 20:16:41 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
2020-11-01 04:22:44 +00:00
|
|
|
import DeleteAllocationButton from '@/components/server/network/DeleteAllocationButton';
|
2020-11-01 05:30:03 +00:00
|
|
|
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
|
|
|
|
import getServerAllocations from '@/api/swr/getServerAllocations';
|
2021-12-04 18:35:55 +00:00
|
|
|
import { formatIp } from '@/helpers';
|
2020-09-25 02:20:19 +00:00
|
|
|
|
2020-10-11 19:34:48 +00:00
|
|
|
const Code = styled.code`${tw`font-mono py-1 px-2 bg-neutral-900 rounded text-sm inline-block`}`;
|
2020-09-25 02:20:19 +00:00
|
|
|
const Label = styled.label`${tw`uppercase text-xs mt-1 text-neutral-400 block px-1 select-none transition-colors duration-150`}`;
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
allocation: Allocation;
|
|
|
|
}
|
|
|
|
|
2020-11-01 05:30:03 +00:00
|
|
|
const AllocationRow = ({ allocation }: Props) => {
|
2020-09-25 02:20:19 +00:00
|
|
|
const [ loading, setLoading ] = useState(false);
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-11-01 05:30:03 +00:00
|
|
|
const { mutate } = getServerAllocations();
|
|
|
|
|
|
|
|
const onNotesChanged = useCallback((id: number, notes: string) => {
|
|
|
|
mutate(data => data?.map(a => a.id === id ? { ...a, notes } : a), false);
|
|
|
|
}, []);
|
2020-09-25 02:20:19 +00:00
|
|
|
|
|
|
|
const setAllocationNotes = debounce((notes: string) => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('server:network');
|
|
|
|
|
|
|
|
setServerAllocationNotes(uuid, allocation.id, notes)
|
|
|
|
.then(() => onNotesChanged(allocation.id, notes))
|
|
|
|
.catch(error => clearAndAddHttpError({ key: 'server:network', error }))
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, 750);
|
|
|
|
|
2020-11-01 05:30:03 +00:00
|
|
|
const setPrimaryAllocation = () => {
|
|
|
|
clearFlashes('server:network');
|
|
|
|
mutate(data => data?.map(a => ({ ...a, isDefault: a.id === allocation.id })), false);
|
|
|
|
|
|
|
|
setPrimaryServerAllocation(uuid, allocation.id)
|
|
|
|
.catch(error => {
|
|
|
|
clearAndAddHttpError({ key: 'server:network', error });
|
|
|
|
mutate();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-09-25 02:20:19 +00:00
|
|
|
return (
|
2020-12-26 17:50:09 +00:00
|
|
|
<GreyRowBox $hoverable={false} css={tw`flex-wrap md:flex-nowrap mt-2`}>
|
2020-10-03 22:48:31 +00:00
|
|
|
<div css={tw`flex items-center w-full md:w-auto`}>
|
|
|
|
<div css={tw`pl-4 pr-6 text-neutral-400`}>
|
2020-11-01 04:22:44 +00:00
|
|
|
<FontAwesomeIcon icon={faNetworkWired}/>
|
2020-10-03 22:48:31 +00:00
|
|
|
</div>
|
|
|
|
<div css={tw`mr-4 flex-1 md:w-40`}>
|
2020-11-01 04:22:44 +00:00
|
|
|
{allocation.alias ?
|
|
|
|
<CopyOnClick text={allocation.alias}><Code css={tw`w-40 truncate`}>{allocation.alias}</Code></CopyOnClick> :
|
2021-12-04 18:35:55 +00:00
|
|
|
<CopyOnClick text={formatIp(allocation.ip)}><Code>{formatIp(allocation.ip)}</Code></CopyOnClick>}
|
2020-10-31 20:16:41 +00:00
|
|
|
<Label>{allocation.alias ? 'Hostname' : 'IP Address'}</Label>
|
2020-10-03 22:48:31 +00:00
|
|
|
</div>
|
|
|
|
<div css={tw`w-16 md:w-24 overflow-hidden`}>
|
|
|
|
<Code>{allocation.port}</Code>
|
|
|
|
<Label>Port</Label>
|
|
|
|
</div>
|
2020-09-25 02:20:19 +00:00
|
|
|
</div>
|
2020-10-03 22:48:31 +00:00
|
|
|
<div css={tw`mt-4 w-full md:mt-0 md:flex-1 md:w-auto`}>
|
2020-09-25 02:20:19 +00:00
|
|
|
<InputSpinner visible={loading}>
|
|
|
|
<Textarea
|
|
|
|
css={tw`bg-neutral-800 hover:border-neutral-600 border-transparent`}
|
|
|
|
placeholder={'Notes'}
|
|
|
|
defaultValue={allocation.notes || undefined}
|
|
|
|
onChange={e => setAllocationNotes(e.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
</InputSpinner>
|
|
|
|
</div>
|
2020-11-01 04:22:44 +00:00
|
|
|
<div css={tw`w-full md:flex-none md:w-40 md:text-center mt-4 md:mt-0 ml-4 flex items-center justify-end`}>
|
2020-09-25 02:20:19 +00:00
|
|
|
{allocation.isDefault ?
|
|
|
|
<span css={tw`bg-green-500 py-1 px-2 rounded text-green-50 text-xs`}>Primary</span>
|
|
|
|
:
|
2020-11-01 04:22:44 +00:00
|
|
|
<>
|
2020-11-09 01:07:26 +00:00
|
|
|
<Can action={'allocation.delete'}>
|
2020-11-01 04:22:44 +00:00
|
|
|
<DeleteAllocationButton allocation={allocation.id}/>
|
|
|
|
</Can>
|
2020-11-09 01:07:26 +00:00
|
|
|
<Can action={'allocation.update'}>
|
2020-11-01 04:22:44 +00:00
|
|
|
<Button
|
|
|
|
isSecondary
|
|
|
|
size={'xsmall'}
|
|
|
|
color={'primary'}
|
2020-11-01 05:30:03 +00:00
|
|
|
onClick={setPrimaryAllocation}
|
2020-11-01 04:22:44 +00:00
|
|
|
>
|
|
|
|
Make Primary
|
|
|
|
</Button>
|
|
|
|
</Can>
|
|
|
|
</>
|
2020-09-25 02:20:19 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</GreyRowBox>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default memo(AllocationRow, isEqual);
|