TimePickerSettings.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { isEmpty } from 'lodash';
  2. import React, { PureComponent } from 'react';
  3. import { rangeUtil, TimeZone } from '@grafana/data';
  4. import { selectors } from '@grafana/e2e-selectors';
  5. import { CollapsableSection, Field, Input, Switch, TimeZonePicker, WeekStartPicker } from '@grafana/ui';
  6. import { AutoRefreshIntervals } from './AutoRefreshIntervals';
  7. interface Props {
  8. onWeekStartChange: (weekStart: string) => void;
  9. onTimeZoneChange: (timeZone: TimeZone) => void;
  10. onRefreshIntervalChange: (interval: string[]) => void;
  11. onNowDelayChange: (nowDelay: string) => void;
  12. onHideTimePickerChange: (hide: boolean) => void;
  13. onLiveNowChange: (liveNow: boolean) => void;
  14. refreshIntervals: string[];
  15. timePickerHidden: boolean;
  16. nowDelay: string;
  17. timezone: TimeZone;
  18. weekStart: string;
  19. liveNow: boolean;
  20. }
  21. interface State {
  22. isNowDelayValid: boolean;
  23. }
  24. export class TimePickerSettings extends PureComponent<Props, State> {
  25. state: State = { isNowDelayValid: true };
  26. onNowDelayChange = (event: React.FormEvent<HTMLInputElement>) => {
  27. const value = event.currentTarget.value;
  28. if (isEmpty(value)) {
  29. this.setState({ isNowDelayValid: true });
  30. return this.props.onNowDelayChange(value);
  31. }
  32. if (rangeUtil.isValidTimeSpan(value)) {
  33. this.setState({ isNowDelayValid: true });
  34. return this.props.onNowDelayChange(value);
  35. }
  36. this.setState({ isNowDelayValid: false });
  37. };
  38. onHideTimePickerChange = () => {
  39. this.props.onHideTimePickerChange(!this.props.timePickerHidden);
  40. };
  41. onLiveNowChange = () => {
  42. this.props.onLiveNowChange(!this.props.liveNow);
  43. };
  44. onTimeZoneChange = (timeZone?: string) => {
  45. if (typeof timeZone !== 'string') {
  46. return;
  47. }
  48. this.props.onTimeZoneChange(timeZone);
  49. };
  50. onWeekStartChange = (weekStart: string) => {
  51. this.props.onWeekStartChange(weekStart);
  52. };
  53. render() {
  54. return (
  55. <CollapsableSection label="Time options" isOpen={true}>
  56. <Field label="Timezone" data-testid={selectors.components.TimeZonePicker.containerV2}>
  57. <TimeZonePicker
  58. inputId="time-options-input"
  59. includeInternal={true}
  60. value={this.props.timezone}
  61. onChange={this.onTimeZoneChange}
  62. width={40}
  63. />
  64. </Field>
  65. <Field label="Week start" data-testid={selectors.components.WeekStartPicker.containerV2}>
  66. <WeekStartPicker
  67. inputId="week-start-input"
  68. width={40}
  69. value={this.props.weekStart}
  70. onChange={this.onWeekStartChange}
  71. />
  72. </Field>
  73. <AutoRefreshIntervals
  74. refreshIntervals={this.props.refreshIntervals}
  75. onRefreshIntervalChange={this.props.onRefreshIntervalChange}
  76. />
  77. <Field label="Now delay" description="Exclude recent data that may be incomplete.">
  78. <Input
  79. id="now-delay-input"
  80. invalid={!this.state.isNowDelayValid}
  81. placeholder="0m"
  82. onChange={this.onNowDelayChange}
  83. defaultValue={this.props.nowDelay}
  84. />
  85. </Field>
  86. <Field label="Hide time picker">
  87. <Switch
  88. id="hide-time-picker-toggle"
  89. value={!!this.props.timePickerHidden}
  90. onChange={this.onHideTimePickerChange}
  91. />
  92. </Field>
  93. <Field
  94. label="Refresh live dashboards"
  95. description="Continuously re-draw panels where the time range references 'now'"
  96. >
  97. <Switch id="refresh-live-dashboards-toggle" value={!!this.props.liveNow} onChange={this.onLiveNowChange} />
  98. </Field>
  99. </CollapsableSection>
  100. );
  101. }
  102. }