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.
246 lines
6.9 KiB
246 lines
6.9 KiB
2 years ago
|
import { Box, styled, Tooltip } from '@mui/material';
|
||
2 years ago
|
import { FC, ReactElement, ReactNode, useMemo } from 'react';
|
||
|
|
||
2 years ago
|
import INPUT_TYPES from '../../lib/consts/INPUT_TYPES';
|
||
2 years ago
|
|
||
2 years ago
|
import FlexBox from '../FlexBox';
|
||
|
import InputWithRef from '../InputWithRef';
|
||
|
import OutlinedInputWithLabel from '../OutlinedInputWithLabel';
|
||
|
import { ExpandablePanel } from '../Panels';
|
||
|
import SelectWithLabel from '../SelectWithLabel';
|
||
|
import SwitchWithLabel from '../SwitchWithLabel';
|
||
|
import { BodyText } from '../Text';
|
||
2 years ago
|
|
||
|
const CHECKED_STATES: Array<string | undefined> = ['1', 'on'];
|
||
2 years ago
|
const ID_SEPARATOR = '-';
|
||
2 years ago
|
|
||
|
const MAP_TO_INPUT_BUILDER: MapToInputBuilder = {
|
||
2 years ago
|
boolean: (args) => {
|
||
|
const { id, isChecked = false, label, name = id } = args;
|
||
|
|
||
|
return (
|
||
|
<InputWithRef
|
||
|
key={`${id}-wrapper`}
|
||
|
input={
|
||
|
<SwitchWithLabel
|
||
|
checked={isChecked}
|
||
|
id={id}
|
||
|
label={label}
|
||
|
name={name}
|
||
|
/>
|
||
|
}
|
||
|
valueType="boolean"
|
||
|
/>
|
||
|
);
|
||
|
},
|
||
|
select: (args) => {
|
||
|
const {
|
||
|
id,
|
||
|
isRequired,
|
||
|
label,
|
||
|
name = id,
|
||
|
selectOptions = [],
|
||
|
value = '',
|
||
|
} = args;
|
||
|
|
||
|
return (
|
||
|
<InputWithRef
|
||
|
key={`${id}-wrapper`}
|
||
|
input={
|
||
|
<SelectWithLabel
|
||
|
id={id}
|
||
|
label={label}
|
||
|
name={name}
|
||
|
selectItems={selectOptions}
|
||
|
value={value}
|
||
|
/>
|
||
|
}
|
||
|
required={isRequired}
|
||
|
/>
|
||
|
);
|
||
|
},
|
||
|
string: (args) => {
|
||
|
const {
|
||
|
id,
|
||
|
isRequired,
|
||
|
isSensitive = false,
|
||
|
label = '',
|
||
|
name = id,
|
||
|
value,
|
||
|
} = args;
|
||
|
|
||
|
return (
|
||
|
<InputWithRef
|
||
|
key={`${id}-wrapper`}
|
||
|
input={
|
||
|
<OutlinedInputWithLabel
|
||
|
id={id}
|
||
|
inputProps={{
|
||
|
inputProps: { 'data-sensitive': isSensitive },
|
||
|
}}
|
||
|
label={label}
|
||
|
name={name}
|
||
|
value={value}
|
||
|
type={isSensitive ? INPUT_TYPES.password : undefined}
|
||
|
/>
|
||
|
}
|
||
|
required={isRequired}
|
||
|
/>
|
||
|
);
|
||
|
},
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
const combineIds = (...pieces: string[]) => pieces.join(ID_SEPARATOR);
|
||
2 years ago
|
|
||
2 years ago
|
const FenceInputWrapper = styled(FlexBox)({
|
||
|
margin: '.4em 0',
|
||
|
});
|
||
|
|
||
2 years ago
|
const CommonFenceInputGroup: FC<CommonFenceInputGroupProps> = ({
|
||
|
fenceId,
|
||
2 years ago
|
fenceParameterTooltipProps,
|
||
2 years ago
|
fenceTemplate,
|
||
2 years ago
|
previousFenceName,
|
||
|
previousFenceParameters,
|
||
2 years ago
|
}) => {
|
||
|
const fenceParameterElements = useMemo(() => {
|
||
|
let result: ReactNode;
|
||
|
|
||
2 years ago
|
if (fenceTemplate && fenceId) {
|
||
|
const { parameters: fenceParameters } = fenceTemplate[fenceId];
|
||
2 years ago
|
|
||
2 years ago
|
let mapToPreviousFenceParameterValues: FenceParameters = {};
|
||
|
|
||
|
if (previousFenceParameters) {
|
||
|
mapToPreviousFenceParameterValues = Object.entries(
|
||
|
previousFenceParameters,
|
||
|
).reduce<FenceParameters>((previous, [parameterId, parameterValue]) => {
|
||
|
const newKey = combineIds(fenceId, parameterId);
|
||
|
|
||
|
previous[newKey] = parameterValue;
|
||
|
|
||
|
return previous;
|
||
|
}, {});
|
||
|
}
|
||
|
|
||
2 years ago
|
const { optional: optionalInputs, required: requiredInputs } =
|
||
2 years ago
|
Object.entries(fenceParameters)
|
||
|
.sort(([a], [b]) => (a > b ? 1 : -1))
|
||
|
.reduce<{
|
||
|
optional: ReactElement[];
|
||
|
required: ReactElement[];
|
||
|
}>(
|
||
|
(
|
||
|
previous,
|
||
|
[
|
||
|
parameterId,
|
||
|
{
|
||
2 years ago
|
content_type: parameterType,
|
||
2 years ago
|
default: parameterDefault,
|
||
2 years ago
|
deprecated: rawParameterDeprecated,
|
||
|
description: parameterDescription,
|
||
2 years ago
|
options: parameterSelectOptions,
|
||
2 years ago
|
required: rawParameterRequired,
|
||
2 years ago
|
},
|
||
|
],
|
||
|
) => {
|
||
2 years ago
|
const isParameterDeprecated =
|
||
|
String(rawParameterDeprecated) === '1';
|
||
2 years ago
|
|
||
|
if (!isParameterDeprecated) {
|
||
|
const { optional, required } = previous;
|
||
|
const buildInput =
|
||
2 years ago
|
MAP_TO_INPUT_BUILDER[parameterType] ??
|
||
2 years ago
|
MAP_TO_INPUT_BUILDER.string;
|
||
|
const fenceJoinParameterId = combineIds(fenceId, parameterId);
|
||
|
|
||
|
const initialValue =
|
||
|
mapToPreviousFenceParameterValues[fenceJoinParameterId] ??
|
||
|
parameterDefault;
|
||
2 years ago
|
const isParameterRequired =
|
||
|
String(rawParameterRequired) === '1';
|
||
|
const isParameterSensitive = /passw/i.test(parameterId);
|
||
2 years ago
|
|
||
|
const parameterInput = buildInput({
|
||
|
id: fenceJoinParameterId,
|
||
|
isChecked: CHECKED_STATES.includes(initialValue),
|
||
|
isRequired: isParameterRequired,
|
||
2 years ago
|
isSensitive: isParameterSensitive,
|
||
2 years ago
|
label: parameterId,
|
||
|
selectOptions: parameterSelectOptions,
|
||
|
value: initialValue,
|
||
|
});
|
||
2 years ago
|
const parameterInputWithTooltip = (
|
||
|
<Tooltip
|
||
|
componentsProps={{
|
||
|
tooltip: {
|
||
|
sx: {
|
||
|
maxWidth: { md: '62.6em' },
|
||
|
},
|
||
|
},
|
||
|
}}
|
||
|
disableInteractive
|
||
|
key={`${fenceJoinParameterId}-tooltip`}
|
||
|
placement="top-start"
|
||
|
title={<BodyText>{parameterDescription}</BodyText>}
|
||
|
{...fenceParameterTooltipProps}
|
||
|
>
|
||
|
<Box>{parameterInput}</Box>
|
||
|
</Tooltip>
|
||
|
);
|
||
2 years ago
|
|
||
|
if (isParameterRequired) {
|
||
2 years ago
|
required.push(parameterInputWithTooltip);
|
||
2 years ago
|
} else {
|
||
2 years ago
|
optional.push(parameterInputWithTooltip);
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
return previous;
|
||
|
},
|
||
|
{
|
||
|
optional: [],
|
||
|
required: [
|
||
|
MAP_TO_INPUT_BUILDER.string({
|
||
|
id: combineIds(fenceId, 'name'),
|
||
|
isRequired: true,
|
||
|
label: 'Fence device name',
|
||
|
value: previousFenceName,
|
||
|
}),
|
||
|
],
|
||
|
},
|
||
|
);
|
||
2 years ago
|
|
||
|
result = (
|
||
2 years ago
|
<FlexBox
|
||
|
sx={{
|
||
|
'& > div:first-child': { marginTop: 0 },
|
||
|
'& > div': { marginBottom: 0 },
|
||
|
}}
|
||
|
>
|
||
2 years ago
|
<ExpandablePanel expandInitially header="Required parameters">
|
||
2 years ago
|
<FenceInputWrapper>{requiredInputs}</FenceInputWrapper>
|
||
2 years ago
|
</ExpandablePanel>
|
||
|
<ExpandablePanel header="Optional parameters">
|
||
2 years ago
|
<FenceInputWrapper>{optionalInputs}</FenceInputWrapper>
|
||
2 years ago
|
</ExpandablePanel>
|
||
2 years ago
|
</FlexBox>
|
||
2 years ago
|
);
|
||
|
}
|
||
|
|
||
|
return result;
|
||
2 years ago
|
}, [
|
||
|
fenceId,
|
||
|
fenceParameterTooltipProps,
|
||
|
fenceTemplate,
|
||
|
previousFenceName,
|
||
|
previousFenceParameters,
|
||
|
]);
|
||
2 years ago
|
|
||
|
return <>{fenceParameterElements}</>;
|
||
|
};
|
||
|
|
||
2 years ago
|
export { ID_SEPARATOR };
|
||
|
|
||
2 years ago
|
export default CommonFenceInputGroup;
|