Wrapper.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. };
  34. onSaveState = (key: string, state: ExploreState) => {
  35. const urlState = serializeStateToUrlParam(state);
  36. this.urlStates[key] = urlState;
  37. this.props.updateLocation({
  38. query: this.urlStates,
  39. });
  40. };
  41. render() {
  42. const { datasourceSrv } = this.props;
  43. // State overrides for props from first Explore
  44. const { split, splitState } = this.state;
  45. const urlStateLeft = parseUrlState(this.urlStates[STATE_KEY_LEFT]);
  46. const urlStateRight = parseUrlState(this.urlStates[STATE_KEY_RIGHT]);
  47. return (
  48. <div className="explore-wrapper">
  49. <Explore
  50. datasourceSrv={datasourceSrv}
  51. onChangeSplit={this.onChangeSplit}
  52. onSaveState={this.onSaveState}
  53. position="left"
  54. split={split}
  55. stateKey={STATE_KEY_LEFT}
  56. urlState={urlStateLeft}
  57. />
  58. {split && (
  59. <Explore
  60. datasourceSrv={datasourceSrv}
  61. onChangeSplit={this.onChangeSplit}
  62. onSaveState={this.onSaveState}
  63. position="right"
  64. split={split}
  65. splitState={splitState}
  66. stateKey={STATE_KEY_RIGHT}
  67. urlState={urlStateRight}
  68. />
  69. )}
  70. </div>
  71. );
  72. }
  73. }
  74. const mapStateToProps = (state: StoreState) => ({
  75. urlStates: state.location.query,
  76. });
  77. const mapDispatchToProps = {
  78. updateLocation,
  79. };
  80. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(Wrapper));