You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
372 lines
9.9 KiB
372 lines
9.9 KiB
2 years ago
|
import { ReactElement, useCallback, useMemo } from 'react';
|
||
2 years ago
|
import { v4 as uuidv4 } from 'uuid';
|
||
2 years ago
|
|
||
2 years ago
|
import NETWORK_TYPES from '../../lib/consts/NETWORK_TYPES';
|
||
|
|
||
2 years ago
|
import AnNetworkInputGroup from './AnNetworkInputGroup';
|
||
2 years ago
|
import buildObjectStateSetterCallback from '../../lib/buildObjectStateSetterCallback';
|
||
2 years ago
|
import Grid from '../Grid';
|
||
2 years ago
|
import IconButton from '../IconButton';
|
||
2 years ago
|
import InputWithRef from '../InputWithRef';
|
||
|
import OutlinedInputWithLabel from '../OutlinedInputWithLabel';
|
||
|
import { buildNumberTestBatch } from '../../lib/test_input';
|
||
|
|
||
2 years ago
|
const INPUT_ID_PREFIX_AN_NETWORK_CONFIG = 'an-network-config-input';
|
||
2 years ago
|
|
||
2 years ago
|
const INPUT_CELL_ID_PREFIX_ANC = `${INPUT_ID_PREFIX_AN_NETWORK_CONFIG}-cell`;
|
||
2 years ago
|
|
||
2 years ago
|
const INPUT_ID_ANC_DNS = `${INPUT_ID_PREFIX_AN_NETWORK_CONFIG}-dns`;
|
||
|
const INPUT_ID_ANC_MTU = `${INPUT_ID_PREFIX_AN_NETWORK_CONFIG}-mtu`;
|
||
|
const INPUT_ID_ANC_NTP = `${INPUT_ID_PREFIX_AN_NETWORK_CONFIG}-ntp`;
|
||
2 years ago
|
|
||
2 years ago
|
const INPUT_LABEL_ANC_DNS = 'DNS';
|
||
|
const INPUT_LABEL_ANC_MTU = 'MTU';
|
||
|
const INPUT_LABEL_ANC_NTP = 'NTP';
|
||
2 years ago
|
|
||
2 years ago
|
const DEFAULT_DNS_CSV = '8.8.8.8,8.8.4.4';
|
||
2 years ago
|
|
||
2 years ago
|
const NETWORK_TYPE_ENTRIES = Object.entries(NETWORK_TYPES);
|
||
|
|
||
|
const assertIfn = (type: string) => type === 'ifn';
|
||
|
const assertMn = (type: string) => type === 'mn';
|
||
2 years ago
|
|
||
2 years ago
|
const AnNetworkConfigInputGroup = <
|
||
2 years ago
|
M extends MapToInputTestID & {
|
||
|
[K in
|
||
2 years ago
|
| typeof INPUT_ID_ANC_DNS
|
||
|
| typeof INPUT_ID_ANC_MTU
|
||
|
| typeof INPUT_ID_ANC_NTP]: string;
|
||
2 years ago
|
},
|
||
|
>({
|
||
|
formUtils,
|
||
2 years ago
|
networkListEntries,
|
||
2 years ago
|
previous: {
|
||
2 years ago
|
dnsCsv: previousDnsCsv = DEFAULT_DNS_CSV,
|
||
2 years ago
|
mtu: previousMtu,
|
||
|
ntpCsv: previousNtpCsv,
|
||
|
} = {},
|
||
2 years ago
|
setNetworkList,
|
||
2 years ago
|
}: AnNetworkConfigInputGroupProps<M>): ReactElement => {
|
||
2 years ago
|
const {
|
||
|
buildFinishInputTestBatchFunction,
|
||
|
buildInputFirstRenderFunction,
|
||
|
msgSetters,
|
||
|
} = formUtils;
|
||
|
|
||
2 years ago
|
const getNetworkNumber = useCallback(
|
||
|
(
|
||
|
type: string,
|
||
|
{
|
||
2 years ago
|
input = networkListEntries,
|
||
|
end = networkListEntries.length,
|
||
2 years ago
|
}: {
|
||
2 years ago
|
input?: Array<[string, ManifestNetwork]>;
|
||
2 years ago
|
end?: number;
|
||
|
} = {},
|
||
|
) => {
|
||
2 years ago
|
const limit = end - 1;
|
||
|
|
||
2 years ago
|
let netNum = 0;
|
||
|
|
||
|
input.every(([, { networkType }], networkIndex) => {
|
||
|
if (networkType === type) {
|
||
|
netNum += 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
return networkIndex < limit;
|
||
2 years ago
|
});
|
||
|
|
||
|
return netNum;
|
||
|
},
|
||
2 years ago
|
[networkListEntries],
|
||
|
);
|
||
|
|
||
|
const networkTypeOptions = useMemo<SelectItem[]>(
|
||
|
() =>
|
||
|
NETWORK_TYPE_ENTRIES.map(([key, value]) => ({
|
||
|
displayValue: value,
|
||
|
value: key,
|
||
|
})),
|
||
|
[],
|
||
2 years ago
|
);
|
||
|
|
||
|
const buildNetwork = useCallback(
|
||
|
({
|
||
|
networkMinIp = '',
|
||
|
networkSubnetMask = '',
|
||
|
networkType = 'ifn',
|
||
|
// Params that depend on others.
|
||
2 years ago
|
networkGateway = assertIfn(networkType) ? '' : undefined,
|
||
2 years ago
|
networkNumber = getNetworkNumber(networkType) + 1,
|
||
2 years ago
|
}: Partial<ManifestNetwork> = {}): {
|
||
|
network: ManifestNetwork;
|
||
2 years ago
|
networkId: string;
|
||
|
} => ({
|
||
|
network: {
|
||
|
networkGateway,
|
||
|
networkMinIp,
|
||
|
networkNumber,
|
||
|
networkSubnetMask,
|
||
|
networkType,
|
||
|
},
|
||
|
networkId: uuidv4(),
|
||
|
}),
|
||
|
[getNetworkNumber],
|
||
|
);
|
||
|
|
||
|
const setNetwork = useCallback(
|
||
2 years ago
|
(key: string, value?: ManifestNetwork) =>
|
||
2 years ago
|
setNetworkList(buildObjectStateSetterCallback(key, value)),
|
||
2 years ago
|
[setNetworkList],
|
||
2 years ago
|
);
|
||
|
|
||
2 years ago
|
const handleNetworkTypeChange = useCallback<AnNetworkTypeChangeEventHandler>(
|
||
|
(
|
||
|
{ networkId: targetId, networkType: previousType },
|
||
|
{ target: { value } },
|
||
|
) => {
|
||
|
const newType = String(value);
|
||
2 years ago
|
|
||
2 years ago
|
let isIdMatch = false;
|
||
|
let newTypeNumber = 0;
|
||
2 years ago
|
|
||
2 years ago
|
const newList = networkListEntries.reduce<ManifestNetworkList>(
|
||
|
(previous, [networkId, networkValue]) => {
|
||
|
const { networkNumber: initnn, networkType: initnt } = networkValue;
|
||
2 years ago
|
|
||
2 years ago
|
let networkNumber = initnn;
|
||
|
let networkType = initnt;
|
||
2 years ago
|
|
||
2 years ago
|
if (networkId === targetId) {
|
||
|
isIdMatch = true;
|
||
2 years ago
|
|
||
2 years ago
|
networkType = newType;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const isTypeMatch = networkType === newType;
|
||
|
|
||
|
if (isTypeMatch) {
|
||
|
newTypeNumber += 1;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if (isIdMatch) {
|
||
2 years ago
|
if (isTypeMatch) {
|
||
2 years ago
|
networkNumber = newTypeNumber;
|
||
|
} else if (networkType === previousType) {
|
||
|
networkNumber -= 1;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
previous[networkId] = {
|
||
|
...networkValue,
|
||
|
networkNumber,
|
||
|
networkType,
|
||
|
};
|
||
|
} else {
|
||
|
previous[networkId] = networkValue;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return previous;
|
||
|
},
|
||
|
{},
|
||
|
);
|
||
2 years ago
|
|
||
2 years ago
|
setNetworkList(newList);
|
||
|
},
|
||
|
[networkListEntries, setNetworkList],
|
||
|
);
|
||
2 years ago
|
|
||
2 years ago
|
const handleNetworkRemove = useCallback<AnNetworkCloseEventHandler>(
|
||
2 years ago
|
({ networkId: rmId, networkType: rmType }) => {
|
||
|
let isIdMatch = false;
|
||
|
let networkNumber = 0;
|
||
|
|
||
2 years ago
|
const newList = networkListEntries.reduce<ManifestNetworkList>(
|
||
2 years ago
|
(previous, [networkId, networkValue]) => {
|
||
|
if (networkId === rmId) {
|
||
|
isIdMatch = true;
|
||
|
} else {
|
||
2 years ago
|
const { networkType } = networkValue;
|
||
|
|
||
2 years ago
|
if (networkType === rmType) {
|
||
|
networkNumber += 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
previous[networkId] = isIdMatch
|
||
|
? { ...networkValue, networkNumber }
|
||
|
: networkValue;
|
||
2 years ago
|
}
|
||
|
|
||
|
return previous;
|
||
|
},
|
||
|
{},
|
||
|
);
|
||
|
|
||
|
setNetworkList(newList);
|
||
|
},
|
||
2 years ago
|
[networkListEntries, setNetworkList],
|
||
2 years ago
|
);
|
||
|
|
||
2 years ago
|
const networksGridLayout = useMemo<GridLayout>(() => {
|
||
|
let result: GridLayout = {};
|
||
|
|
||
2 years ago
|
result = networkListEntries.reduce<GridLayout>(
|
||
2 years ago
|
(
|
||
|
previous,
|
||
|
[
|
||
|
networkId,
|
||
|
{
|
||
2 years ago
|
networkGateway,
|
||
|
networkMinIp,
|
||
2 years ago
|
networkNumber,
|
||
2 years ago
|
networkSubnetMask,
|
||
2 years ago
|
networkType,
|
||
|
},
|
||
|
],
|
||
|
) => {
|
||
2 years ago
|
const cellId = `${INPUT_CELL_ID_PREFIX_ANC}-${networkId}`;
|
||
2 years ago
|
|
||
2 years ago
|
const idPrefix = `an-network-${networkId}`;
|
||
2 years ago
|
|
||
2 years ago
|
const inputIdPrefix = `${INPUT_ID_PREFIX_AN_NETWORK_CONFIG}-${networkId}`;
|
||
2 years ago
|
const inputGatewayId = `${inputIdPrefix}-gateway`;
|
||
|
const inputMinIpId = `${inputIdPrefix}-min-ip`;
|
||
2 years ago
|
const inputNetworkTypeId = `${inputIdPrefix}-network-type`;
|
||
2 years ago
|
const inputSubnetMaskId = `${inputIdPrefix}-subnet-mask`;
|
||
|
|
||
2 years ago
|
const isFirstNetwork = networkNumber === 1;
|
||
2 years ago
|
const isIfn = assertIfn(networkType);
|
||
|
const isMn = assertMn(networkType);
|
||
|
const isOptional = isMn || !isFirstNetwork;
|
||
2 years ago
|
|
||
|
previous[cellId] = {
|
||
|
children: (
|
||
2 years ago
|
<AnNetworkInputGroup
|
||
2 years ago
|
formUtils={formUtils}
|
||
|
idPrefix={idPrefix}
|
||
|
inputGatewayId={inputGatewayId}
|
||
|
inputMinIpId={inputMinIpId}
|
||
2 years ago
|
inputNetworkTypeId={inputNetworkTypeId}
|
||
2 years ago
|
inputSubnetMaskId={inputSubnetMaskId}
|
||
2 years ago
|
networkId={networkId}
|
||
|
networkNumber={networkNumber}
|
||
|
networkType={networkType}
|
||
2 years ago
|
networkTypeOptions={networkTypeOptions}
|
||
|
onClose={handleNetworkRemove}
|
||
|
onNetworkTypeChange={handleNetworkTypeChange}
|
||
2 years ago
|
previous={{
|
||
2 years ago
|
gateway: networkGateway,
|
||
|
minIp: networkMinIp,
|
||
|
subnetMask: networkSubnetMask,
|
||
2 years ago
|
}}
|
||
2 years ago
|
readonlyNetworkName={!isOptional}
|
||
|
showCloseButton={isOptional}
|
||
|
showGateway={isIfn}
|
||
2 years ago
|
/>
|
||
|
),
|
||
|
md: 3,
|
||
|
sm: 2,
|
||
|
};
|
||
|
|
||
|
return previous;
|
||
|
},
|
||
|
result,
|
||
|
);
|
||
|
|
||
|
return result;
|
||
2 years ago
|
}, [
|
||
|
formUtils,
|
||
|
networkListEntries,
|
||
|
networkTypeOptions,
|
||
|
handleNetworkRemove,
|
||
|
handleNetworkTypeChange,
|
||
|
]);
|
||
2 years ago
|
|
||
|
return (
|
||
|
<Grid
|
||
|
columns={{ xs: 1, sm: 2, md: 3 }}
|
||
|
layout={{
|
||
|
...networksGridLayout,
|
||
2 years ago
|
'an-network-config-cell-add-network': {
|
||
2 years ago
|
children: (
|
||
|
<IconButton
|
||
|
mapPreset="add"
|
||
|
onClick={() => {
|
||
|
const { network: newNet, networkId: newNetId } = buildNetwork();
|
||
|
|
||
|
setNetwork(newNetId, newNet);
|
||
|
}}
|
||
|
/>
|
||
|
),
|
||
2 years ago
|
display: 'flex',
|
||
|
justifyContent: 'center',
|
||
|
md: 3,
|
||
|
sm: 2,
|
||
2 years ago
|
},
|
||
2 years ago
|
'an-network-config-input-cell-dns': {
|
||
2 years ago
|
children: (
|
||
|
<InputWithRef
|
||
|
input={
|
||
|
<OutlinedInputWithLabel
|
||
2 years ago
|
id={INPUT_ID_ANC_DNS}
|
||
|
label={INPUT_LABEL_ANC_DNS}
|
||
2 years ago
|
value={previousDnsCsv}
|
||
|
/>
|
||
|
}
|
||
|
required
|
||
|
/>
|
||
|
),
|
||
|
},
|
||
2 years ago
|
'an-network-config-input-cell-ntp': {
|
||
2 years ago
|
children: (
|
||
|
<InputWithRef
|
||
|
input={
|
||
|
<OutlinedInputWithLabel
|
||
2 years ago
|
id={INPUT_ID_ANC_NTP}
|
||
|
label={INPUT_LABEL_ANC_NTP}
|
||
2 years ago
|
value={previousNtpCsv}
|
||
|
/>
|
||
|
}
|
||
|
/>
|
||
|
),
|
||
|
},
|
||
2 years ago
|
'an-network-config-input-cell-mtu': {
|
||
2 years ago
|
children: (
|
||
|
<InputWithRef
|
||
|
input={
|
||
|
<OutlinedInputWithLabel
|
||
2 years ago
|
id={INPUT_ID_ANC_MTU}
|
||
2 years ago
|
inputProps={{ placeholder: '1500' }}
|
||
2 years ago
|
label={INPUT_LABEL_ANC_MTU}
|
||
2 years ago
|
value={previousMtu}
|
||
|
/>
|
||
|
}
|
||
|
inputTestBatch={buildNumberTestBatch(
|
||
2 years ago
|
INPUT_LABEL_ANC_MTU,
|
||
2 years ago
|
() => {
|
||
2 years ago
|
msgSetters[INPUT_ID_ANC_MTU]();
|
||
2 years ago
|
},
|
||
|
{
|
||
2 years ago
|
onFinishBatch:
|
||
|
buildFinishInputTestBatchFunction(INPUT_ID_ANC_MTU),
|
||
2 years ago
|
},
|
||
|
(message) => {
|
||
2 years ago
|
msgSetters[INPUT_ID_ANC_MTU]({
|
||
2 years ago
|
children: message,
|
||
|
});
|
||
|
},
|
||
|
)}
|
||
2 years ago
|
onFirstRender={buildInputFirstRenderFunction(INPUT_ID_ANC_MTU)}
|
||
2 years ago
|
valueType="number"
|
||
|
/>
|
||
|
),
|
||
|
},
|
||
|
}}
|
||
|
spacing="1em"
|
||
|
/>
|
||
|
);
|
||
|
};
|
||
|
|
||
2 years ago
|
export { INPUT_ID_ANC_DNS, INPUT_ID_ANC_MTU, INPUT_ID_ANC_NTP };
|
||
2 years ago
|
|
||
2 years ago
|
export default AnNetworkConfigInputGroup;
|