ExploreToolbar.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'react-redux';
  3. import { hot } from 'react-hot-loader';
  4. import { ExploreId } from 'app/types/explore';
  5. import { DataSourceSelectItem, RawTimeRange, TimeRange } from '@grafana/ui';
  6. import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker';
  7. import { StoreState } from 'app/types/store';
  8. import { changeDatasource, clearQueries, splitClose, runQueries, splitOpen } from './state/actions';
  9. import TimePicker from './TimePicker';
  10. import { ClickOutsideWrapper } from 'app/core/components/ClickOutsideWrapper/ClickOutsideWrapper';
  11. enum IconSide {
  12. left = 'left',
  13. right = 'right',
  14. }
  15. const createResponsiveButton = (options: {
  16. splitted: boolean;
  17. title: string;
  18. onClick: () => void;
  19. buttonClassName?: string;
  20. iconClassName?: string;
  21. iconSide?: IconSide;
  22. }) => {
  23. const defaultOptions = {
  24. iconSide: IconSide.left,
  25. };
  26. const props = { ...options, defaultOptions };
  27. const { title, onClick, buttonClassName, iconClassName, splitted, iconSide } = props;
  28. return (
  29. <button className={`btn navbar-button ${buttonClassName ? buttonClassName : ''}`} onClick={onClick}>
  30. {iconClassName && iconSide === IconSide.left ? <i className={`${iconClassName} icon-margin-right`} /> : null}
  31. <span className="btn-title">{!splitted ? title : ''}</span>
  32. {iconClassName && iconSide === IconSide.right ? <i className={`${iconClassName} icon-margin-left`} /> : null}
  33. </button>
  34. );
  35. };
  36. interface OwnProps {
  37. exploreId: ExploreId;
  38. timepickerRef: React.RefObject<TimePicker>;
  39. onChangeTime: (range: TimeRange, changedByScanner?: boolean) => void;
  40. }
  41. interface StateProps {
  42. datasourceMissing: boolean;
  43. exploreDatasources: DataSourceSelectItem[];
  44. loading: boolean;
  45. range: RawTimeRange;
  46. selectedDatasource: DataSourceSelectItem;
  47. splitted: boolean;
  48. }
  49. interface DispatchProps {
  50. changeDatasource: typeof changeDatasource;
  51. clearAll: typeof clearQueries;
  52. runQuery: typeof runQueries;
  53. closeSplit: typeof splitClose;
  54. split: typeof splitOpen;
  55. }
  56. type Props = StateProps & DispatchProps & OwnProps;
  57. export class UnConnectedExploreToolbar extends PureComponent<Props, {}> {
  58. constructor(props) {
  59. super(props);
  60. }
  61. onChangeDatasource = async option => {
  62. this.props.changeDatasource(this.props.exploreId, option.value);
  63. };
  64. onClearAll = () => {
  65. this.props.clearAll(this.props.exploreId);
  66. };
  67. onRunQuery = () => {
  68. this.props.runQuery(this.props.exploreId);
  69. };
  70. onCloseTimePicker = () => {
  71. this.props.timepickerRef.current.setState({ isOpen: false });
  72. };
  73. render() {
  74. const {
  75. datasourceMissing,
  76. exploreDatasources,
  77. exploreId,
  78. loading,
  79. range,
  80. selectedDatasource,
  81. splitted,
  82. timepickerRef,
  83. } = this.props;
  84. return (
  85. <div className={splitted ? 'explore-toolbar splitted' : 'explore-toolbar'}>
  86. <div className="explore-toolbar-item">
  87. <div className="explore-toolbar-header">
  88. <div className="explore-toolbar-header-title">
  89. {exploreId === 'left' && (
  90. <span className="navbar-page-btn">
  91. <i className="gicon gicon-explore" />
  92. Explore
  93. </span>
  94. )}
  95. </div>
  96. <a className="explore-toolbar-header-close" onClick={() => this.props.closeSplit(exploreId)}>
  97. <i className="fa fa-times fa-fw" />
  98. </a>
  99. </div>
  100. </div>
  101. <div className="explore-toolbar-item">
  102. <div className="explore-toolbar-content">
  103. {!datasourceMissing ? (
  104. <div className="explore-toolbar-content-item">
  105. <div className="datasource-picker">
  106. <DataSourcePicker
  107. onChange={this.onChangeDatasource}
  108. datasources={exploreDatasources}
  109. current={selectedDatasource}
  110. />
  111. </div>
  112. </div>
  113. ) : null}
  114. {exploreId === 'left' && !splitted ? (
  115. <div className="explore-toolbar-content-item">
  116. {createResponsiveButton({
  117. splitted,
  118. title: 'Split',
  119. onClick: this.props.split,
  120. iconClassName: 'fa fa-fw fa-columns icon-margin-right',
  121. iconSide: IconSide.left,
  122. })}
  123. </div>
  124. ) : null}
  125. <div className="explore-toolbar-content-item timepicker">
  126. <ClickOutsideWrapper onClick={this.onCloseTimePicker}>
  127. <TimePicker ref={timepickerRef} range={range} onChangeTime={this.props.onChangeTime} />
  128. </ClickOutsideWrapper>
  129. </div>
  130. <div className="explore-toolbar-content-item">
  131. <button className="btn navbar-button navbar-button--no-icon" onClick={this.onClearAll}>
  132. Clear All
  133. </button>
  134. </div>
  135. <div className="explore-toolbar-content-item">
  136. {createResponsiveButton({
  137. splitted,
  138. title: 'Run Query',
  139. onClick: this.onRunQuery,
  140. buttonClassName: 'navbar-button--secondary',
  141. iconClassName: loading ? 'fa fa-spinner fa-fw fa-spin run-icon' : 'fa fa-level-down fa-fw run-icon',
  142. iconSide: IconSide.right,
  143. })}
  144. </div>
  145. </div>
  146. </div>
  147. </div>
  148. );
  149. }
  150. }
  151. const mapStateToProps = (state: StoreState, { exploreId }: OwnProps): StateProps => {
  152. const splitted = state.explore.split;
  153. const exploreItem = state.explore[exploreId];
  154. const { datasourceInstance, datasourceMissing, exploreDatasources, queryTransactions, range } = exploreItem;
  155. const selectedDatasource = datasourceInstance
  156. ? exploreDatasources.find(datasource => datasource.name === datasourceInstance.name)
  157. : undefined;
  158. const loading = queryTransactions.some(qt => !qt.done);
  159. return {
  160. datasourceMissing,
  161. exploreDatasources,
  162. loading,
  163. range,
  164. selectedDatasource,
  165. splitted,
  166. };
  167. };
  168. const mapDispatchToProps: DispatchProps = {
  169. changeDatasource,
  170. clearAll: clearQueries,
  171. runQuery: runQueries,
  172. closeSplit: splitClose,
  173. split: splitOpen,
  174. };
  175. export const ExploreToolbar = hot(module)(
  176. connect(
  177. mapStateToProps,
  178. mapDispatchToProps
  179. )(UnConnectedExploreToolbar)
  180. );