import React, { PureComponent } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import classNames from 'classnames'; import { css } from '@emotion/css'; import { ExploreId, ExploreItemState } from 'app/types/explore'; import { Icon, IconButton, SetInterval, ToolbarButton, ToolbarButtonRow, Tooltip } from '@grafana/ui'; import { DataSourceInstanceSettings, RawTimeRange } from '@grafana/data'; import { DataSourcePicker } from '@grafana/runtime'; 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 { getFiscalYearStartMonth, getTimeZone } from '../profile/state/selectors'; import { updateFiscalYearStartMonthForSession, 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'; import { isSplit } from './state/selectors'; interface OwnProps { exploreId: ExploreId; onChangeTime: (range: RawTimeRange, changedByScanner?: boolean) => void; } type Props = OwnProps & ConnectedProps; class UnConnectedExploreToolbar extends PureComponent { onChangeDatasource = async (dsSettings: DataSourceInstanceSettings) => { this.props.changeDatasource(this.props.exploreId, dsSettings.uid, { importQueries: true }); }; onClearAll = () => { this.props.clearAll(this.props.exploreId); }; onRunQuery = (loading = false) => { const { runQueries, cancelQueries, exploreId } = this.props; if (loading) { return cancelQueries(exploreId); } else { return runQueries(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, fiscalYearStartMonth, splitted, syncedTimes, refreshInterval, onChangeTime, split, hasLiveOption, isLive, isPaused, containerWidth, onChangeTimeZone, onChangeFiscalYearStartMonth, } = 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)} aria-label="Copy shortened link to the executed query" /> {!isLive && ( )} {!isLive && ( Clear all )} {refreshInterval && } {hasLiveOption && ( {(controls) => ( )} )}
); } } const mapStateToProps = (state: StoreState, { exploreId }: OwnProps) => { const { syncedTimes } = state.explore; 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), fiscalYearStartMonth: getFiscalYearStartMonth(state.user), splitted: isSplit(state), refreshInterval, hasLiveOption, isLive, isPaused, syncedTimes, containerWidth, }; }; const mapDispatchToProps = { changeDatasource, changeRefreshInterval, clearAll: clearQueries, cancelQueries, runQueries, closeSplit: splitClose, split: splitOpen, syncTimes, onChangeTimeZone: updateTimeZoneForSession, onChangeFiscalYearStartMonth: updateFiscalYearStartMonthForSession, }; const connector = connect(mapStateToProps, mapDispatchToProps); export const ExploreToolbar = connector(UnConnectedExploreToolbar);