import React, { PureComponent } from 'react'; import { connect } from 'react-redux'; import { hot } from 'react-hot-loader'; import classNames from 'classnames'; import { css } from 'emotion'; import { ExploreId, ExploreItemState } from 'app/types/explore'; import { Icon, IconButton, SetInterval, ToolbarButton, ToolbarButtonRow, Tooltip } from '@grafana/ui'; import { DataSourceInstanceSettings, RawTimeRange, TimeRange, TimeZone } from '@grafana/data'; import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker'; import { StoreState } from 'app/types/store'; import { createAndCopyShortLink } from 'app/core/utils/shortLinks'; import { changeDatasource } from './state/datasource'; import { splitClose, splitOpen } from './state/main'; import { syncTimes, changeRefreshInterval } from './state/time'; import { getTimeZone } from '../profile/state/selectors'; import { updateTimeZoneForSession } from '../profile/state/reducers'; import { ExploreTimeControls } from './ExploreTimeControls'; import { LiveTailButton } from './LiveTailButton'; import { RunButton } from './RunButton'; import { LiveTailControls } from './useLiveTailControls'; import { cancelQueries, clearQueries, runQueries } from './state/query'; import ReturnToDashboardButton from './ReturnToDashboardButton'; interface OwnProps { exploreId: ExploreId; onChangeTime: (range: RawTimeRange, changedByScanner?: boolean) => void; } interface StateProps { datasourceMissing: boolean; loading: boolean; range: TimeRange; timeZone: TimeZone; splitted: boolean; syncedTimes: boolean; refreshInterval?: string; hasLiveOption: boolean; isLive: boolean; isPaused: boolean; datasourceLoading?: boolean | null; containerWidth: number; datasourceName?: string; } interface DispatchProps { changeDatasource: typeof changeDatasource; clearAll: typeof clearQueries; cancelQueries: typeof cancelQueries; runQueries: typeof runQueries; closeSplit: typeof splitClose; split: typeof splitOpen; syncTimes: typeof syncTimes; changeRefreshInterval: typeof changeRefreshInterval; onChangeTimeZone: typeof updateTimeZoneForSession; } type Props = StateProps & DispatchProps & OwnProps; export class UnConnectedExploreToolbar extends PureComponent { onChangeDatasource = async (dsSettings: DataSourceInstanceSettings) => { this.props.changeDatasource(this.props.exploreId, dsSettings.name, { importQueries: true }); }; onClearAll = () => { this.props.clearAll(this.props.exploreId); }; onRunQuery = (loading = false) => { if (loading) { return this.props.cancelQueries(this.props.exploreId); } else { return this.props.runQueries(this.props.exploreId); } }; onChangeRefreshInterval = (item: string) => { const { changeRefreshInterval, exploreId } = this.props; changeRefreshInterval(exploreId, item); }; onChangeTimeSync = () => { const { syncTimes, exploreId } = this.props; syncTimes(exploreId); }; render() { const { datasourceMissing, closeSplit, exploreId, loading, range, timeZone, splitted, syncedTimes, refreshInterval, onChangeTime, split, hasLiveOption, isLive, isPaused, containerWidth, onChangeTimeZone, } = this.props; const showSmallDataSourcePicker = (splitted ? containerWidth < 700 : containerWidth < 800) || false; const showSmallTimePicker = splitted || containerWidth < 1210; return (
{exploreId === 'left' && ( Explore )}
{splitted && ( closeSplit(exploreId)} name="times" /> )}
{!datasourceMissing ? (
) : null} {exploreId === 'left' && !splitted ? ( split()} icon="columns" disabled={isLive} > Split ) : null} createAndCopyShortLink(window.location.href)} /> {!isLive && ( )} {!isLive && ( Clear all )} {refreshInterval && } {hasLiveOption && ( {(controls) => ( )} )}
); } } const mapStateToProps = (state: StoreState, { exploreId }: OwnProps): StateProps => { const splitted = state.explore.split; const syncedTimes = state.explore.syncedTimes; const exploreItem: ExploreItemState = state.explore[exploreId]; const { datasourceInstance, datasourceMissing, range, refreshInterval, loading, isLive, isPaused, containerWidth, } = exploreItem; const hasLiveOption = !!datasourceInstance?.meta?.streaming; return { datasourceMissing, datasourceName: datasourceInstance?.name, loading, range, timeZone: getTimeZone(state.user), splitted, refreshInterval, hasLiveOption, isLive, isPaused, syncedTimes, containerWidth, }; }; const mapDispatchToProps: DispatchProps = { changeDatasource, changeRefreshInterval, clearAll: clearQueries, cancelQueries, runQueries, closeSplit: splitClose, split: splitOpen, syncTimes, onChangeTimeZone: updateTimeZoneForSession, }; export const ExploreToolbar = hot(module)(connect(mapStateToProps, mapDispatchToProps)(UnConnectedExploreToolbar));