Skip to content

Commit ceb9f08

Browse files
authored
Chore: Replaces moment with Grafanas DateTime (grafana#16919)
* Wip: Initial commit * Refactor: Replaces moment.utc( * Refactor: replaces the last isMoment statements * Refactor: Removes almost all moment imports * Refactor: Moves moment_wrapper to grafana/ui * Refactor: Renames momentWrapper * Refactor: Removes one more moment import * Refactor: Removes unitOfTime import * Fix: Fixes Prettier error * Refactor: Renames DateTimeType to DateTime * Refactor: Renames isDateTimeType to isDateTime * Refactor: Renames dateTime to dateTime * Feature: Bans moment imports and types
1 parent e7a9afe commit ceb9f08

File tree

86 files changed

+582
-483
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+582
-483
lines changed

packages/grafana-ui/src/components/Graph/mockGraphWithLegendData.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GraphWithLegendProps } from './GraphWithLegend';
2-
import moment from 'moment';
32
import { LegendDisplayMode } from '../Legend/Legend';
3+
import { dateTime } from '../../utils/moment_wrapper';
44
// import { LegendList } from '../Legend/LegendList';
55

66
export const mockGraphWithLegendData = ({
@@ -3287,8 +3287,8 @@ export const mockGraphWithLegendData = ({
32873287
},
32883288
],
32893289
timeRange: {
3290-
from: moment('2019-04-09T07:01:14.247Z'),
3291-
to: moment('2019-04-09T13:01:14.247Z'),
3290+
from: dateTime('2019-04-09T07:01:14.247Z'),
3291+
to: dateTime('2019-04-09T13:01:14.247Z'),
32923292
raw: {
32933293
from: 'now-6h',
32943294
to: 'now',

packages/grafana-ui/src/components/Table/TableCellBuilder.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import _ from 'lodash';
33
import React, { ReactElement } from 'react';
44
import { GridCellProps } from 'react-virtualized';
55
import { Table, Props } from './Table';
6-
import moment from 'moment';
76
import { ValueFormatter, getValueFormat, getColorFromHexRgbOrName } from '../../utils/index';
87
import { GrafanaTheme } from '../../types/theme';
98
import { InterpolateFunction } from '../../types/panel';
109
import { Field } from '../../types/data';
10+
import { dateTime } from '../../utils/moment_wrapper';
1111

1212
export interface TableCellBuilderOptions {
1313
value: any;
@@ -96,7 +96,7 @@ export function getCellBuilder(schema: Field, style: ColumnStyle | null, props:
9696
if (_.isArray(v)) {
9797
v = v[0];
9898
}
99-
let date = moment(v);
99+
let date = dateTime(v);
100100
if (false) {
101101
// TODO?????? this.props.isUTC) {
102102
date = date.utc();

packages/grafana-ui/src/components/TimePicker/TimePicker.story.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
2-
import moment, { Moment } from 'moment';
32
import { storiesOf } from '@storybook/react';
43
import { action } from '@storybook/addon-actions';
54

65
import { TimePicker } from './TimePicker';
76
import { UseState } from '../../utils/storybook/UseState';
87
import { withRighAlignedStory } from '../../utils/storybook/withRightAlignedStory';
8+
import { TimeFragment } from '../../types/time';
9+
import { dateTime } from '../../utils/moment_wrapper';
910

1011
const TimePickerStories = storiesOf('UI/TimePicker', module);
1112
export const popoverOptions = {
@@ -177,9 +178,9 @@ TimePickerStories.add('default', () => {
177178
return (
178179
<UseState
179180
initialState={{
180-
from: moment(),
181-
to: moment(),
182-
raw: { from: 'now-6h' as string | Moment, to: 'now' as string | Moment },
181+
from: dateTime(),
182+
to: dateTime(),
183+
raw: { from: 'now-6h' as TimeFragment, to: 'now' as TimeFragment },
183184
}}
184185
>
185186
{(value, updateValue) => {

packages/grafana-ui/src/components/TimePicker/TimePicker.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { PureComponent } from 'react';
2-
import moment from 'moment';
32
import { ButtonSelect } from '../Select/ButtonSelect';
43
import { mapTimeOptionToTimeRange, mapTimeRangeToRangeString } from './time';
54
import { Props as TimePickerPopoverProps } from './TimePickerPopover';
@@ -8,6 +7,7 @@ import { PopperContent } from '../Tooltip/PopperController';
87
import { Timezone } from '../../utils/datemath';
98
import { TimeRange, TimeOption, TimeOptions } from '../../types/time';
109
import { SelectOptionItem } from '../Select/Select';
10+
import { isDateTime } from '../../utils/moment_wrapper';
1111

1212
export interface Props {
1313
value: TimeRange;
@@ -266,7 +266,7 @@ export class TimePicker extends PureComponent<Props, State> {
266266
} = this.props;
267267
const options = this.mapTimeOptionsToSelectOptionItems(selectTimeOptions);
268268
const rangeString = mapTimeRangeToRangeString(value);
269-
const isAbsolute = moment.isMoment(value.raw.to);
269+
const isAbsolute = isDateTime(value.raw.to);
270270

271271
return (
272272
<div className="time-picker">

packages/grafana-ui/src/components/TimePicker/TimePickerCalendar.story.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import React from 'react';
22
import { storiesOf } from '@storybook/react';
33
import { action } from '@storybook/addon-actions';
4-
import { Moment } from 'moment';
54

65
import { withCenteredStory } from '../../utils/storybook/withCenteredStory';
76
import { TimePickerCalendar } from './TimePickerCalendar';
87
import { UseState } from '../../utils/storybook/UseState';
8+
import { TimeFragment } from '../../types/time';
99

1010
const TimePickerCalendarStories = storiesOf('UI/TimePicker/TimePickerCalendar', module);
1111

1212
TimePickerCalendarStories.addDecorator(withCenteredStory);
1313

1414
TimePickerCalendarStories.add('default', () => (
15-
<UseState initialState={'now-6h' as string | Moment}>
15+
<UseState initialState={'now-6h' as TimeFragment}>
1616
{(value, updateValue) => {
1717
return (
1818
<TimePickerCalendar

packages/grafana-ui/src/components/TimePicker/TimePickerCalendar.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React, { PureComponent } from 'react';
22
import Calendar from 'react-calendar/dist/entry.nostyle';
3-
import moment, { Moment } from 'moment';
43
import { TimeFragment } from '../../types/time';
54
import { Timezone } from '../../utils/datemath';
5+
import { DateTime, dateTime, isDateTime } from '../../utils/moment_wrapper';
66

7-
import { stringToMoment } from './time';
7+
import { stringToDateTimeType } from './time';
88

99
export interface Props {
1010
value: TimeFragment;
1111
isTimezoneUtc: boolean;
1212
roundup?: boolean;
1313
timezone?: Timezone;
14-
onChange: (value: Moment) => void;
14+
onChange: (value: DateTime) => void;
1515
}
1616

1717
export class TimePickerCalendar extends PureComponent<Props> {
@@ -22,15 +22,15 @@ export class TimePickerCalendar extends PureComponent<Props> {
2222
return;
2323
}
2424

25-
onChange(moment(date));
25+
onChange(dateTime(date));
2626
};
2727

2828
render() {
2929
const { value, isTimezoneUtc, roundup, timezone } = this.props;
30-
const dateValue = moment.isMoment(value)
30+
const dateValue = isDateTime(value)
3131
? value.toDate()
32-
: stringToMoment(value, isTimezoneUtc, roundup, timezone).toDate();
33-
const calendarValue = dateValue instanceof Date && !isNaN(dateValue.getTime()) ? dateValue : moment().toDate();
32+
: stringToDateTimeType(value, isTimezoneUtc, roundup, timezone).toDate();
33+
const calendarValue = dateValue instanceof Date && !isNaN(dateValue.getTime()) ? dateValue : dateTime().toDate();
3434

3535
return (
3636
<Calendar

packages/grafana-ui/src/components/TimePicker/TimePickerInput.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, { PureComponent, ChangeEvent } from 'react';
2-
import moment from 'moment';
32
import { TimeFragment, TIME_FORMAT } from '../../types/time';
4-
5-
import { stringToMoment, isValidTimeString } from './time';
63
import { Input } from '../Input/Input';
4+
import { stringToDateTimeType, isValidTimeString } from './time';
5+
import { isDateTime } from '../../utils/moment_wrapper';
76

87
export interface Props {
98
value: TimeFragment;
@@ -22,7 +21,7 @@ export class TimePickerInput extends PureComponent<Props> {
2221
return isValid;
2322
}
2423

25-
const parsed = stringToMoment(value, isTimezoneUtc);
24+
const parsed = stringToDateTimeType(value, isTimezoneUtc);
2625
const isValid = parsed.isValid();
2726
return isValid;
2827
};
@@ -35,7 +34,7 @@ export class TimePickerInput extends PureComponent<Props> {
3534
};
3635

3736
valueToString = (value: TimeFragment) => {
38-
if (moment.isMoment(value)) {
37+
if (isDateTime(value)) {
3938
return value.format(TIME_FORMAT);
4039
} else {
4140
return value;

packages/grafana-ui/src/components/TimePicker/TimePickerOptionGroup.story.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { ComponentType } from 'react';
22
import { storiesOf } from '@storybook/react';
3-
import moment from 'moment';
43
import { action } from '@storybook/addon-actions';
54

65
import { TimePickerOptionGroup } from './TimePickerOptionGroup';
76
import { TimeRange } from '../../types/time';
87
import { withRighAlignedStory } from '../../utils/storybook/withRightAlignedStory';
98
import { popoverOptions } from './TimePicker.story';
9+
import { dateTime } from '../../utils/moment_wrapper';
1010

1111
const TimePickerOptionGroupStories = storiesOf('UI/TimePicker/TimePickerOptionGroup', module);
1212

@@ -21,7 +21,7 @@ const data = {
2121
action('onPopoverClose fired')(timeRange);
2222
},
2323
popoverProps: {
24-
value: { from: moment(), to: moment(), raw: { from: 'now/d', to: 'now/d' } },
24+
value: { from: dateTime(), to: dateTime(), raw: { from: 'now/d', to: 'now/d' } },
2525
options: popoverOptions,
2626
isTimezoneUtc: false,
2727
onChange: (timeRange: TimeRange) => {

packages/grafana-ui/src/components/TimePicker/TimePickerPopover.story.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22
import { action } from '@storybook/addon-actions';
3-
import moment, { Moment } from 'moment';
43

54
import { storiesOf } from '@storybook/react';
65
import { withCenteredStory } from '../../utils/storybook/withCenteredStory';
76
import { TimePickerPopover } from './TimePickerPopover';
87
import { UseState } from '../../utils/storybook/UseState';
98
import { popoverOptions } from './TimePicker.story';
9+
import { dateTime, DateTime } from '../../utils/moment_wrapper';
1010

1111
const TimePickerPopoverStories = storiesOf('UI/TimePicker/TimePickerPopover', module);
1212

@@ -15,9 +15,9 @@ TimePickerPopoverStories.addDecorator(withCenteredStory);
1515
TimePickerPopoverStories.add('default', () => (
1616
<UseState
1717
initialState={{
18-
from: moment(),
19-
to: moment(),
20-
raw: { from: 'now-6h' as string | Moment, to: 'now' as string | Moment },
18+
from: dateTime(),
19+
to: dateTime(),
20+
raw: { from: 'now-6h' as string | DateTime, to: 'now' as string | DateTime },
2121
}}
2222
>
2323
{(value, updateValue) => {

packages/grafana-ui/src/components/TimePicker/TimePickerPopover.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { Component, SyntheticEvent } from 'react';
22
import { TimeRange, TimeOptions, TimeOption } from '../../types/time';
3-
import { Moment } from 'moment';
43

54
import { TimePickerCalendar } from './TimePickerCalendar';
65
import { TimePickerInput } from './TimePickerInput';
76
import { mapTimeOptionToTimeRange } from './time';
87
import { Timezone } from '../../utils/datemath';
8+
import { DateTime } from '../../utils/moment_wrapper';
99

1010
export interface Props {
1111
value: TimeRange;
@@ -42,13 +42,13 @@ export class TimePickerPopover extends Component<Props, State> {
4242
});
4343
};
4444

45-
onFromCalendarChanged = (value: Moment) => {
45+
onFromCalendarChanged = (value: DateTime) => {
4646
this.setState({
4747
value: { ...this.state.value, raw: { ...this.state.value.raw, from: value } },
4848
});
4949
};
5050

51-
onToCalendarChanged = (value: Moment) => {
51+
onToCalendarChanged = (value: DateTime) => {
5252
this.setState({
5353
value: { ...this.state.value, raw: { ...this.state.value.raw, to: value } },
5454
});

packages/grafana-ui/src/components/TimePicker/time.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
import moment, { Moment } from 'moment';
21
import { TimeOption, TimeRange, TIME_FORMAT } from '../../types/time';
32
import { describeTimeRange } from '../../utils/rangeutil';
43
import * as dateMath from '../../utils/datemath';
4+
import { dateTime, DateTime, toUtc } from '../../utils/moment_wrapper';
55

66
export const mapTimeOptionToTimeRange = (
77
timeOption: TimeOption,
88
isTimezoneUtc: boolean,
99
timezone?: dateMath.Timezone
1010
): TimeRange => {
11-
const fromMoment = stringToMoment(timeOption.from, isTimezoneUtc, false, timezone);
12-
const toMoment = stringToMoment(timeOption.to, isTimezoneUtc, true, timezone);
11+
const fromMoment = stringToDateTimeType(timeOption.from, isTimezoneUtc, false, timezone);
12+
const toMoment = stringToDateTimeType(timeOption.to, isTimezoneUtc, true, timezone);
1313

1414
return { from: fromMoment, to: toMoment, raw: { from: timeOption.from, to: timeOption.to } };
1515
};
1616

17-
export const stringToMoment = (
17+
export const stringToDateTimeType = (
1818
value: string,
1919
isTimezoneUtc: boolean,
2020
roundUp?: boolean,
2121
timezone?: dateMath.Timezone
22-
): Moment => {
22+
): DateTime => {
2323
if (value.indexOf('now') !== -1) {
2424
if (!dateMath.isValid(value)) {
25-
return moment();
25+
return dateTime();
2626
}
2727

2828
const parsed = dateMath.parse(value, roundUp, timezone);
29-
return parsed || moment();
29+
return parsed || dateTime();
3030
}
3131

3232
if (isTimezoneUtc) {
33-
return moment.utc(value, TIME_FORMAT);
33+
return toUtc(value, TIME_FORMAT);
3434
}
3535

36-
return moment(value, TIME_FORMAT);
36+
return dateTime(value, TIME_FORMAT);
3737
};
3838

3939
export const mapTimeRangeToRangeString = (timeRange: TimeRange): string => {

packages/grafana-ui/src/types/time.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Moment } from 'moment';
1+
import { DateTime } from '../utils/moment_wrapper';
22

33
export interface RawTimeRange {
4-
from: Moment | string;
5-
to: Moment | string;
4+
from: DateTime | string;
5+
to: DateTime | string;
66
}
77

88
export interface TimeRange {
9-
from: Moment;
10-
to: Moment;
9+
from: DateTime;
10+
to: DateTime;
1111
raw: RawTimeRange;
1212
}
1313

@@ -47,6 +47,6 @@ export interface TimeOptions {
4747
[key: string]: TimeOption[];
4848
}
4949

50-
export type TimeFragment = string | Moment;
50+
export type TimeFragment = string | DateTime;
5151

5252
export const TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';

0 commit comments

Comments
 (0)