GraphContainer.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { RawTimeRange, TimeRange } from '@grafana/ui';
  5. import { ExploreId, ExploreItemState } from 'app/types/explore';
  6. import { StoreState } from 'app/types';
  7. import { toggleGraph } from './state/actions';
  8. import Graph from './Graph';
  9. import Panel from './Panel';
  10. interface GraphContainerProps {
  11. onChangeTime: (range: TimeRange) => void;
  12. exploreId: ExploreId;
  13. graphResult?: any[];
  14. loading: boolean;
  15. range: RawTimeRange;
  16. showingGraph: boolean;
  17. showingTable: boolean;
  18. split: boolean;
  19. toggleGraph: typeof toggleGraph;
  20. }
  21. export class GraphContainer extends PureComponent<GraphContainerProps> {
  22. onClickGraphButton = () => {
  23. this.props.toggleGraph(this.props.exploreId);
  24. };
  25. render() {
  26. const { exploreId, graphResult, loading, onChangeTime, showingGraph, showingTable, range, split } = this.props;
  27. const graphHeight = showingGraph && showingTable ? '200px' : '400px';
  28. if (!graphResult) {
  29. return null;
  30. }
  31. return (
  32. <Panel label="Graph" isOpen={showingGraph} loading={loading} onToggle={this.onClickGraphButton}>
  33. <Graph
  34. data={graphResult}
  35. height={graphHeight}
  36. id={`explore-graph-${exploreId}`}
  37. onChangeTime={onChangeTime}
  38. range={range}
  39. split={split}
  40. />
  41. </Panel>
  42. );
  43. }
  44. }
  45. function mapStateToProps(state: StoreState, { exploreId }) {
  46. const explore = state.explore;
  47. const { split } = explore;
  48. const item: ExploreItemState = explore[exploreId];
  49. const { graphResult, queryTransactions, range, showingGraph, showingTable } = item;
  50. const loading = queryTransactions.some(qt => qt.resultType === 'Graph' && !qt.done);
  51. return { graphResult, loading, range, showingGraph, showingTable, split };
  52. }
  53. const mapDispatchToProps = {
  54. toggleGraph,
  55. };
  56. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(GraphContainer));