Wrapper.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React, { Component } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { updateLocation } from 'app/core/actions';
  5. import { StoreState } from 'app/types';
  6. import { ExploreId, ExploreUrlState } from 'app/types/explore';
  7. import { parseUrlState } from 'app/core/utils/explore';
  8. import { initializeExploreSplit, resetExplore } from './state/actions';
  9. import ErrorBoundary from './ErrorBoundary';
  10. import Explore from './Explore';
  11. import { CustomScrollbar } from '@grafana/ui';
  12. interface WrapperProps {
  13. initializeExploreSplit: typeof initializeExploreSplit;
  14. split: boolean;
  15. updateLocation: typeof updateLocation;
  16. resetExplore: typeof resetExplore;
  17. urlStates: { [key: string]: string };
  18. }
  19. export class Wrapper extends Component<WrapperProps> {
  20. initialSplit: boolean;
  21. urlStates: { [key: string]: ExploreUrlState };
  22. constructor(props: WrapperProps) {
  23. super(props);
  24. this.urlStates = {};
  25. const { left, right } = props.urlStates;
  26. if (props.urlStates.left) {
  27. this.urlStates.leftState = parseUrlState(left);
  28. }
  29. if (props.urlStates.right) {
  30. this.urlStates.rightState = parseUrlState(right);
  31. this.initialSplit = true;
  32. }
  33. }
  34. componentDidMount() {
  35. if (this.initialSplit) {
  36. this.props.initializeExploreSplit();
  37. }
  38. }
  39. componentWillUnmount() {
  40. this.props.resetExplore();
  41. }
  42. render() {
  43. const { split } = this.props;
  44. const { leftState, rightState } = this.urlStates;
  45. return (
  46. <div className="page-scrollbar-wrapper">
  47. <CustomScrollbar autoHeightMin={'100%'}>
  48. <div className="explore-wrapper">
  49. <ErrorBoundary>
  50. <Explore exploreId={ExploreId.left} urlState={leftState} />
  51. </ErrorBoundary>
  52. {split && (
  53. <ErrorBoundary>
  54. <Explore exploreId={ExploreId.right} urlState={rightState} />
  55. </ErrorBoundary>
  56. )}
  57. </div>
  58. </CustomScrollbar>
  59. </div>
  60. );
  61. }
  62. }
  63. const mapStateToProps = (state: StoreState) => {
  64. const urlStates = state.location.query;
  65. const { split } = state.explore;
  66. return { split, urlStates };
  67. };
  68. const mapDispatchToProps = {
  69. initializeExploreSplit,
  70. updateLocation,
  71. resetExplore,
  72. };
  73. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(Wrapper));