DashboardPanel.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { StoreState } from 'app/types';
  4. import { initPanelState } from '../../panel/state/actions';
  5. import { setPanelInstanceState } from '../../panel/state/reducers';
  6. import { DashboardModel, PanelModel } from '../state';
  7. import { LazyLoader } from './LazyLoader';
  8. import { PanelChrome } from './PanelChrome';
  9. import { PanelChromeAngular } from './PanelChromeAngular';
  10. export interface OwnProps {
  11. panel: PanelModel;
  12. stateKey: string;
  13. dashboard: DashboardModel;
  14. isEditing: boolean;
  15. isViewing: boolean;
  16. width: number;
  17. height: number;
  18. lazy?: boolean;
  19. }
  20. const mapStateToProps = (state: StoreState, props: OwnProps) => {
  21. const panelState = state.panels[props.stateKey];
  22. if (!panelState) {
  23. return { plugin: null };
  24. }
  25. return {
  26. plugin: panelState.plugin,
  27. instanceState: panelState.instanceState,
  28. };
  29. };
  30. const mapDispatchToProps = {
  31. initPanelState,
  32. setPanelInstanceState,
  33. };
  34. const connector = connect(mapStateToProps, mapDispatchToProps);
  35. export type Props = OwnProps & ConnectedProps<typeof connector>;
  36. export class DashboardPanelUnconnected extends PureComponent<Props> {
  37. static defaultProps: Partial<Props> = {
  38. lazy: true,
  39. };
  40. componentDidMount() {
  41. this.props.panel.isInView = !this.props.lazy;
  42. if (!this.props.lazy) {
  43. this.onPanelLoad();
  44. }
  45. }
  46. onInstanceStateChange = (value: any) => {
  47. this.props.setPanelInstanceState({ key: this.props.stateKey, value });
  48. };
  49. onVisibilityChange = (v: boolean) => {
  50. this.props.panel.isInView = v;
  51. };
  52. onPanelLoad = () => {
  53. if (!this.props.plugin) {
  54. this.props.initPanelState(this.props.panel);
  55. }
  56. };
  57. render() {
  58. const { dashboard, panel, isViewing, isEditing, width, height, lazy, plugin } = this.props;
  59. const renderPanelChrome = (isInView: boolean) =>
  60. plugin &&
  61. (plugin.angularPanelCtrl ? (
  62. <PanelChromeAngular
  63. plugin={plugin}
  64. panel={panel}
  65. dashboard={dashboard}
  66. isViewing={isViewing}
  67. isEditing={isEditing}
  68. isInView={isInView}
  69. width={width}
  70. height={height}
  71. />
  72. ) : (
  73. <PanelChrome
  74. plugin={plugin}
  75. panel={panel}
  76. dashboard={dashboard}
  77. isViewing={isViewing}
  78. isEditing={isEditing}
  79. isInView={isInView}
  80. width={width}
  81. height={height}
  82. onInstanceStateChange={this.onInstanceStateChange}
  83. />
  84. ));
  85. return lazy ? (
  86. <LazyLoader width={width} height={height} onChange={this.onVisibilityChange} onLoad={this.onPanelLoad}>
  87. {({ isInView }) => renderPanelChrome(isInView)}
  88. </LazyLoader>
  89. ) : (
  90. renderPanelChrome(true)
  91. );
  92. }
  93. }
  94. export const DashboardPanel = connector(DashboardPanelUnconnected);