import React, { useState } from 'react'; import { DataSourceRef, LiveChannelScope, SelectableValue } from '@grafana/data'; import { DataSourcePicker, getBackendSrv } from '@grafana/runtime'; import { Input, Field, Button, ValuePicker, HorizontalGroup } from '@grafana/ui'; import { useAppNotification } from 'app/core/copy/appNotification'; import { Rule } from './types'; interface Props { onRuleAdded: (rule: Rule) => void; } type PatternType = 'ds' | 'any'; const patternTypes: Array> = [ { label: 'Data source', description: 'Configure a channel scoped to a data source instance', value: 'ds', }, { label: 'Any', description: 'Enter an arbitray channel pattern', value: 'any', }, ]; export function AddNewRule({ onRuleAdded }: Props) { const [patternType, setPatternType] = useState(); const [pattern, setPattern] = useState(); const [patternPrefix, setPatternPrefix] = useState(''); const [datasource, setDatasource] = useState(); const notifyApp = useAppNotification(); const onSubmit = () => { if (!pattern) { notifyApp.error('Enter path'); return; } if (patternType === 'ds' && !patternPrefix.length) { notifyApp.error('Select datasource'); return; } getBackendSrv() .post(`api/live/channel-rules`, { pattern: patternPrefix + pattern, settings: { converter: { type: 'jsonAuto', }, frameOutputs: [ { type: 'managedStream', }, ], }, }) .then((v: any) => { console.log('ADDED', v); setPattern(undefined); setPatternType(undefined); onRuleAdded(v.rule); }) .catch((e) => { notifyApp.error('Error adding rule', e); e.isHandled = true; }); }; if (patternType) { return (
{patternType === 'any' && ( setPattern(e.currentTarget.value)} placeholder="scope/namespace/path" /> )} {patternType === 'ds' && ( <> { setDatasource(ds); setPatternPrefix(`${LiveChannelScope.DataSource}/${ds.uid}/`); }} /> setPattern(e.currentTarget.value)} placeholder="path" /> )}
); } return (
setPatternType(v.value)} />
); }