PipelineAdminPage.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React, { useEffect, useState, ChangeEvent } from 'react';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. import { Input } from '@grafana/ui';
  4. import Page from 'app/core/components/Page/Page';
  5. import { useNavModel } from 'app/core/hooks/useNavModel';
  6. import { AddNewRule } from './AddNewRule';
  7. import { PipelineTable } from './PipelineTable';
  8. import { Rule } from './types';
  9. export default function PipelineAdminPage() {
  10. const [rules, setRules] = useState<Rule[]>([]);
  11. const [defaultRules, setDefaultRules] = useState<any[]>([]);
  12. const [newRule, setNewRule] = useState<Rule>();
  13. const navModel = useNavModel('live-pipeline');
  14. const [error, setError] = useState<string>();
  15. const loadRules = () => {
  16. getBackendSrv()
  17. .get(`api/live/channel-rules`)
  18. .then((data) => {
  19. setRules(data.rules ?? []);
  20. setDefaultRules(data.rules ?? []);
  21. })
  22. .catch((e) => {
  23. if (e.data) {
  24. setError(JSON.stringify(e.data, null, 2));
  25. }
  26. });
  27. };
  28. useEffect(() => {
  29. loadRules();
  30. }, []);
  31. const onSearchQueryChange = (e: ChangeEvent<HTMLInputElement>) => {
  32. if (e.target.value) {
  33. setRules(rules.filter((rule) => rule.pattern.toLowerCase().includes(e.target.value.toLowerCase())));
  34. } else {
  35. setRules(defaultRules);
  36. }
  37. };
  38. return (
  39. <Page navModel={navModel}>
  40. <Page.Contents>
  41. {error && <pre>{error}</pre>}
  42. <div className="page-action-bar">
  43. <div className="gf-form gf-form--grow">
  44. <Input placeholder="Search pattern..." onChange={onSearchQueryChange} />
  45. </div>
  46. </div>
  47. <PipelineTable rules={rules} onRuleChanged={loadRules} selectRule={newRule} />
  48. <AddNewRule
  49. onRuleAdded={(r: Rule) => {
  50. console.log('GOT', r, 'vs', rules[0]);
  51. setNewRule(r);
  52. loadRules();
  53. }}
  54. />
  55. </Page.Contents>
  56. </Page>
  57. );
  58. }