Wrapper.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 { serializeStateToUrlParam, parseUrlState } from 'app/core/utils/explore';
  6. import { StoreState } from 'app/types';
  7. import { ExploreState } from 'app/types/explore';
  8. import ErrorBoundary from './ErrorBoundary';
  9. import Explore from './Explore';
  10. interface WrapperProps {
  11. backendSrv?: any;
  12. datasourceSrv?: any;
  13. updateLocation: typeof updateLocation;
  14. urlStates: { [key: string]: string };
  15. }
  16. interface WrapperState {
  17. split: boolean;
  18. splitState: ExploreState;
  19. }
  20. const STATE_KEY_LEFT = 'state';
  21. const STATE_KEY_RIGHT = 'stateRight';
  22. export class Wrapper extends Component<WrapperProps, WrapperState> {
  23. urlStates: { [key: string]: string };
  24. constructor(props: WrapperProps) {
  25. super(props);
  26. this.urlStates = props.urlStates;
  27. this.state = {
  28. split: Boolean(props.urlStates[STATE_KEY_RIGHT]),
  29. splitState: undefined,
  30. };
  31. }
  32. onChangeSplit = (split: boolean, splitState: ExploreState) => {
  33. this.setState({ split, splitState });
  34. // When closing split, remove URL state for split part
  35. if (!split) {
  36. delete this.urlStates[STATE_KEY_RIGHT];
  37. this.props.updateLocation({
  38. query: this.urlStates,
  39. });
  40. }
  41. };
  42. onSaveState = (key: string, state: ExploreState) => {
  43. const urlState = serializeStateToUrlParam(state, true);
  44. this.urlStates[key] = urlState;
  45. this.props.updateLocation({
  46. query: this.urlStates,
  47. });
  48. };
  49. render() {
  50. const { datasourceSrv } = this.props;
  51. // State overrides for props from first Explore
  52. const { split, splitState } = this.state;
  53. const urlStateLeft = parseUrlState(this.urlStates[STATE_KEY_LEFT]);
  54. const urlStateRight = parseUrlState(this.urlStates[STATE_KEY_RIGHT]);
  55. return (
  56. <div className="explore-wrapper">
  57. <ErrorBoundary>
  58. <Explore
  59. datasourceSrv={datasourceSrv}
  60. onChangeSplit={this.onChangeSplit}
  61. onSaveState={this.onSaveState}
  62. position="left"
  63. split={split}
  64. stateKey={STATE_KEY_LEFT}
  65. urlState={urlStateLeft}
  66. />
  67. </ErrorBoundary>
  68. {split && (
  69. <ErrorBoundary>
  70. <Explore
  71. datasourceSrv={datasourceSrv}
  72. onChangeSplit={this.onChangeSplit}
  73. onSaveState={this.onSaveState}
  74. position="right"
  75. split={split}
  76. splitState={splitState}
  77. stateKey={STATE_KEY_RIGHT}
  78. urlState={urlStateRight}
  79. />
  80. </ErrorBoundary>
  81. )}
  82. </div>
  83. );
  84. }
  85. }
  86. const mapStateToProps = (state: StoreState) => ({
  87. urlStates: state.location.query,
  88. });
  89. const mapDispatchToProps = {
  90. updateLocation,
  91. };
  92. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(Wrapper));