import React, { PureComponent } from 'react'; import { Select, Input, Tooltip, LegacyForms } from '@grafana/ui'; import { DashboardModel } from '../../state/DashboardModel'; import { getTimeZoneGroups, TimeZone, rangeUtil, SelectableValue } from '@grafana/data'; import { config } from '@grafana/runtime'; import kbn from 'app/core/utils/kbn'; import isEmpty from 'lodash/isEmpty'; const grafanaTimeZones = [ { value: '', label: 'Default' }, { value: 'browser', label: 'Local browser time' }, { value: 'utc', label: 'UTC' }, ]; const timeZones = getTimeZoneGroups().reduce((tzs, group) => { const options = group.options.map(tz => ({ value: tz, label: tz })); tzs.push.apply(tzs, options); return tzs; }, grafanaTimeZones); interface Props { getDashboard: () => DashboardModel; onTimeZoneChange: (timeZone: TimeZone) => void; onRefreshIntervalChange: (interval: string[]) => void; onNowDelayChange: (nowDelay: string) => void; onHideTimePickerChange: (hide: boolean) => void; } interface State { isNowDelayValid: boolean; } export class TimePickerSettings extends PureComponent { state: State = { isNowDelayValid: true }; componentDidMount() { const { timepicker } = this.props.getDashboard(); let intervals: string[] = timepicker.refresh_intervals ?? [ '5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d', ]; if (config.minRefreshInterval) { intervals = intervals.filter(rate => { return kbn.interval_to_ms(rate) > kbn.interval_to_ms(config.minRefreshInterval); }); } this.props.onRefreshIntervalChange(intervals); } getRefreshIntervals = () => { const dashboard = this.props.getDashboard(); if (!Array.isArray(dashboard.timepicker.refresh_intervals)) { return ''; } return dashboard.timepicker.refresh_intervals.join(','); }; onRefreshIntervalChange = (event: React.FormEvent) => { if (!event.currentTarget.value) { return; } const intervals = event.currentTarget.value.split(','); this.props.onRefreshIntervalChange(intervals); this.forceUpdate(); }; onNowDelayChange = (event: React.FormEvent) => { const value = event.currentTarget.value; if (isEmpty(value)) { this.setState({ isNowDelayValid: true }); return this.props.onNowDelayChange(value); } if (rangeUtil.isValidTimeSpan(value)) { this.setState({ isNowDelayValid: true }); return this.props.onNowDelayChange(value); } this.setState({ isNowDelayValid: false }); }; onHideTimePickerChange = () => { const dashboard = this.props.getDashboard(); this.props.onHideTimePickerChange(!dashboard.timepicker.hidden); this.forceUpdate(); }; onTimeZoneChange = (timeZone: SelectableValue) => { if (!timeZone || typeof timeZone.value !== 'string') { return; } this.props.onTimeZoneChange(timeZone.value); this.forceUpdate(); }; render() { const dashboard = this.props.getDashboard(); const value = timeZones.find(item => item.value === dashboard.timezone); return (
Time Options
Now delay now-
); } }