2021-09-30 15:37:31 -05:00
|
|
|
import React, { useState, useMemo } from 'react';
|
|
|
|
import { Modal, TabContent, TabsBar, Tab, Button, useStyles } from '@grafana/ui';
|
|
|
|
import { Rule, RuleType, PipeLineEntitiesInfo, RuleSetting } from './types';
|
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
|
|
import { css } from '@emotion/css';
|
|
|
|
import { GrafanaTheme } from '@grafana/data';
|
|
|
|
import { RuleSettingsEditor } from './RuleSettingsEditor';
|
|
|
|
import { getPipeLineEntities } from './utils';
|
2021-10-06 14:55:54 -05:00
|
|
|
import { RuleSettingsArray } from './RuleSettingsArray';
|
2021-10-22 10:56:16 -05:00
|
|
|
import { RuleTest } from './RuleTest';
|
2021-09-13 23:09:55 -05:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
rule: Rule;
|
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
2021-09-30 15:37:31 -05:00
|
|
|
clickColumn: RuleType;
|
2021-09-13 23:09:55 -05:00
|
|
|
}
|
2021-10-22 10:56:16 -05:00
|
|
|
interface TabInfo {
|
2021-09-30 15:37:31 -05:00
|
|
|
label: string;
|
2021-10-22 10:56:16 -05:00
|
|
|
type?: RuleType;
|
|
|
|
isTest?: boolean;
|
|
|
|
isConverter?: boolean;
|
|
|
|
icon?: string;
|
2021-09-30 15:37:31 -05:00
|
|
|
}
|
2021-10-22 10:56:16 -05:00
|
|
|
const tabs: TabInfo[] = [
|
|
|
|
{ label: 'Converter', type: 'converter', isConverter: true },
|
|
|
|
{ label: 'Processors', type: 'frameProcessors' },
|
|
|
|
{ label: 'Outputs', type: 'frameOutputs' },
|
|
|
|
{ label: 'Test', isTest: true, icon: 'flask' },
|
2021-09-13 23:09:55 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
export const RuleModal: React.FC<Props> = (props) => {
|
2021-09-30 15:37:31 -05:00
|
|
|
const { isOpen, onClose, clickColumn } = props;
|
|
|
|
const [rule, setRule] = useState<Rule>(props.rule);
|
2021-10-22 10:56:16 -05:00
|
|
|
const [activeTab, setActiveTab] = useState<TabInfo | undefined>(tabs.find((t) => t.type === clickColumn));
|
2021-09-30 15:37:31 -05:00
|
|
|
// to show color of Save button
|
|
|
|
const [hasChange, setChange] = useState<boolean>(false);
|
2021-10-22 10:56:16 -05:00
|
|
|
const [ruleSetting, setRuleSetting] = useState<any>(activeTab?.type ? rule?.settings?.[activeTab.type] : undefined);
|
2021-09-30 15:37:31 -05:00
|
|
|
const [entitiesInfo, setEntitiesInfo] = useState<PipeLineEntitiesInfo>();
|
|
|
|
const styles = useStyles(getStyles);
|
|
|
|
|
2021-10-06 14:55:54 -05:00
|
|
|
const onRuleSettingChange = (value: RuleSetting | RuleSetting[]) => {
|
2021-09-30 15:37:31 -05:00
|
|
|
setChange(true);
|
2021-10-22 10:56:16 -05:00
|
|
|
if (activeTab?.type) {
|
|
|
|
setRule({
|
|
|
|
...rule,
|
|
|
|
settings: {
|
|
|
|
...rule.settings,
|
|
|
|
[activeTab?.type]: value,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-09-30 15:37:31 -05:00
|
|
|
setRuleSetting(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
// load pipeline entities info
|
|
|
|
useMemo(() => {
|
|
|
|
getPipeLineEntities().then((data) => {
|
|
|
|
setEntitiesInfo(data);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onSave = () => {
|
|
|
|
getBackendSrv()
|
|
|
|
.put(`api/live/channel-rules`, rule)
|
|
|
|
.then(() => {
|
|
|
|
setChange(false);
|
2021-10-06 14:55:54 -05:00
|
|
|
onClose();
|
2021-09-30 15:37:31 -05:00
|
|
|
})
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
};
|
2021-09-13 23:09:55 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal isOpen={isOpen} title={rule.pattern} onDismiss={onClose} closeOnEscape>
|
|
|
|
<TabsBar>
|
|
|
|
{tabs.map((tab, index) => {
|
|
|
|
return (
|
|
|
|
<Tab
|
|
|
|
key={index}
|
|
|
|
label={tab.label}
|
2021-10-22 10:56:16 -05:00
|
|
|
active={tab === activeTab}
|
|
|
|
icon={tab.icon as any}
|
2021-09-13 23:09:55 -05:00
|
|
|
onChangeTab={() => {
|
2021-10-22 10:56:16 -05:00
|
|
|
setActiveTab(tab);
|
|
|
|
if (tab.type) {
|
|
|
|
// to notify children of the new rule
|
|
|
|
setRuleSetting(rule?.settings?.[tab.type]);
|
|
|
|
}
|
2021-09-13 23:09:55 -05:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</TabsBar>
|
|
|
|
<TabContent>
|
2021-10-22 10:56:16 -05:00
|
|
|
{entitiesInfo && rule && activeTab && (
|
|
|
|
<>
|
|
|
|
{activeTab?.isTest && <RuleTest rule={rule} />}
|
|
|
|
{activeTab.isConverter && (
|
|
|
|
<RuleSettingsEditor
|
|
|
|
onChange={onRuleSettingChange}
|
|
|
|
value={ruleSetting}
|
|
|
|
ruleType={'converter'}
|
|
|
|
entitiesInfo={entitiesInfo}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{!activeTab.isConverter && activeTab.type && (
|
|
|
|
<RuleSettingsArray
|
|
|
|
onChange={onRuleSettingChange}
|
|
|
|
value={ruleSetting}
|
|
|
|
ruleType={activeTab.type}
|
|
|
|
entitiesInfo={entitiesInfo}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
2021-10-06 14:55:54 -05:00
|
|
|
)}
|
2021-09-30 15:37:31 -05:00
|
|
|
<Button onClick={onSave} className={styles.save} variant={hasChange ? 'primary' : 'secondary'}>
|
|
|
|
Save
|
|
|
|
</Button>
|
2021-09-13 23:09:55 -05:00
|
|
|
</TabContent>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-30 15:37:31 -05:00
|
|
|
const getStyles = (theme: GrafanaTheme) => {
|
|
|
|
return {
|
|
|
|
save: css`
|
|
|
|
margin-top: 5px;
|
|
|
|
`,
|
|
|
|
};
|
2021-09-13 23:09:55 -05:00
|
|
|
};
|