Skip to content

refactor(ui): drop moment.js #2644

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

Merged
merged 2 commits into from
Jan 11, 2024
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
41 changes: 9 additions & 32 deletions ui/package-lock.json

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

3 changes: 1 addition & 2 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
"@types/react-redux": "^7.1.31",
"buffer": "^6.0.3",
"clsx": "^2.1.0",
"date-fns": "^2.30.0",
"date-fns": "^3.2.0",
"dotenv": "^16.3.1",
"formik": "^2.4.5",
"highlight.js": "^11.9.0",
"moment": "^2.30.1",
"monaco-editor": "^0.45.0",
"monaco-themes": "^0.4.4",
"nightwind": "^1.1.13",
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/preferences/Preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default function Preferences() {
Display dates and times in UTC timezone
</p>
<p className="text-gray-400 mt-2 text-sm">
{inTimezone(new Date().toUTCString())}
{inTimezone(new Date().toISOString())}
</p>
</Switch.Label>
<dd className="text-gray-900 mt-1 flex text-sm sm:col-span-2 sm:mt-0">
Expand Down
58 changes: 27 additions & 31 deletions ui/src/components/segments/ConstraintForm.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Dialog } from '@headlessui/react';
import { QuestionMarkCircleIcon } from '@heroicons/react/20/solid';
import { XMarkIcon } from '@heroicons/react/24/outline';
import { addMinutes, format, formatISO, parseISO } from 'date-fns';
import { Form, Formik, useField, useFormikContext } from 'formik';
import moment from 'moment';
import { forwardRef, useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { Link } from 'react-router-dom';
Expand Down Expand Up @@ -132,40 +132,40 @@ function ConstraintValueDateTimeInput(props: ConstraintInputProps) {

const [field] = useField({
...props,
validate: (value) => {
const m = moment(value);
return m.isValid() ? undefined : 'Value is not a valid datetime';
validate: (value: string) => {
const m = parseISO(value);
return m ? undefined : 'Value is not a valid datetime';
}
});

const [fieldDate, setFieldDate] = useState(field.value?.split('T')[0] || '');
const [fieldTime, setFieldTime] = useState(field.value?.split('T')[1] || '');
const [fieldDate, setFieldDate] = useState('');
const [fieldTime, setFieldTime] = useState('');

useEffect(() => {
// if both date and time are set, then combine, parse, and set the value
if (
fieldDate &&
fieldDate.trim() !== '' &&
fieldTime &&
fieldTime.trim() !== ''
) {
if (timezone === Timezone.LOCAL) {
// if local timezone, then parse as local (moment default) and convert to UTC
const m = moment(`${fieldDate}T${fieldTime}`);
setFieldValue(field.name, m.utc().format());
return;
if (field.value) {
let m = parseISO(field.value);
if (timezone === Timezone.UTC) {
// if utc timezone, then convert to UTC
m = addMinutes(m, m.getTimezoneOffset());
}

// otherwise, parse as UTC
const m = moment.utc(`${fieldDate}T${fieldTime}`);
setFieldValue(field.name, m.format());
return;
setFieldDate(format(m, 'yyyy-MM-dd'));
setFieldTime(format(m, 'HH:mm'));
}
}, [field.value, timezone]);

// otherwise, if only date is set, then parse and set the value
useEffect(() => {
// if both date and time are set, then combine, parse, and set the value
if (fieldDate && fieldDate.trim() !== '') {
const m = moment(fieldDate);
setFieldValue(field.name, m.utc().format());
let d = `${fieldDate}T00:00:00`;
if (fieldTime && fieldTime.trim() !== '') {
d = `${fieldDate}T${fieldTime}:00`;
}
let m = parseISO(d);
if (timezone === Timezone.UTC) {
// if utc timezone, then convert to UTC
m = addMinutes(m, -m.getTimezoneOffset());
}
setFieldValue(field.name, formatISO(m));
}
}, [timezone, field.name, fieldDate, fieldTime, setFieldValue]);

Expand Down Expand Up @@ -209,11 +209,7 @@ function ConstraintValueDateTimeInput(props: ConstraintInputProps) {
type="time"
id="valueTime"
name="valueTime"
value={
timezone === Timezone.LOCAL
? moment(fieldTime, 'HH:mm Z').format('HH:mm')
: moment.utc(fieldTime, 'HH:mm Z').format('HH:mm')
}
value={fieldTime}
onChange={(e) => {
setFieldTime(e.target.value);
}}
Expand Down
8 changes: 5 additions & 3 deletions ui/src/data/hooks/timezone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment';
import { addMinutes, format, parseISO } from 'date-fns';
import { useSelector } from 'react-redux';
import { selectTimezone } from '~/app/preferences/preferencesSlice';
import { Timezone } from '~/types/Preferences';
Expand All @@ -7,9 +7,11 @@ export const useTimezone = () => {
const timezone = useSelector(selectTimezone);

const inTimezone = (v: string) => {
const d = parseISO(v);
return timezone === Timezone.LOCAL
? moment(v).format('YYYY-MM-DD HH:mm:ss')
: moment.utc(v).format('YYYY-MM-DD HH:mm:ss') + ' UTC';
? format(d, 'yyyy-MM-dd HH:mm:ss')
: format(addMinutes(d, d.getTimezoneOffset()), 'yyyy-MM-dd HH:mm:ss') +
' UTC';
};

return {
Expand Down