import { ReactElement, useMemo } from 'react'; import NETWORK_TYPES from '../../lib/consts/NETWORK_TYPES'; import FlexBox from '../FlexBox'; import Grid from '../Grid'; import InputWithRef from '../InputWithRef'; import OutlinedInputWithLabel from '../OutlinedInputWithLabel'; import { InnerPanel, InnerPanelBody, InnerPanelHeader } from '../Panels'; import SwitchWithLabel from '../SwitchWithLabel'; import { buildIPAddressTestBatch, buildNumberTestBatch, } from '../../lib/test_input'; import { BodyText } from '../Text'; const INPUT_ID_PREFIX_ANVIL_HOST = 'anvil-host-input'; const INPUT_CELL_ID_PREFIX_ANVIL_HOST = `${INPUT_ID_PREFIX_ANVIL_HOST}-cell`; const AnvilHostInputGroup = ({ formUtils: { buildFinishInputTestBatchFunction, buildInputFirstRenderFunction, msgSetters, setMsgSetter, }, hostLabel, previous: { fences: fenceList = {}, networks: networkList = {}, upses: upsList = {}, } = {}, }: AnvilHostInputGroupProps): ReactElement => { const fenceListEntries = useMemo( () => Object.entries(fenceList), [fenceList], ); const networkListEntries = useMemo( () => Object.entries(networkList), [networkList], ); const upsListEntries = useMemo(() => Object.entries(upsList), [upsList]); const fenceListGridLayout = useMemo( () => fenceListEntries.reduce( (previous, [fenceId, { fenceName, fencePort }]) => { const idPostfix = `${fenceId}-port`; const cellId = `${INPUT_CELL_ID_PREFIX_ANVIL_HOST}-${idPostfix}`; const inputId = `${INPUT_ID_PREFIX_ANVIL_HOST}-${idPostfix}`; const inputLabel = fenceName; setMsgSetter(inputId); previous[cellId] = { children: ( } inputTestBatch={buildNumberTestBatch( inputLabel, () => { msgSetters[inputId](); }, { onFinishBatch: buildFinishInputTestBatchFunction(inputId) }, (message) => { msgSetters[inputId]({ children: message, }); }, )} onFirstRender={buildInputFirstRenderFunction(inputId)} required valueType="number" /> ), }; return previous; }, {}, ), [ buildFinishInputTestBatchFunction, buildInputFirstRenderFunction, fenceListEntries, msgSetters, setMsgSetter, ], ); const networkListGridLayout = useMemo( () => networkListEntries.reduce( (previous, [networkId, { networkIp, networkNumber, networkType }]) => { const idPostfix = `${networkId}-ip`; const cellId = `${INPUT_CELL_ID_PREFIX_ANVIL_HOST}-${idPostfix}`; const inputId = `${INPUT_ID_PREFIX_ANVIL_HOST}-${idPostfix}`; const inputLabel = `${NETWORK_TYPES[networkType]} ${networkNumber}`; setMsgSetter(inputId); previous[cellId] = { children: ( } inputTestBatch={buildIPAddressTestBatch( inputLabel, () => { msgSetters[inputId](); }, { onFinishBatch: buildFinishInputTestBatchFunction(inputId) }, (message) => { msgSetters[inputId]({ children: message, }); }, )} onFirstRender={buildInputFirstRenderFunction(inputId)} required /> ), }; return previous; }, {}, ), [ networkListEntries, setMsgSetter, buildFinishInputTestBatchFunction, buildInputFirstRenderFunction, msgSetters, ], ); const upsListGridLayout = useMemo( () => upsListEntries.reduce( (previous, [upsId, { isPowerHost, upsName }]) => { const idPostfix = `${upsId}-power-host`; const cellId = `${INPUT_CELL_ID_PREFIX_ANVIL_HOST}-${idPostfix}`; const inputId = `${INPUT_ID_PREFIX_ANVIL_HOST}-${idPostfix}`; previous[cellId] = { children: ( } valueType="boolean" /> ), }; return previous; }, {}, ), [upsListEntries], ); return ( {hostLabel} {Boolean(fenceListEntries.length || upsListEntries.length) && ( )} ); }; export { INPUT_ID_PREFIX_ANVIL_HOST }; export default AnvilHostInputGroup;