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