Skip to content

Commit aece225

Browse files
authored
Migrated moment.js to Luxon (#2540)
Signed-off-by: Benjamin Perez <benjamin@bexsoft.net>
1 parent 756f004 commit aece225

File tree

11 files changed

+65
-55
lines changed

11 files changed

+65
-55
lines changed

portal-ui/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@
3232
"luxon": "^3.1.1",
3333
"mds": "https://github.com/minio/mds.git#v0.0.9",
3434
"minio": "^7.0.32",
35-
"moment": "^2.29.4",
3635
"react": "^18.1.0",
3736
"react-chartjs-2": "^2.9.0",
3837
"react-component-export-image": "^1.0.6",
3938
"react-copy-to-clipboard": "^5.0.2",
4039
"react-dom": "^18.1.0",
4140
"react-dropzone": "^11.4.2",
4241
"react-grid-layout": "^1.2.0",
43-
"react-moment": "^1.1.1",
4442
"react-redux": "^8.0.5",
4543
"react-router-dom": "6.4.5",
4644
"react-virtualized": "^9.22.3",

portal-ui/src/screens/Console/Buckets/ListBuckets/Objects/ObjectDetails/FileVersionItem.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
import React from "react";
18-
import * as reactMoment from "react-moment";
1918
import Grid from "@mui/material/Grid";
20-
import { Theme } from "@mui/material/styles";
2119
import createStyles from "@mui/styles/createStyles";
20+
import { DateTime } from "luxon";
21+
import { Theme } from "@mui/material/styles";
2222
import { withStyles } from "@mui/styles";
2323
import { displayFileIconName } from "../ListObjects/utils";
2424
import { IFileInfo } from "./types";
@@ -215,6 +215,12 @@ const FileVersionItem = ({
215215
pill = "null";
216216
}
217217

218+
let lastModified = DateTime.now();
219+
220+
if (versionInfo.last_modified) {
221+
lastModified = DateTime.fromISO(versionInfo.last_modified);
222+
}
223+
218224
return (
219225
<Grid
220226
container
@@ -321,9 +327,7 @@ const FileVersionItem = ({
321327
<Grid item xs={12} className={classes.collapsableInfo}>
322328
<span className={classes.versionData}>
323329
<strong>Last modified:</strong>{" "}
324-
<reactMoment.default>
325-
{versionInfo.last_modified}
326-
</reactMoment.default>
330+
{lastModified.toFormat("ccc, LLL dd yyyy HH:mm:ss (ZZZZ)")}
327331
</span>
328332
<span className={classes.versionData}>
329333
<strong>Size:</strong> {niceBytes(versionInfo.size || "0")}

portal-ui/src/screens/Console/Common/FormComponents/DateTimePickerWrapper/DateTimePickerWrapper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import React, { Fragment } from "react";
1818
import { Grid, InputLabel, TextField, Tooltip } from "@mui/material";
1919
import { LocalizationProvider } from "@mui/x-date-pickers";
20-
import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment";
20+
import { AdapterLuxon } from "@mui/x-date-pickers/AdapterLuxon";
2121
import { DateTimePicker } from "@mui/x-date-pickers";
2222
import InputAdornment from "@mui/material/InputAdornment";
2323
import ScheduleIcon from "@mui/icons-material/Schedule";
@@ -279,7 +279,7 @@ const DateTimePickerWrapper = ({
279279
: classes.dateSelectorFormOverride;
280280

281281
const inputItem = (
282-
<LocalizationProvider dateAdapter={AdapterMoment}>
282+
<LocalizationProvider dateAdapter={AdapterLuxon}>
283283
<DateTimePicker
284284
value={value}
285285
onChange={onChange}

portal-ui/src/screens/Console/Common/FormComponents/DaysSelector/DaysSelector.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import React, { Fragment, useEffect, useState } from "react";
1818
import Grid from "@mui/material/Grid";
1919
import InputLabel from "@mui/material/InputLabel";
20-
import moment from "moment/moment";
20+
import { DateTime } from "luxon";
2121
import { Theme } from "@mui/material/styles";
2222
import createStyles from "@mui/styles/createStyles";
2323
import withStyles from "@mui/styles/withStyles";
@@ -111,10 +111,7 @@ const calculateNewTime = (
111111
hours: number,
112112
minutes: number
113113
) => {
114-
return moment(initialDate)
115-
.add(days, "days")
116-
.add(hours, "hours")
117-
.add(minutes, "minutes");
114+
return DateTime.fromJSDate(initialDate).plus({ days, hours, minutes });
118115
};
119116

120117
const DaysSelector = ({
@@ -130,22 +127,29 @@ const DaysSelector = ({
130127
const [selectedHours, setSelectedHours] = useState<number>(0);
131128
const [selectedMinutes, setSelectedMinutes] = useState<number>(0);
132129
const [validDate, setValidDate] = useState<boolean>(true);
133-
const [dateSelected, setDateSelected] = useState<moment.Moment>(moment());
130+
const [dateSelected, setDateSelected] = useState<DateTime>(DateTime.now());
134131

135132
useEffect(() => {
136-
setDateSelected(
137-
calculateNewTime(
138-
initialDate,
139-
selectedDays,
140-
selectedHours,
141-
selectedMinutes
142-
)
143-
);
133+
if (
134+
!isNaN(selectedHours) &&
135+
!isNaN(selectedDays) &&
136+
!isNaN(selectedMinutes)
137+
) {
138+
setDateSelected(
139+
calculateNewTime(
140+
initialDate,
141+
selectedDays,
142+
selectedHours,
143+
selectedMinutes
144+
)
145+
);
146+
}
144147
}, [initialDate, selectedDays, selectedHours, selectedMinutes]);
145148

146149
useEffect(() => {
147150
if (validDate) {
148-
onChange(dateSelected.format("YYYY-MM-DDTHH:mm:ss"), true);
151+
const formattedDate = dateSelected.toFormat("yyyy-MM-dd HH:mm:ss");
152+
onChange(formattedDate.split(" ").join("T"), true);
149153
} else {
150154
onChange("0000-00-00", false);
151155
}
@@ -275,7 +279,7 @@ const DaysSelector = ({
275279
{entity} will be available until:
276280
</div>{" "}
277281
<div className={classes.validTill}>
278-
{dateSelected.format("MM/DD/YYYY HH:mm:ss")}
282+
{dateSelected.toFormat("MM/dd/yyyy HH:mm:ss")}
279283
</div>
280284
</div>
281285
) : (

portal-ui/src/screens/Console/Common/TLSCertificate/TLSCertificate.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
import React from "react";
18+
import { DateTime } from "luxon";
1819
import { Theme } from "@mui/material/styles";
1920
import createStyles from "@mui/styles/createStyles";
2021
import withStyles from "@mui/styles/withStyles";
@@ -33,7 +34,6 @@ import {
3334
Typography,
3435
} from "@mui/material";
3536
import EventBusyIcon from "@mui/icons-material/EventBusy";
36-
import Moment from "react-moment";
3737
import CertificateIcon from "../../../../icons/CertificateIcon";
3838

3939
const styles = (theme: Theme) =>
@@ -111,6 +111,9 @@ const TLSCertificate = ({
111111
onDelete = () => {},
112112
}: ITLSCertificate) => {
113113
const certificates = certificateInfo.domains || [];
114+
115+
const expiry = DateTime.fromISO(certificateInfo.expiry);
116+
114117
return (
115118
<Chip
116119
key={certificateInfo.name}
@@ -130,9 +133,7 @@ const TLSCertificate = ({
130133
<EventBusyIcon color="inherit" fontSize="small" />
131134
&nbsp;
132135
<span className={"label"}>Expiry:&nbsp;</span>
133-
<span>
134-
<Moment format="YYYY/MM/DD">{certificateInfo.expiry}</Moment>
135-
</span>
136+
<span>{expiry.toFormat("yyyy/MM/dd")}</span>
136137
</Box>
137138
<Divider />
138139
<br />

portal-ui/src/screens/Console/Logs/ErrorLogs/ErrorLogs.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ import createStyles from "@mui/styles/createStyles";
2121
import withStyles from "@mui/styles/withStyles";
2222
import { useSelector } from "react-redux";
2323
import { FormControl, Grid, InputBase, MenuItem, Select } from "@mui/material";
24+
import { DateTime } from "luxon";
2425

25-
import moment from "moment/moment";
2626
import { ErrorResponseHandler } from "../../../../../src/common/types";
2727
import api from "../../../../../src/common/api";
2828
import { AppState, useAppDispatch } from "../../../../store";
2929

30-
import { LogMessage } from "../types";
3130
import { wsProtocol } from "../../../../utils/wsUtils";
3231
import {
3332
actionsTray,
@@ -149,7 +148,7 @@ const ErrorLogs = () => {
149148
// console.log(message.data.toString())
150149
// FORMAT: 00:35:17 UTC 01/01/2021
151150

152-
let m: LogMessage = JSON.parse(message.data.toString());
151+
let m: any = JSON.parse(message.data.toString());
153152
let isValidEntry = true;
154153
if (
155154
m.level === "" &&
@@ -161,7 +160,11 @@ const ErrorLogs = () => {
161160
) {
162161
isValidEntry = false;
163162
}
164-
const logTime = moment(m.time, "HH:mm:s UTC MM/DD/YYYY").toDate();
163+
164+
const logTime = DateTime.fromFormat(
165+
m.time,
166+
"HH:mm:ss UTC MM/dd/yyyy"
167+
).toJSDate();
165168

166169
m.time = logTime;
167170
m.key = Math.random();
@@ -401,5 +404,4 @@ const ErrorLogs = () => {
401404
);
402405
};
403406

404-
//export default withStyles(styles)(connector(ErrorLogs));
405407
export default ErrorLogs;

portal-ui/src/screens/Console/Logs/ErrorLogs/LogLine.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
import React, { Fragment, useState } from "react";
17-
import { LogMessage } from "../types";
17+
import { DateTime } from "luxon";
1818
import TableRow from "@mui/material/TableRow";
1919
import TableCell from "@mui/material/TableCell";
2020
import Collapse from "@mui/material/Collapse";
2121
import Box from "@mui/material/Box";
2222
import Typography from "@mui/material/Typography";
23-
import Moment from "react-moment";
23+
import { LogMessage } from "../types";
2424
import BoxArrowUp from "../../../../icons/BoxArrowUp";
2525
import BoxArrowDown from "../../../../icons/BoxArrowDown";
2626
import WarnFilledIcon from "../../../../icons/WarnFilledIcon";
@@ -159,7 +159,12 @@ const LogLine = (props: { log: LogMessage }) => {
159159

160160
titleLogMessage = (titleLogMessage || "").replace(tColorRegex, "");
161161

162-
let dateStr = <Moment format="YYYY/MM/DD UTC HH:mm:ss">{log.time}</Moment>;
162+
const logTime = DateTime.fromJSDate(log.time);
163+
164+
let dateStr = (
165+
<Fragment>{logTime.toFormat("yyyy/MM/dd HH:mm:ss (ZZZZ)")}</Fragment>
166+
);
167+
163168
if (log.time.getFullYear() === 1) {
164169
dateStr = <Fragment>n/a</Fragment>;
165170
}

portal-ui/src/screens/Console/Speedtest/Speedtest.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import { Theme } from "@mui/material/styles";
2222
import { useNavigate } from "react-router-dom";
2323
import { AppState } from "../../../store";
2424
import { Button } from "mds";
25+
import { DateTime } from "luxon";
2526
import createStyles from "@mui/styles/createStyles";
26-
import moment from "moment/moment";
2727
import PageHeader from "../Common/PageHeader/PageHeader";
2828
import {
2929
actionsTray,
@@ -123,14 +123,12 @@ const Speedtest = () => {
123123
`${wsProt}://${url.hostname}:${port}${baseUrl}ws/speedtest?&size=${size}${sizeUnit}&duration=${duration}s`
124124
);
125125

126-
const baseDate = moment();
126+
const baseDate = DateTime.now();
127127

128-
const currentTime = baseDate.unix() / 1000;
128+
const currentTime = baseDate.toUnixInteger() / 1000;
129129

130130
const incrementDate =
131-
baseDate
132-
.add(parseInt("10") * 2, "s" as moment.unitOfTime.DurationConstructor)
133-
.unix() / 1000;
131+
baseDate.plus({ seconds: parseInt("10") * 2 }).toUnixInteger() / 1000;
134132

135133
const totalSeconds = (incrementDate - currentTime) / 1000;
136134

@@ -160,7 +158,7 @@ const Speedtest = () => {
160158
return [...prSt, ...insertData];
161159
});
162160

163-
const currTime = moment().unix() / 1000;
161+
const currTime = DateTime.now().toUnixInteger() / 1000;
164162
setCurrentValue(currTime);
165163
};
166164
c.onclose = () => {

portal-ui/src/screens/Console/Tenants/TenantDetails/SubnetLicenseTenant.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Grid from "@mui/material/Grid";
2222
import { containerForHeader } from "../../Common/FormComponents/common/styleLibrary";
2323
import { Typography } from "@mui/material";
2424
import { niceBytes } from "../../../../common/utils";
25-
import Moment from "react-moment";
25+
import { DateTime } from "luxon";
2626
import { Link } from "react-router-dom";
2727
import Paper from "@mui/material/Paper";
2828
import { ITenant } from "../ListTenants/types";
@@ -99,6 +99,10 @@ const SubnetLicenseTenant = ({
9999
licenseInfo,
100100
activateProduct,
101101
}: ISubnetLicenseTenant) => {
102+
const expiryTime = tenant
103+
? DateTime.fromISO(tenant.subnet_license.expires_at)
104+
: DateTime.now();
105+
102106
return (
103107
<Paper
104108
className={
@@ -174,9 +178,7 @@ const SubnetLicenseTenant = ({
174178
gutterBottom
175179
className={classes.licenseInfoValue}
176180
>
177-
<Moment format="YYYY-MM-DD">
178-
{tenant.subnet_license.expires_at}
179-
</Moment>
181+
{expiryTime.toFormat("yyyy-MM-dd")}
180182
</Typography>
181183
</Grid>
182184
<Grid item xs={6}>

portal-ui/src/screens/Console/Trace/Trace.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
import React, { Fragment, useState } from "react";
18+
import { DateTime } from "luxon";
1819
import { Box, Grid } from "@mui/material";
1920
import { IMessageEvent, w3cwebsocket as W3CWebSocket } from "websocket";
2021
import { Button } from "mds";
@@ -37,7 +38,6 @@ import {
3738
import TableWrapper from "../Common/TableWrapper/TableWrapper";
3839
import PageHeader from "../Common/PageHeader/PageHeader";
3940
import CheckboxWrapper from "../Common/FormComponents/CheckboxWrapper/CheckboxWrapper";
40-
import moment from "moment/moment";
4141
import PageLayout from "../Common/Layout/PageLayout";
4242
import { FilterIcon } from "../../../icons";
4343

@@ -175,7 +175,8 @@ const Trace = ({ classes }: ITrace) => {
175175
};
176176
c.onmessage = (message: IMessageEvent) => {
177177
let m: TraceMessage = JSON.parse(message.data.toString());
178-
m.ptime = moment(m.time, "YYYY-MM-DD HH:mm:s.SSSS +0000 UTC").toDate();
178+
179+
m.ptime = DateTime.fromISO(m.time).toJSDate();
179180
m.key = Math.random();
180181
dispatch(traceMessageReceived(m));
181182
};

0 commit comments

Comments
 (0)