2021-10-06 14:55:54 -05:00
|
|
|
import React, { useState } from 'react';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-08-03 08:47:09 -05:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
2021-10-06 14:55:54 -05:00
|
|
|
import { Select } from '@grafana/ui';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { RuleSettingsEditor } from './RuleSettingsEditor';
|
|
|
|
import { RuleType, RuleSetting, PipeLineEntitiesInfo } from './types';
|
|
|
|
|
2021-10-06 14:55:54 -05:00
|
|
|
interface Props {
|
|
|
|
ruleType: RuleType;
|
|
|
|
onChange: (value: RuleSetting[]) => void;
|
|
|
|
value: RuleSetting[];
|
|
|
|
entitiesInfo: PipeLineEntitiesInfo;
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:54:34 -05:00
|
|
|
export const RuleSettingsArray = ({ onChange, value, ruleType, entitiesInfo }: Props) => {
|
2021-10-06 14:55:54 -05:00
|
|
|
const [index, setIndex] = useState<number>(0);
|
|
|
|
const arr = value ?? [];
|
|
|
|
const onRuleChange = (v: RuleSetting) => {
|
|
|
|
if (!value) {
|
|
|
|
onChange([v]);
|
|
|
|
} else {
|
|
|
|
const copy = [...value];
|
|
|
|
copy[index] = v;
|
|
|
|
onChange(copy);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// create array of value.length + 1
|
|
|
|
let indexArr: Array<SelectableValue<number>> = [];
|
|
|
|
for (let i = 0; i <= arr.length; i++) {
|
|
|
|
indexArr.push({
|
|
|
|
label: `${ruleType}: ${i}`,
|
|
|
|
value: i,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Select
|
|
|
|
placeholder="Select an index"
|
|
|
|
options={indexArr}
|
|
|
|
value={index}
|
|
|
|
onChange={(index) => {
|
|
|
|
// set index to find the correct setting
|
|
|
|
setIndex(index.value!);
|
|
|
|
}}
|
|
|
|
></Select>
|
|
|
|
<RuleSettingsEditor onChange={onRuleChange} value={arr[index]} ruleType={ruleType} entitiesInfo={entitiesInfo} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|