2018-09-28 10:38:50 -05:00
|
|
|
import React, { Component } from 'react';
|
2018-09-28 09:44:07 -05:00
|
|
|
import { hot } from 'react-hot-loader';
|
|
|
|
import { connect } from 'react-redux';
|
2018-05-15 10:07:38 -05:00
|
|
|
|
2018-09-28 09:44:07 -05:00
|
|
|
import { updateLocation } from 'app/core/actions';
|
2018-10-01 05:56:26 -05:00
|
|
|
import { serializeStateToUrlParam, parseUrlState } from 'app/core/utils/explore';
|
2018-09-28 09:44:07 -05:00
|
|
|
import { StoreState } from 'app/types';
|
2018-10-01 05:56:26 -05:00
|
|
|
import { ExploreState } from 'app/types/explore';
|
2018-05-15 10:07:38 -05:00
|
|
|
|
2018-10-26 11:16:00 -05:00
|
|
|
import ErrorBoundary from './ErrorBoundary';
|
2018-10-01 05:56:26 -05:00
|
|
|
import Explore from './Explore';
|
2018-09-28 09:44:07 -05:00
|
|
|
|
|
|
|
interface WrapperProps {
|
|
|
|
backendSrv?: any;
|
|
|
|
datasourceSrv?: any;
|
|
|
|
updateLocation: typeof updateLocation;
|
|
|
|
urlStates: { [key: string]: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WrapperState {
|
|
|
|
split: boolean;
|
|
|
|
splitState: ExploreState;
|
|
|
|
}
|
|
|
|
|
|
|
|
const STATE_KEY_LEFT = 'state';
|
|
|
|
const STATE_KEY_RIGHT = 'stateRight';
|
|
|
|
|
2018-09-28 10:38:50 -05:00
|
|
|
export class Wrapper extends Component<WrapperProps, WrapperState> {
|
2018-09-28 09:44:07 -05:00
|
|
|
urlStates: { [key: string]: string };
|
|
|
|
|
|
|
|
constructor(props: WrapperProps) {
|
|
|
|
super(props);
|
|
|
|
this.urlStates = props.urlStates;
|
|
|
|
this.state = {
|
|
|
|
split: Boolean(props.urlStates[STATE_KEY_RIGHT]),
|
|
|
|
splitState: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeSplit = (split: boolean, splitState: ExploreState) => {
|
|
|
|
this.setState({ split, splitState });
|
2018-10-05 10:09:40 -05:00
|
|
|
// When closing split, remove URL state for split part
|
|
|
|
if (!split) {
|
|
|
|
delete this.urlStates[STATE_KEY_RIGHT];
|
|
|
|
this.props.updateLocation({
|
|
|
|
query: this.urlStates,
|
|
|
|
});
|
|
|
|
}
|
2018-05-15 10:07:38 -05:00
|
|
|
};
|
|
|
|
|
2018-09-28 09:44:07 -05:00
|
|
|
onSaveState = (key: string, state: ExploreState) => {
|
2018-10-05 10:09:40 -05:00
|
|
|
const urlState = serializeStateToUrlParam(state, true);
|
2018-09-28 09:44:07 -05:00
|
|
|
this.urlStates[key] = urlState;
|
|
|
|
this.props.updateLocation({
|
|
|
|
query: this.urlStates,
|
|
|
|
});
|
2018-05-15 10:07:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2018-09-28 09:44:07 -05:00
|
|
|
const { datasourceSrv } = this.props;
|
2018-05-15 10:07:38 -05:00
|
|
|
// State overrides for props from first Explore
|
2018-09-28 09:44:07 -05:00
|
|
|
const { split, splitState } = this.state;
|
|
|
|
const urlStateLeft = parseUrlState(this.urlStates[STATE_KEY_LEFT]);
|
|
|
|
const urlStateRight = parseUrlState(this.urlStates[STATE_KEY_RIGHT]);
|
2018-10-26 11:16:00 -05:00
|
|
|
|
2018-05-15 10:07:38 -05:00
|
|
|
return (
|
|
|
|
<div className="explore-wrapper">
|
2018-10-26 11:16:00 -05:00
|
|
|
<ErrorBoundary>
|
2018-05-15 10:07:38 -05:00
|
|
|
<Explore
|
2018-09-28 09:44:07 -05:00
|
|
|
datasourceSrv={datasourceSrv}
|
|
|
|
onChangeSplit={this.onChangeSplit}
|
|
|
|
onSaveState={this.onSaveState}
|
2018-10-26 11:16:00 -05:00
|
|
|
position="left"
|
2018-05-15 10:07:38 -05:00
|
|
|
split={split}
|
2018-10-26 11:16:00 -05:00
|
|
|
stateKey={STATE_KEY_LEFT}
|
|
|
|
urlState={urlStateLeft}
|
2018-05-15 10:07:38 -05:00
|
|
|
/>
|
2018-10-26 11:16:00 -05:00
|
|
|
</ErrorBoundary>
|
|
|
|
{split && (
|
|
|
|
<ErrorBoundary>
|
|
|
|
<Explore
|
|
|
|
datasourceSrv={datasourceSrv}
|
|
|
|
onChangeSplit={this.onChangeSplit}
|
|
|
|
onSaveState={this.onSaveState}
|
|
|
|
position="right"
|
|
|
|
split={split}
|
|
|
|
splitState={splitState}
|
|
|
|
stateKey={STATE_KEY_RIGHT}
|
|
|
|
urlState={urlStateRight}
|
|
|
|
/>
|
|
|
|
</ErrorBoundary>
|
2018-09-28 09:44:07 -05:00
|
|
|
)}
|
2018-05-15 10:07:38 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 09:44:07 -05:00
|
|
|
|
|
|
|
const mapStateToProps = (state: StoreState) => ({
|
|
|
|
urlStates: state.location.query,
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = {
|
|
|
|
updateLocation,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(Wrapper));
|