Wrapper.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 } from './state/actions';
  9. import ErrorBoundary from './ErrorBoundary';
  10. import Explore from './Explore';
  11. interface WrapperProps {
  12. initializeExploreSplit: typeof initializeExploreSplit;
  13. split: boolean;
  14. updateLocation: typeof updateLocation;
  15. urlStates: { [key: string]: string };
  16. }
  17. export class Wrapper extends Component<WrapperProps> {
  18. initialSplit: boolean;
  19. urlStates: { [key: string]: ExploreUrlState };
  20. constructor(props: WrapperProps) {
  21. super(props);
  22. this.urlStates = {};
  23. const { left, right } = props.urlStates;
  24. if (props.urlStates.left) {
  25. this.urlStates.leftState = parseUrlState(left);
  26. }
  27. if (props.urlStates.right) {
  28. this.urlStates.rightState = parseUrlState(right);
  29. this.initialSplit = true;
  30. }
  31. }
  32. componentDidMount() {
  33. if (this.initialSplit) {
  34. this.props.initializeExploreSplit();
  35. }
  36. }
  37. render() {
  38. const { split } = this.props;
  39. const { leftState, rightState } = this.urlStates;
  40. return (
  41. <div className="explore-wrapper">
  42. <ErrorBoundary>
  43. <Explore exploreId={ExploreId.left} urlState={leftState} />
  44. </ErrorBoundary>
  45. {split && (
  46. <ErrorBoundary>
  47. <Explore exploreId={ExploreId.right} urlState={rightState} />
  48. </ErrorBoundary>
  49. )}
  50. </div>
  51. );
  52. }
  53. }
  54. const mapStateToProps = (state: StoreState) => {
  55. const urlStates = state.location.query;
  56. const { split } = state.explore;
  57. return { split, urlStates };
  58. };
  59. const mapDispatchToProps = {
  60. initializeExploreSplit,
  61. updateLocation,
  62. };
  63. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(Wrapper));