2022-11-25 20:25:03 +00:00
|
|
|
import { memo, useCallback, useState } from 'react';
|
2020-09-25 02:20:19 +00:00
|
|
|
import isEqual from 'react-fast-compare';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
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';
|
2022-06-20 19:28:27 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2020-09-25 02:20:19 +00:00
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
|
|
|
import { Allocation } from '@/api/server/getServer';
|
2022-11-25 20:25:03 +00:00
|
|
|
import styled from 'styled-components';
|
2020-09-25 02:20:19 +00:00
|
|
|
import { debounce } from 'debounce';
|
|
|
|
import setServerAllocationNotes from '@/api/server/network/setServerAllocationNotes';
|
2022-06-20 19:28:27 +00:00
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
2020-09-25 02:20:19 +00:00
|
|
|
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';
|
2022-06-26 18:34:09 +00:00
|
|
|
import { ip } from '@/lib/formatters';
|
2022-06-20 19:28:27 +00:00
|
|
|
import Code from '@/components/elements/Code';
|
2020-09-25 02:20:19 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const Label = styled.label`
|
|
|
|
${tw`uppercase text-xs mt-1 text-neutral-400 block px-1 select-none transition-colors duration-150`}
|
|
|
|
`;
|
2020-09-25 02:20:19 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
allocation: Allocation;
|
|
|
|
}
|
|
|
|
|
2020-11-01 05:30:03 +00:00
|
|
|
const AllocationRow = ({ allocation }: Props) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2022-06-20 19:28:27 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlashKey('server:network');
|
2022-11-25 20:25:03 +00:00
|
|
|
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) => {
|
2022-11-25 20:25:03 +00:00
|
|
|
mutate(data => data?.map(a => (a.id === id ? { ...a, notes } : a)), false);
|
2020-11-01 05:30:03 +00:00
|
|
|
}, []);
|
2020-09-25 02:20:19 +00:00
|
|
|
|
|
|
|
const setAllocationNotes = debounce((notes: string) => {
|
|
|
|
setLoading(true);
|
2022-06-20 19:28:27 +00:00
|
|
|
clearFlashes();
|
2020-09-25 02:20:19 +00:00
|
|
|
|
|
|
|
setServerAllocationNotes(uuid, allocation.id, notes)
|
|
|
|
.then(() => onNotesChanged(allocation.id, notes))
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => clearAndAddHttpError(error))
|
2020-09-25 02:20:19 +00:00
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, 750);
|
|
|
|
|
2020-11-01 05:30:03 +00:00
|
|
|
const setPrimaryAllocation = () => {
|
2022-06-20 19:28:27 +00:00
|
|
|
clearFlashes();
|
2022-11-25 20:25:03 +00:00
|
|
|
mutate(data => data?.map(a => ({ ...a, isDefault: a.id === allocation.id })), false);
|
2020-11-01 05:30:03 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
setPrimaryServerAllocation(uuid, allocation.id).catch(error => {
|
2022-06-26 19:13:52 +00:00
|
|
|
clearAndAddHttpError(error);
|
|
|
|
mutate();
|
|
|
|
});
|
2020-11-01 05:30:03 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 02:20:19 +00:00
|
|
|
return (
|
2022-06-20 19:28:27 +00:00
|
|
|
<GreyRowBox $hoverable={false} className={'flex-wrap md:flex-nowrap mt-2'}>
|
|
|
|
<div className={'flex items-center w-full md:w-auto'}>
|
|
|
|
<div className={'pl-4 pr-6 text-neutral-400'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon icon={faNetworkWired} />
|
2020-10-03 22:48:31 +00:00
|
|
|
</div>
|
2022-06-20 19:28:27 +00:00
|
|
|
<div className={'mr-4 flex-1 md:w-40'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{allocation.alias ? (
|
|
|
|
<CopyOnClick text={allocation.alias}>
|
|
|
|
<Code dark className={'w-40 truncate'}>
|
|
|
|
{allocation.alias}
|
|
|
|
</Code>
|
|
|
|
</CopyOnClick>
|
|
|
|
) : (
|
|
|
|
<CopyOnClick text={ip(allocation.ip)}>
|
|
|
|
<Code dark>{ip(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>
|
2022-06-20 19:28:27 +00:00
|
|
|
<div className={'w-16 md:w-24 overflow-hidden'}>
|
|
|
|
<Code dark>{allocation.port}</Code>
|
2020-10-03 22:48:31 +00:00
|
|
|
<Label>Port</Label>
|
|
|
|
</div>
|
2020-09-25 02:20:19 +00:00
|
|
|
</div>
|
2022-06-20 19:28:27 +00:00
|
|
|
<div className={'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
|
2022-06-20 19:28:27 +00:00
|
|
|
className={'bg-neutral-800 hover:border-neutral-600 border-transparent'}
|
2020-09-25 02:20:19 +00:00
|
|
|
placeholder={'Notes'}
|
|
|
|
defaultValue={allocation.notes || undefined}
|
2022-11-25 20:25:03 +00:00
|
|
|
onChange={e => setAllocationNotes(e.currentTarget.value)}
|
2020-09-25 02:20:19 +00:00
|
|
|
/>
|
|
|
|
</InputSpinner>
|
|
|
|
</div>
|
2022-06-20 19:28:27 +00:00
|
|
|
<div className={'flex justify-end space-x-4 mt-4 w-full md:mt-0 md:w-48'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{allocation.isDefault ? (
|
|
|
|
<Button size={Button.Sizes.Small} className={'!text-gray-50 !bg-blue-600'} disabled>
|
|
|
|
Primary
|
|
|
|
</Button>
|
|
|
|
) : (
|
2020-11-01 04:22:44 +00:00
|
|
|
<>
|
2020-11-09 01:07:26 +00:00
|
|
|
<Can action={'allocation.delete'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<DeleteAllocationButton allocation={allocation.id} />
|
2020-11-01 04:22:44 +00:00
|
|
|
</Can>
|
2020-11-09 01:07:26 +00:00
|
|
|
<Can action={'allocation.update'}>
|
2022-06-20 19:28:27 +00:00
|
|
|
<Button.Text size={Button.Sizes.Small} onClick={setPrimaryAllocation}>
|
2020-11-01 04:22:44 +00:00
|
|
|
Make Primary
|
2022-06-20 19:28:27 +00:00
|
|
|
</Button.Text>
|
2020-11-01 04:22:44 +00:00
|
|
|
</Can>
|
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-09-25 02:20:19 +00:00
|
|
|
</div>
|
|
|
|
</GreyRowBox>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default memo(AllocationRow, isEqual);
|