import React, { PureComponent } from 'react'; import { connect } from 'react-redux'; import { hot } from 'react-hot-loader'; import { ExploreId } from 'app/types/explore'; import { DataSourceSelectItem, RawTimeRange, TimeRange } from '@grafana/ui'; import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker'; import { StoreState } from 'app/types/store'; import { changeDatasource, clearQueries, splitClose, runQueries, splitOpen } from './state/actions'; import TimePicker from './TimePicker'; import { ClickOutsideWrapper } from 'app/core/components/ClickOutsideWrapper/ClickOutsideWrapper'; enum IconSide { left = 'left', right = 'right', } const createResponsiveButton = (options: { splitted: boolean; title: string; onClick: () => void; buttonClassName?: string; iconClassName?: string; iconSide?: IconSide; }) => { const defaultOptions = { iconSide: IconSide.left, }; const props = { ...options, defaultOptions }; const { title, onClick, buttonClassName, iconClassName, splitted, iconSide } = props; return ( ); }; interface OwnProps { exploreId: ExploreId; timepickerRef: React.RefObject; onChangeTime: (range: TimeRange, changedByScanner?: boolean) => void; } interface StateProps { datasourceMissing: boolean; exploreDatasources: DataSourceSelectItem[]; loading: boolean; range: RawTimeRange; selectedDatasource: DataSourceSelectItem; splitted: boolean; } interface DispatchProps { changeDatasource: typeof changeDatasource; clearAll: typeof clearQueries; runQuery: typeof runQueries; closeSplit: typeof splitClose; split: typeof splitOpen; } type Props = StateProps & DispatchProps & OwnProps; export class UnConnectedExploreToolbar extends PureComponent { constructor(props) { super(props); } onChangeDatasource = async option => { this.props.changeDatasource(this.props.exploreId, option.value); }; onClearAll = () => { this.props.clearAll(this.props.exploreId); }; onRunQuery = () => { this.props.runQuery(this.props.exploreId); }; onCloseTimePicker = () => { this.props.timepickerRef.current.setState({ isOpen: false }); }; render() { const { datasourceMissing, exploreDatasources, exploreId, loading, range, selectedDatasource, splitted, timepickerRef, } = this.props; return (
{exploreId === 'left' && ( Explore )}
{exploreId === 'right' && ( )}
{!datasourceMissing ? (
) : null} {exploreId === 'left' && !splitted ? (
{createResponsiveButton({ splitted, title: 'Split', onClick: this.props.split, iconClassName: 'fa fa-fw fa-columns icon-margin-right', iconSide: IconSide.left, })}
) : null}
{createResponsiveButton({ splitted, title: 'Run Query', onClick: this.onRunQuery, buttonClassName: 'navbar-button--secondary', iconClassName: loading ? 'fa fa-spinner fa-fw fa-spin run-icon' : 'fa fa-level-down fa-fw run-icon', iconSide: IconSide.right, })}
); } } const mapStateToProps = (state: StoreState, { exploreId }: OwnProps): StateProps => { const splitted = state.explore.split; const exploreItem = state.explore[exploreId]; const { datasourceInstance, datasourceMissing, exploreDatasources, queryTransactions, range } = exploreItem; const selectedDatasource = datasourceInstance ? exploreDatasources.find(datasource => datasource.name === datasourceInstance.name) : undefined; const loading = queryTransactions.some(qt => !qt.done); return { datasourceMissing, exploreDatasources, loading, range, selectedDatasource, splitted, }; }; const mapDispatchToProps: DispatchProps = { changeDatasource, clearAll: clearQueries, runQuery: runQueries, closeSplit: splitClose, split: splitOpen, }; export const ExploreToolbar = hot(module)( connect( mapStateToProps, mapDispatchToProps )(UnConnectedExploreToolbar) );