AlertTab.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Services & Utils
  4. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  5. import appEvents from 'app/core/app_events';
  6. // Components
  7. import { EditorTabBody, EditorToolbarView } from '../dashboard/dashgrid/EditorTabBody';
  8. import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
  9. import StateHistory from './StateHistory';
  10. import 'app/features/alerting/AlertTabCtrl';
  11. // Types
  12. import { DashboardModel } from '../dashboard/dashboard_model';
  13. import { PanelModel } from '../dashboard/panel_model';
  14. interface Props {
  15. angularPanel?: AngularComponent;
  16. dashboard: DashboardModel;
  17. panel: PanelModel;
  18. }
  19. export class AlertTab extends PureComponent<Props> {
  20. element: any;
  21. component: AngularComponent;
  22. panelCtrl: any;
  23. componentDidMount() {
  24. if (this.shouldLoadAlertTab()) {
  25. this.loadAlertTab();
  26. }
  27. }
  28. componentDidUpdate(prevProps: Props) {
  29. if (this.shouldLoadAlertTab()) {
  30. this.loadAlertTab();
  31. }
  32. }
  33. shouldLoadAlertTab() {
  34. return this.props.angularPanel && this.element && !this.component;
  35. }
  36. componentWillUnmount() {
  37. if (this.component) {
  38. this.component.destroy();
  39. }
  40. }
  41. loadAlertTab() {
  42. const { angularPanel } = this.props;
  43. const scope = angularPanel.getScope();
  44. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  45. if (!scope.$$childHead) {
  46. setTimeout(() => {
  47. this.forceUpdate();
  48. });
  49. return;
  50. }
  51. this.panelCtrl = scope.$$childHead.ctrl;
  52. const loader = getAngularLoader();
  53. const template = '<alert-tab />';
  54. const scopeProps = {
  55. ctrl: this.panelCtrl,
  56. };
  57. this.component = loader.load(this.element, scopeProps, template);
  58. }
  59. stateHistory = (): EditorToolbarView => {
  60. return {
  61. title: 'State history',
  62. render: () => {
  63. return (
  64. <StateHistory
  65. dashboard={this.props.dashboard}
  66. panelId={this.props.panel.id}
  67. onRefresh={this.panelCtrl.refresh}
  68. />
  69. );
  70. },
  71. };
  72. };
  73. deleteAlert = (): EditorToolbarView => {
  74. const { panel } = this.props;
  75. return {
  76. title: 'Delete',
  77. btnType: 'danger',
  78. onClick: () => {
  79. appEvents.emit('confirm-modal', {
  80. title: 'Delete Alert',
  81. text: 'Are you sure you want to delete this alert rule?',
  82. text2: 'You need to save dashboard for the delete to take effect',
  83. icon: 'fa-trash',
  84. yesText: 'Delete',
  85. onConfirm: () => {
  86. delete panel.alert;
  87. panel.thresholds = [];
  88. this.panelCtrl.alertState = null;
  89. this.panelCtrl.render();
  90. this.forceUpdate();
  91. },
  92. });
  93. },
  94. };
  95. };
  96. onAddAlert = () => {
  97. this.panelCtrl._enableAlert();
  98. this.component.digest();
  99. this.forceUpdate();
  100. };
  101. render() {
  102. const { alert } = this.props.panel;
  103. const toolbarItems = alert ? [this.stateHistory(), this.deleteAlert()] : [];
  104. const model = {
  105. title: 'Panel has no alert rule defined',
  106. icon: 'icon-gf icon-gf-alert',
  107. onClick: this.onAddAlert,
  108. buttonTitle: 'Create Alert',
  109. };
  110. return (
  111. <EditorTabBody heading="Alert" toolbarItems={toolbarItems}>
  112. <>
  113. <div ref={element => (this.element = element)} />
  114. {!alert && <EmptyListCTA model={model} />}
  115. </>
  116. </EditorTabBody>
  117. );
  118. }
  119. }