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(ui): replace moment-timezone with native Intl #12097

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"history": "^4.10.1",
"js-yaml": "^4.1.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"monaco-editor": "^0.44.0",
"prop-types": "^15.8.1",
"react": "^16.14.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react';
import {useContext, useEffect, useState} from 'react';

import {Autocomplete} from 'argo-ui';
import moment from 'moment-timezone';
import {Observable} from 'rxjs';
import {map, publishReplay, refCount} from 'rxjs/operators';
import * as models from '../../../../models';
Expand All @@ -21,8 +20,8 @@ import {FullHeightLogsViewer} from './full-height-logs-viewer';
import {extractJsonValue, JsonLogsFieldSelector, SelectedJsonFields} from './json-logs-field-selector';

const TZ_LOCALSTORAGE_KEY = 'DEFAULT_TZ';

const DEFAULT_TZ = process.env.DEFAULT_TZ || 'UTC';
const timezones = Intl.supportedValuesOf('timeZone');

interface WorkflowLogsViewerProps {
workflow: models.Workflow;
Expand Down Expand Up @@ -57,15 +56,17 @@ function parseTime(formattedString: string): undefined | ParsedTime {
return {quoted: table[1], fullstring: table[0]};
}

function parseAndTransform(formattedString: string, timezone: string) {
function parseAndTransform(formattedString: string, timeZone: string) {
const maybeTime = parseTime(formattedString);
if (maybeTime === undefined) {
return formattedString;
}

try {
const newTime = moment.tz(maybeTime.quoted, timezone).format('YYYY-MM-DDTHH:mm:ss z');
const newFormattedTime = `time=\"${newTime}\"`;
// hack to get a local ISO time: en-CA locale is very close to ISO (https://en.wikipedia.org/wiki/Date_and_time_notation_in_Canada)
const newTime = new Date(maybeTime.quoted).toLocaleString('en-CA', {timeZone, hour12: false}).replace(', ', 'T');
const shortTz = new Date().toLocaleTimeString('en-US', {timeZone, timeZoneName: 'short'}).split(' ')[2];
const newFormattedTime = `time=\"${newTime} ${shortTz}\"`;
const newFormattedString = formattedString.replace(maybeTime.fullstring, newFormattedTime);
return newFormattedString;
} catch {
Expand All @@ -90,8 +91,6 @@ export function WorkflowLogsViewer({workflow, nodeId, initialPodName, container,
const [uiTimezone, setUITimezone] = useState<string>(DEFAULT_TZ);
// timezone used for timezone formatting
const [timezone, setTimezone] = useLocalStorage<string>(TZ_LOCALSTORAGE_KEY, DEFAULT_TZ);
// list of timezones the moment-timezone library supports
const [timezones, setTimezones] = useState<string[]>([]);

// update the UI everytime the timezone changes
useEffect(() => {
Expand Down Expand Up @@ -158,16 +157,6 @@ export function WorkflowLogsViewer({workflow, nodeId, initialPodName, container,
return () => clearTimeout(x);
}, [logFilter]);

useEffect(() => {
const tzs = moment.tz.names();
const tzsSet = new Set<string>();
tzs.forEach(item => {
tzsSet.add(item);
});
const flatTzs = [...tzsSet];
setTimezones(flatTzs);
}, []);

const annotations = workflow.metadata.annotations || {};
const podNameVersion = annotations[ANNOTATION_KEY_POD_NAME_VERSION];

Expand Down
2 changes: 1 addition & 1 deletion ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5825,7 +5825,7 @@ mkdirp@^0.5.1:
dependencies:
minimist "^1.2.6"

moment-timezone@^0.5.34, moment-timezone@^0.5.43:
agilgur5 marked this conversation as resolved.
Show resolved Hide resolved
moment-timezone@^0.5.34:
version "0.5.43"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.43.tgz#3dd7f3d0c67f78c23cd1906b9b2137a09b3c4790"
integrity sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==
Expand Down