From a193d790b23eccd32e843d005d841d6bc590c705 Mon Sep 17 00:00:00 2001 From: Mehmet Salih Yavuz Date: Mon, 23 Dec 2024 20:42:43 +0300 Subject: [PATCH] refactor(moment): Replace Moment.js with DayJs (#31310) --- superset-frontend/package-lock.json | 20 ++++-- .../legacy-preset-chart-deckgl/package.json | 1 - .../legacy-preset-chart-nvd3/package.json | 2 +- .../legacy-preset-chart-nvd3/src/NVD3Vis.js | 11 +-- .../plugins/plugin-chart-echarts/package.json | 2 +- .../transformProps.ts | 7 +- .../src/BigNumber/utils.ts | 7 +- .../plugin-chart-handlebars/package.json | 2 +- .../Handlebars/HandlebarsViewer.tsx | 4 +- .../SqlLab/components/QueryTable/index.tsx | 7 +- .../components/SaveDatasetModal/index.tsx | 4 +- .../CachedLabel/TooltipContent.test.tsx | 4 +- .../components/CachedLabel/TooltipContent.tsx | 4 +- .../src/components/Chart/chartAction.js | 6 +- .../src/components/LastUpdated/index.tsx | 15 ++-- .../components/ListView/Filters/DateRange.tsx | 1 + .../src/components/Timer/Timer.stories.tsx | 4 +- ...mezoneSelector.DaylightSavingTime.test.tsx | 10 +-- .../TimezoneSelector.test.tsx | 40 ++++------- .../src/components/TimezoneSelector/index.tsx | 69 +++++++------------ .../src/dashboard/components/Header/index.jsx | 6 +- .../OverwriteConfirmModal.tsx | 6 +- .../components/SliceHeaderControls/index.tsx | 8 ++- .../DateFilterControl/utils/dateParser.ts | 1 + .../components/controls/TimeOffsetControl.tsx | 1 + .../features/alerts/AlertReportModal.test.tsx | 22 ++---- .../features/allEntities/AllEntitiesTable.tsx | 6 +- .../features/annotations/AnnotationModal.tsx | 1 + .../AddDataset/EditDataset/UsageTab/index.tsx | 6 +- .../src/features/home/ActivityTable.tsx | 9 ++- .../src/pages/AlertReportList/index.tsx | 7 +- .../src/pages/AllEntities/index.tsx | 3 +- .../src/pages/AnnotationList/index.tsx | 16 +++-- .../src/pages/ExecutionLogList/index.tsx | 13 ++-- .../src/pages/QueryHistoryList/index.tsx | 8 ++- superset-frontend/src/preamble.ts | 4 +- superset-frontend/src/utils/dates.js | 42 ----------- .../utils/{dates.test.js => dates.test.ts} | 27 +++++++- superset-frontend/src/utils/dates.ts | 69 +++++++++++++++++++ .../TimeTable/SparklineCell.tsx | 4 +- superset-frontend/webpack.config.js | 1 - 41 files changed, 268 insertions(+), 212 deletions(-) delete mode 100644 superset-frontend/src/utils/dates.js rename superset-frontend/src/utils/{dates.test.js => dates.test.ts} (71%) create mode 100644 superset-frontend/src/utils/dates.ts diff --git a/superset-frontend/package-lock.json b/superset-frontend/package-lock.json index 14b1fe74c..8fb8d157c 100644 --- a/superset-frontend/package-lock.json +++ b/superset-frontend/package-lock.json @@ -34175,7 +34175,8 @@ "version": "4.5.0", "resolved": "https://registry.npmjs.org/lodash.difference/-/lodash.difference-4.5.0.tgz", "integrity": "sha512-dS2j+W26TQ7taQBGN8Lbbq04ssV3emRw4NY58WErlTO29pIqS0HmoT5aJ9+TUQ1N3G+JOZSji4eugsWwGp9yPA==", - "dev": true + "dev": true, + "license": "MIT" }, "node_modules/lodash.escape": { "version": "4.0.1", @@ -39964,6 +39965,8 @@ }, "node_modules/moment": { "version": "2.30.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz", + "integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==", "license": "MIT", "engines": { "node": "*" @@ -39974,6 +39977,7 @@ "resolved": "https://registry.npmjs.org/moment-locales-webpack-plugin/-/moment-locales-webpack-plugin-1.2.0.tgz", "integrity": "sha512-QAi5v0OlPUP7GXviKMtxnpBAo8WmTHrUNN7iciAhNOEAd9evCOvuN0g1N7ThIg3q11GLCkjY1zQ2saRcf/43nQ==", "dev": true, + "license": "MIT", "dependencies": { "lodash.difference": "^4.5.0" }, @@ -58121,9 +58125,9 @@ "license": "Apache-2.0", "dependencies": { "d3-array": "^1.2.0", + "dayjs": "^1.11.13", "echarts": "^5.4.1", - "lodash": "^4.17.21", - "moment": "^2.30.1" + "lodash": "^4.17.21" }, "peerDependencies": { "@superset-ui/chart-controls": "*", @@ -58150,8 +58154,8 @@ "@superset-ui/chart-controls": "*", "@superset-ui/core": "*", "ace-builds": "^1.4.14", + "dayjs": "^1.11.13", "lodash": "^4.17.11", - "moment": "^2.26.0", "react": "^16.13.1", "react-ace": "^10.1.0", "react-dom": "^16.13.1" @@ -68727,9 +68731,9 @@ "version": "file:plugins/plugin-chart-echarts", "requires": { "d3-array": "^1.2.0", + "dayjs": "^1.11.13", "echarts": "^5.4.1", - "lodash": "^4.17.21", - "moment": "^2.30.1" + "lodash": "^4.17.21" } }, "@superset-ui/plugin-chart-handlebars": { @@ -86638,7 +86642,9 @@ "dev": true }, "moment": { - "version": "2.30.1" + "version": "2.30.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz", + "integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==" }, "moment-locales-webpack-plugin": { "version": "1.2.0", diff --git a/superset-frontend/plugins/legacy-preset-chart-deckgl/package.json b/superset-frontend/plugins/legacy-preset-chart-deckgl/package.json index 24b142bd4..63e5e39e4 100644 --- a/superset-frontend/plugins/legacy-preset-chart-deckgl/package.json +++ b/superset-frontend/plugins/legacy-preset-chart-deckgl/package.json @@ -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", diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/package.json b/superset-frontend/plugins/legacy-preset-chart-nvd3/package.json index ed8b4f33c..70bb6e196 100644 --- a/superset-frontend/plugins/legacy-preset-chart-nvd3/package.json +++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/package.json @@ -35,7 +35,7 @@ "dompurify": "^3.2.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" diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Vis.js b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Vis.js index 4bd9a2a94..5b70c24a3 100644 --- a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Vis.js +++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Vis.js @@ -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 { @@ -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 @@ -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, @@ -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 { diff --git a/superset-frontend/plugins/plugin-chart-echarts/package.json b/superset-frontend/plugins/plugin-chart-echarts/package.json index b3f235eff..e67df3297 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/package.json +++ b/superset-frontend/plugins/plugin-chart-echarts/package.json @@ -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": "*", diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/transformProps.ts index e04dda076..9ba045802 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/transformProps.ts @@ -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, @@ -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); if (dateObject.isValid()) { return dateObject.valueOf(); } diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/utils.ts b/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/utils.ts index 84fe31691..a25ec7432 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/utils.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/utils.ts @@ -17,7 +17,8 @@ * under the License. */ -import moment from 'moment'; +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc'; import { getTimeFormatter, getTimeFormatterForGranularity, @@ -25,9 +26,11 @@ import { 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(); } diff --git a/superset-frontend/plugins/plugin-chart-handlebars/package.json b/superset-frontend/plugins/plugin-chart-handlebars/package.json index a9610d11a..9df2df8fa 100644 --- a/superset-frontend/plugins/plugin-chart-handlebars/package.json +++ b/superset-frontend/plugins/plugin-chart-handlebars/package.json @@ -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" diff --git a/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx b/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx index 737416e19..883040743 100644 --- a/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx +++ b/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx @@ -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'; @@ -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: {{ }} diff --git a/superset-frontend/src/SqlLab/components/QueryTable/index.tsx b/superset-frontend/src/SqlLab/components/QueryTable/index.tsx index 43cc5da7b..3080b84f9 100644 --- a/superset-frontend/src/SqlLab/components/QueryTable/index.tsx +++ b/superset-frontend/src/SqlLab/components/QueryTable/index.tsx @@ -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'; @@ -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'; @@ -255,7 +254,9 @@ const QueryTable = ({ ); q.started = ( - + ); q.querylink = (