grafana/public/app/features/dashboard/components/DashboardSettings/TimePickerSettings.tsx
Guilherme Caulada a9faab6b09
Dashboard: Add week start option to global and dashboard preferences (#40010)
* Add global week start option to shared preferences

* Add default_week_start to configuration docs

* Add week start option to dashboards

* Add week start argument to tsdb time range parser

* Fix strict check issues

* Add tests for week start

* Change wording on default_week_start documentation

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>

* Update week_start column to be a nullable field

Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>

* Update configuration to include browser option

* Update WeekStartPicker container selector

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>

* Add menuShouldPortal to WeekStartPicker to remove deprecation warning

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>

* Add inputId to WeekStartPicker

* Use e2e selector on WeekStartPicker aria-label

* Simplify WeekStartPicker onChange condition

* Specify value type on WeekStartPicker weekStarts

* Remove setWeekStart side effect from reducer

* Fix updateLocale failing to reset week start

* Store week start as string to handle empty values

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
2021-10-18 10:27:14 -03:00

111 lines
3.5 KiB
TypeScript

import React, { PureComponent } from 'react';
import { Input, TimeZonePicker, Field, Switch, CollapsableSection, WeekStartPicker } from '@grafana/ui';
import { rangeUtil, TimeZone } from '@grafana/data';
import { isEmpty } from 'lodash';
import { selectors } from '@grafana/e2e-selectors';
import { AutoRefreshIntervals } from './AutoRefreshIntervals';
interface Props {
onWeekStartChange: (weekStart: string) => void;
onTimeZoneChange: (timeZone: TimeZone) => void;
onRefreshIntervalChange: (interval: string[]) => void;
onNowDelayChange: (nowDelay: string) => void;
onHideTimePickerChange: (hide: boolean) => void;
onLiveNowChange: (liveNow: boolean) => void;
refreshIntervals: string[];
timePickerHidden: boolean;
nowDelay: string;
timezone: TimeZone;
weekStart: string;
liveNow: boolean;
}
interface State {
isNowDelayValid: boolean;
}
export class TimePickerSettings extends PureComponent<Props, State> {
state: State = { isNowDelayValid: true };
onNowDelayChange = (event: React.FormEvent<HTMLInputElement>) => {
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 = () => {
this.props.onHideTimePickerChange(!this.props.timePickerHidden);
};
onLiveNowChange = () => {
this.props.onLiveNowChange(!this.props.liveNow);
};
onTimeZoneChange = (timeZone?: string) => {
if (typeof timeZone !== 'string') {
return;
}
this.props.onTimeZoneChange(timeZone);
};
onWeekStartChange = (weekStart: string) => {
this.props.onWeekStartChange(weekStart);
};
render() {
return (
<CollapsableSection label="Time options" isOpen={true}>
<Field label="Timezone" aria-label={selectors.components.TimeZonePicker.container}>
<TimeZonePicker
includeInternal={true}
value={this.props.timezone}
onChange={this.onTimeZoneChange}
width={40}
/>
</Field>
<Field label="Week start" aria-label={selectors.components.WeekStartPicker.container}>
<WeekStartPicker width={40} value={this.props.weekStart} onChange={this.onWeekStartChange} />
</Field>
<AutoRefreshIntervals
refreshIntervals={this.props.refreshIntervals}
onRefreshIntervalChange={this.props.onRefreshIntervalChange}
/>
<Field
label="Now delay now"
description="Enter 1m to ignore the last minute. It might contain incomplete metrics."
>
<Input
invalid={!this.state.isNowDelayValid}
placeholder="0m"
onChange={this.onNowDelayChange}
defaultValue={this.props.nowDelay}
/>
</Field>
<Field label="Hide time picker">
<Switch
id="hide-time-picker-toggle"
value={!!this.props.timePickerHidden}
onChange={this.onHideTimePickerChange}
/>
</Field>
<Field
label="Refresh live dashboards"
description="Continuously re-draw panels where the time range references 'now'"
>
<Switch id="refresh-live-dashboards-toggle" value={!!this.props.liveNow} onChange={this.onLiveNowChange} />
</Field>
</CollapsableSection>
);
}
}