From 48a104667f65217900c2413dfe1c8111be1ab560 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 24 Sep 2020 19:20:19 -0700 Subject: [PATCH] Fix re-rendering mess on allocation page --- .../server/network/AllocationRow.tsx | 88 ++++++++++++++ .../server/network/NetworkContainer.tsx | 108 ++++-------------- 2 files changed, 113 insertions(+), 83 deletions(-) create mode 100644 resources/scripts/components/server/network/AllocationRow.tsx diff --git a/resources/scripts/components/server/network/AllocationRow.tsx b/resources/scripts/components/server/network/AllocationRow.tsx new file mode 100644 index 000000000..c8b7870e7 --- /dev/null +++ b/resources/scripts/components/server/network/AllocationRow.tsx @@ -0,0 +1,88 @@ +import React, { memo, useState } from 'react'; +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'; +import Button from '@/components/elements/Button'; +import GreyRowBox from '@/components/elements/GreyRowBox'; +import { Allocation } from '@/api/server/getServer'; +import styled from 'styled-components/macro'; +import { debounce } from 'debounce'; +import setServerAllocationNotes from '@/api/server/network/setServerAllocationNotes'; +import useFlash from '@/plugins/useFlash'; +import { ServerContext } from '@/state/server'; + +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`}`; + +interface Props { + allocation: Allocation; + onSetPrimary: (id: number) => void; + onNotesChanged: (id: number, notes: string) => void; +} + +const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => { + const [ loading, setLoading ] = useState(false); + const { clearFlashes, clearAndAddHttpError } = useFlash(); + const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); + + 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); + + return ( + 0 ? tw`mt-2 overflow-x-auto` : tw`overflow-x-auto`} + > +
+ +
+
+ {allocation.alias || allocation.ip} + +
+
+ {allocation.port} + +
+
+ +