Wrapper.tsx 2.7 KB

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