Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(moment): Replace Moment.js with DayJs #31310

Merged
merged 22 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ba7a325
refactor(moment): Replace Moment.js with DayJs
msyavuz Dec 5, 2024
ded1285
fix(package): sync package.json and tests
msyavuz Dec 6, 2024
63d7c02
refactor(moment): refactor moment out of components
msyavuz Dec 16, 2024
17d0041
refactor(dayjs): re-add moment and migrate components
msyavuz Dec 17, 2024
8eb99d3
refactor(dayjs): migrate some components
msyavuz Dec 17, 2024
f556d9c
chore(moment): add comment and change moment to dayjs
msyavuz Dec 17, 2024
8572277
refactor(dayjs): centralize dayjs extensions
msyavuz Dec 17, 2024
fd2f451
chore(dayjs): add test for extensions and change package.jsons
msyavuz Dec 17, 2024
4a55d06
fix(type): type from dayjs instead
msyavuz Dec 17, 2024
9c8eca1
Merge branch 'master' into msyavuz/momentjs-to-dayjs
msyavuz Dec 17, 2024
6cbac28
fix(test): only wait for load if its loading
msyavuz Dec 18, 2024
e9fcacd
test(AlertReportModal): use events directly instead?
msyavuz Dec 18, 2024
a035c87
test(AlertReportModal): fix the test
msyavuz Dec 18, 2024
af97ee0
refactor(pluigin-echarts): move to dayjs
msyavuz Dec 18, 2024
5a0862a
chore: remove unused waitfor
msyavuz Dec 18, 2024
a3e10bf
feat(moment): remove timezone-support, migrate handlebars to dayjs
msyavuz Dec 18, 2024
638599f
chore(timezone-support): remove usages
msyavuz Dec 18, 2024
854a66a
refactor(legacy-plugins): migrate to dayjs
msyavuz Dec 18, 2024
972bf08
fix(timezoneselector): use map instead of array and remove unneccessa…
msyavuz Dec 19, 2024
58a7355
refactor(alert report modal): remove loading check
msyavuz Dec 19, 2024
f60a221
chore(alert report modal): remove unused import
msyavuz Dec 19, 2024
03ab506
chore(types): add some types instead of any
msyavuz Dec 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions superset-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"d3-color": "^1.4.1",
"d3-scale": "^3.0.0",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"mousetrap": "^1.6.5",
"prop-types": "^15.8.1",
"react-bootstrap-slider": "3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dompurify": "^3.1.3",
"fast-safe-stringify": "^2.1.1",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"dayjs": "^1.11.13",
"nvd3-fork": "^2.0.5",
"prop-types": "^15.8.1",
"urijs": "^1.19.11"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
import { kebabCase, throttle } from 'lodash';
import d3 from 'd3';
import moment from 'moment';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import nv from 'nvd3-fork';
import PropTypes from 'prop-types';
import {
Expand Down Expand Up @@ -83,6 +84,8 @@ const NO_DATA_RENDER_DATA = [
},
];

dayjs.extend(utc);

const smartDateVerboseFormatter = getTimeFormatter(SMART_DATE_VERBOSE_ID);

// Override the noData render function to make a prettier UX
Expand Down Expand Up @@ -1055,7 +1058,7 @@ function nvd3Vis(element, props) {
});
const records = (annotationData[e.name].records || [])
.map(r => {
const timeValue = new Date(moment.utc(r[e.timeColumn]));
const timeValue = new Date(dayjs.utc(r[e.timeColumn]));

return {
...r,
Expand Down Expand Up @@ -1131,9 +1134,9 @@ function nvd3Vis(element, props) {

const records = (annotationData[e.name].records || [])
.map(r => {
const timeValue = new Date(moment.utc(r[e.timeColumn]));
const timeValue = new Date(dayjs.utc(r[e.timeColumn]));
const intervalEndValue = new Date(
moment.utc(r[e.intervalEndColumn]),
dayjs.utc(r[e.intervalEndColumn]),
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"d3-array": "^1.2.0",
"echarts": "^5.4.1",
"lodash": "^4.17.21",
"moment": "^2.30.1"
"dayjs": "^1.11.13"
},
"peerDependencies": {
"@superset-ui/chart-controls": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import moment from 'moment';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import {
ChartProps,
getMetricLabel,
Expand All @@ -27,9 +28,11 @@ import {
} from '@superset-ui/core';
import { getComparisonFontSize, getHeaderFontSize } from './utils';

dayjs.extend(utc);

export const parseMetricValue = (metricValue: number | string | null) => {
if (typeof metricValue === 'string') {
const dateObject = moment.utc(metricValue, moment.ISO_8601, true);
const dateObject = dayjs.utc(metricValue, undefined, true);
msyavuz marked this conversation as resolved.
Show resolved Hide resolved
if (dateObject.isValid()) {
return dateObject.valueOf();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@
* under the License.
*/

import moment from 'moment';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import {
getTimeFormatter,
getTimeFormatterForGranularity,
SMART_DATE_ID,
TimeGranularity,
} from '@superset-ui/core';

dayjs.extend(utc);

export const parseMetricValue = (metricValue: number | string | null) => {
if (typeof metricValue === 'string') {
const dateObject = moment.utc(metricValue, moment.ISO_8601, true);
const dateObject = dayjs.utc(metricValue, undefined, true);
if (dateObject.isValid()) {
return dateObject.valueOf();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@superset-ui/core": "*",
"ace-builds": "^1.4.14",
"lodash": "^4.17.11",
"moment": "^2.26.0",
"dayjs": "^1.11.13",
"react": "^16.13.1",
"react-ace": "^10.1.0",
"react-dom": "^16.13.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import { SafeMarkdown, styled, t } from '@superset-ui/core';
import Handlebars from 'handlebars';
import moment from 'moment';
import dayjs from 'dayjs';
import { useMemo, useState } from 'react';
import { isPlainObject } from 'lodash';
import Helpers from 'just-handlebars-helpers';
Expand Down Expand Up @@ -78,7 +78,7 @@ export const HandlebarsViewer = ({
// usage: {{dateFormat my_date format="MMMM YYYY"}}
Handlebars.registerHelper('dateFormat', function (context, block) {
const f = block.hash.format || 'YYYY-MM-DD';
return moment(context).format(f);
return dayjs(context).format(f);
});

// usage: {{ }}
Expand Down
7 changes: 4 additions & 3 deletions superset-frontend/src/SqlLab/components/QueryTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/
import { useMemo, ReactNode } from 'react';
import moment from 'moment';
import Card from 'src/components/Card';
import ProgressBar from 'src/components/ProgressBar';
import { t, useTheme, QueryResponse } from '@superset-ui/core';
Expand All @@ -32,7 +31,7 @@ import {
} from 'src/SqlLab/actions/sqlLab';
import TableView from 'src/components/TableView';
import Button from 'src/components/Button';
import { fDuration } from 'src/utils/dates';
import { fDuration, extendedDayjs } from 'src/utils/dates';
import Icons from 'src/components/Icons';
import Label from 'src/components/Label';
import { Tooltip } from 'src/components/Tooltip';
Expand Down Expand Up @@ -255,7 +254,9 @@ const QueryTable = ({
</Button>
);
q.started = (
<Label monospace>{moment(q.startDttm).format('L HH:mm:ss')}</Label>
<Label monospace>
{extendedDayjs(q.startDttm).format('L HH:mm:ss')}
</Label>
);
q.querylink = (
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
VizType,
} from '@superset-ui/core';
import { useSelector, useDispatch } from 'react-redux';
import moment from 'moment';
import dayjs from 'dayjs';
import rison from 'rison';
import { createDatasource } from 'src/SqlLab/actions/sqlLab';
import { addDangerToast } from 'src/components/MessageToasts/actions';
Expand Down Expand Up @@ -164,7 +164,7 @@ export const SaveDatasetModal = ({
);

const getDefaultDatasetName = () =>
`${datasource?.name || UNTITLED} ${moment().format('L HH:mm:ss')}`;
`${datasource?.name || UNTITLED} ${dayjs().format('L HH:mm:ss')}`;
const [datasetName, setDatasetName] = useState(getDefaultDatasetName());
const [newOrOverwrite, setNewOrOverwrite] = useState(
DatasetRadioState.SaveNew,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { render, screen } from 'spec/helpers/testing-library';
import moment from 'moment';
import { extendedDayjs } from 'src/utils/dates';
import { TooltipContent } from './TooltipContent';

test('Rendering TooltipContent correctly - no timestep', () => {
Expand All @@ -31,7 +31,7 @@ test('Rendering TooltipContent correctly - no timestep', () => {
test('Rendering TooltipContent correctly - with timestep', () => {
render(<TooltipContent cachedTimestamp="01-01-2000" />);
expect(screen.getByTestId('tooltip-content')?.textContent).toBe(
`Loaded data cached ${moment
`Loaded data cached ${extendedDayjs
.utc('01-01-2000')
.fromNow()}. Click to force-refresh`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { FC } from 'react';
import moment from 'moment';
import { extendedDayjs } from 'src/utils/dates';
import { t } from '@superset-ui/core';

interface Props {
Expand All @@ -28,7 +28,7 @@ export const TooltipContent: FC<Props> = ({ cachedTimestamp }) => {
const cachedText = cachedTimestamp ? (
<span>
{t('Loaded data cached')}
<b> {moment.utc(cachedTimestamp).fromNow()}</b>
<b> {extendedDayjs.utc(cachedTimestamp).fromNow()}</b>
</span>
) : (
t('Loaded from cache')
Expand Down
6 changes: 4 additions & 2 deletions superset-frontend/src/components/Chart/chartAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
/* eslint no-undef: 'error' */
/* eslint no-param-reassign: ["error", { "props": false }] */
import moment from 'moment';
import {
FeatureFlag,
isDefined,
Expand All @@ -43,6 +42,7 @@ import { allowCrossDomain as domainShardingEnabled } from 'src/utils/hostNamesCo
import { updateDataMask } from 'src/dataMask/actions';
import { waitForAsyncData } from 'src/middleware/asyncEvent';
import { safeStringify } from 'src/utils/safeStringify';
import { extendedDayjs } from 'src/utils/dates';

export const CHART_UPDATE_STARTED = 'CHART_UPDATE_STARTED';
export function chartUpdateStarted(queryController, latestQueryFormData, key) {
Expand Down Expand Up @@ -454,7 +454,9 @@ export function exploreJSON(
formData.extra_filters && formData.extra_filters.length > 0,
viz_type: formData.viz_type,
data_age: resultItem.is_cached
? moment(new Date()).diff(moment.utc(resultItem.cached_dttm))
? extendedDayjs(new Date()).diff(
extendedDayjs.utc(resultItem.cached_dttm),
)
: null,
}),
),
Expand Down
15 changes: 9 additions & 6 deletions superset-frontend/src/components/LastUpdated/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ import {
MouseEventHandler,
} from 'react';

import moment, { Moment, MomentInput } from 'moment';
import { extendedDayjs } from 'src/utils/dates';
import { t, styled } from '@superset-ui/core';
import Icons from 'src/components/Icons';
import dayjs from 'dayjs';

const REFRESH_INTERVAL = 60000; // every minute

interface LastUpdatedProps {
updatedAt: MomentInput;
updatedAt: string | number | Date | undefined;
update?: MouseEventHandler<HTMLSpanElement>;
}
moment.updateLocale('en', {
extendedDayjs.updateLocale('en', {
calendar: {
lastDay: '[Yesterday at] LTS',
sameDay: '[Today at] LTS',
Expand Down Expand Up @@ -62,14 +63,16 @@ export const LastUpdated: FunctionComponent<LastUpdatedProps> = ({
updatedAt,
update,
}) => {
const [timeSince, setTimeSince] = useState<Moment>(moment(updatedAt));
const [timeSince, setTimeSince] = useState<dayjs.Dayjs>(
extendedDayjs(updatedAt),
);

useEffect(() => {
setTimeSince(() => moment(updatedAt));
setTimeSince(() => extendedDayjs(updatedAt));

// update UI every minute in case day changes
const interval = setInterval(() => {
setTimeSince(() => moment(updatedAt));
setTimeSince(() => extendedDayjs(updatedAt));
}, REFRESH_INTERVAL);

return () => clearInterval(interval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
RefObject,
} from 'react';

// TODO: @msyavuz - Replace with dayjs after migrating datepicker to antd5
import moment, { Moment } from 'moment';
import { styled, t } from '@superset-ui/core';
import { RangePicker } from 'src/components/DatePicker';
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/components/Timer/Timer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import moment from 'moment';
import { extendedDayjs } from 'src/utils/dates';
import Timer, { TimerProps } from './index';

export default {
Expand All @@ -32,7 +32,7 @@ InteractiveTimer.args = {

InteractiveTimer.argTypes = {
startTime: {
defaultValue: moment().utc().valueOf(),
defaultValue: extendedDayjs().utc().valueOf(),
table: {
disable: true,
},
Expand Down
Loading
Loading