AddNewRule.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { useState } from 'react';
  2. import { DataSourceRef, LiveChannelScope, SelectableValue } from '@grafana/data';
  3. import { DataSourcePicker, getBackendSrv } from '@grafana/runtime';
  4. import { Input, Field, Button, ValuePicker, HorizontalGroup } from '@grafana/ui';
  5. import { useAppNotification } from 'app/core/copy/appNotification';
  6. import { Rule } from './types';
  7. interface Props {
  8. onRuleAdded: (rule: Rule) => void;
  9. }
  10. type PatternType = 'ds' | 'any';
  11. const patternTypes: Array<SelectableValue<PatternType>> = [
  12. {
  13. label: 'Data source',
  14. description: 'Configure a channel scoped to a data source instance',
  15. value: 'ds',
  16. },
  17. {
  18. label: 'Any',
  19. description: 'Enter an arbitray channel pattern',
  20. value: 'any',
  21. },
  22. ];
  23. export function AddNewRule({ onRuleAdded }: Props) {
  24. const [patternType, setPatternType] = useState<PatternType>();
  25. const [pattern, setPattern] = useState<string>();
  26. const [patternPrefix, setPatternPrefix] = useState<string>('');
  27. const [datasource, setDatasource] = useState<DataSourceRef>();
  28. const notifyApp = useAppNotification();
  29. const onSubmit = () => {
  30. if (!pattern) {
  31. notifyApp.error('Enter path');
  32. return;
  33. }
  34. if (patternType === 'ds' && !patternPrefix.length) {
  35. notifyApp.error('Select datasource');
  36. return;
  37. }
  38. getBackendSrv()
  39. .post(`api/live/channel-rules`, {
  40. pattern: patternPrefix + pattern,
  41. settings: {
  42. converter: {
  43. type: 'jsonAuto',
  44. },
  45. frameOutputs: [
  46. {
  47. type: 'managedStream',
  48. },
  49. ],
  50. },
  51. })
  52. .then((v: any) => {
  53. console.log('ADDED', v);
  54. setPattern(undefined);
  55. setPatternType(undefined);
  56. onRuleAdded(v.rule);
  57. })
  58. .catch((e) => {
  59. notifyApp.error('Error adding rule', e);
  60. e.isHandled = true;
  61. });
  62. };
  63. if (patternType) {
  64. return (
  65. <div>
  66. <HorizontalGroup>
  67. {patternType === 'any' && (
  68. <Field label="Pattern">
  69. <Input
  70. value={pattern ?? ''}
  71. onChange={(e) => setPattern(e.currentTarget.value)}
  72. placeholder="scope/namespace/path"
  73. />
  74. </Field>
  75. )}
  76. {patternType === 'ds' && (
  77. <>
  78. <Field label="Data source">
  79. <DataSourcePicker
  80. current={datasource}
  81. onChange={(ds) => {
  82. setDatasource(ds);
  83. setPatternPrefix(`${LiveChannelScope.DataSource}/${ds.uid}/`);
  84. }}
  85. />
  86. </Field>
  87. <Field label="Path">
  88. <Input value={pattern ?? ''} onChange={(e) => setPattern(e.currentTarget.value)} placeholder="path" />
  89. </Field>
  90. </>
  91. )}
  92. <Field label="">
  93. <Button onClick={onSubmit} variant={pattern?.length ? 'primary' : 'secondary'}>
  94. Add
  95. </Button>
  96. </Field>
  97. <Field label="">
  98. <Button variant="secondary" onClick={() => setPatternType(undefined)}>
  99. Cancel
  100. </Button>
  101. </Field>
  102. </HorizontalGroup>
  103. </div>
  104. );
  105. }
  106. return (
  107. <div>
  108. <ValuePicker
  109. label="Add channel rule"
  110. variant="secondary"
  111. size="md"
  112. icon="plus"
  113. menuPlacement="auto"
  114. isFullWidth={false}
  115. options={patternTypes}
  116. onChange={(v) => setPatternType(v.value)}
  117. />
  118. </div>
  119. );
  120. }