AlertTab.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React, { PureComponent } from 'react';
  2. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  3. import { EditorTabBody } from './EditorTabBody';
  4. import 'app/features/alerting/AlertTabCtrl';
  5. interface Props {
  6. angularPanel?: AngularComponent;
  7. }
  8. export class AlertTab extends PureComponent<Props> {
  9. element: any;
  10. component: AngularComponent;
  11. constructor(props) {
  12. super(props);
  13. }
  14. componentDidMount() {
  15. if (this.shouldLoadAlertTab()) {
  16. this.loadAlertTab();
  17. }
  18. }
  19. componentDidUpdate(prevProps: Props) {
  20. if (this.shouldLoadAlertTab()) {
  21. this.loadAlertTab();
  22. }
  23. }
  24. shouldLoadAlertTab() {
  25. return this.props.angularPanel && this.element;
  26. }
  27. componentWillUnmount() {
  28. if (this.component) {
  29. this.component.destroy();
  30. }
  31. }
  32. loadAlertTab() {
  33. const { angularPanel } = this.props;
  34. const scope = angularPanel.getScope();
  35. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  36. if (!scope.$$childHead) {
  37. setTimeout(() => {
  38. this.forceUpdate();
  39. });
  40. return;
  41. }
  42. const panelCtrl = scope.$$childHead.ctrl;
  43. const loader = getAngularLoader();
  44. const template = '<alert-tab />';
  45. const scopeProps = {
  46. ctrl: panelCtrl,
  47. };
  48. this.component = loader.load(this.element, scopeProps, template);
  49. }
  50. render() {
  51. return (
  52. <EditorTabBody heading="Alert" toolbarItems={[]}>
  53. <div ref={element => (this.element = element)} />
  54. </EditorTabBody>
  55. );
  56. }
  57. }