Skip to content

Commit 7228b0e

Browse files
committed
Revert "Replace moment with date-fns (#196)"
This reverts commit a321507.
1 parent 0df91e5 commit 7228b0e

6 files changed

Lines changed: 29 additions & 57 deletions

File tree

package-lock.json

Lines changed: 12 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
"@react-spring/web": "^9.7.5",
1313
"chart.js": "^3.7.1",
1414
"clsx": "^1.1.1",
15-
"date-fns": "^4.1.0",
1615
"firebase": "^9.6.9",
17-
"format-duration": "^3.0.2",
16+
"moment": "^2.29.1",
1817
"obscenity": "^0.4.4",
1918
"project-name-generator": "^2.1.9",
2019
"react": "^17.0.2",

src/components/ElapsedTime.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import formatDistanceStrict from "date-fns/formatDistanceStrict";
2-
31
import useMoment from "../hooks/useMoment";
42

53
// Wrapper around useMoment, since state hooks cause rerender of component
64
function ElapsedTime({ value }) {
7-
const time = useMoment(5000);
8-
const opts = { addSuffix: true };
9-
return <>{formatDistanceStrict(value, time, opts)}</>;
5+
const time = useMoment();
6+
return <>{time.to(value)}</>;
107
}
118

129
export default ElapsedTime;

src/hooks/useMoment.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import moment from "moment";
12
import { useEffect, useState } from "react";
23

34
import useFirebaseRef from "./useFirebaseRef";
45

56
function useMoment(delay = 1000) {
6-
const [time, setTime] = useState(Date.now()); // Estimated firebase server time
7+
const [time, setTime] = useState(moment()); // Estimated firebase server time
78
const [offset] = useFirebaseRef(".info/serverTimeOffset");
89

910
useEffect(() => {
1011
if (!delay) return;
11-
const id = setInterval(() => setTime(Date.now() + offset), delay);
12+
13+
const id = setInterval(() => {
14+
setTime(moment(Date.now() + offset));
15+
}, delay);
1216
return () => clearInterval(id);
1317
}, [offset, delay]);
1418

src/util.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import formatDuration from "format-duration";
1+
import moment from "moment";
22
import {
33
RegExpMatcher,
44
TextCensor,
@@ -196,11 +196,11 @@ export function generateName() {
196196
}
197197

198198
export function formatTime(t, hideSubsecond) {
199-
const res = formatDuration(Math.max(t, 0), {
200-
leading: true,
201-
ms: !hideSubsecond,
202-
});
203-
return hideSubsecond ? res : res.slice(0, -1);
199+
t = Math.max(t, 0);
200+
const hours = Math.floor(t / (3600 * 1000));
201+
const rest = t % (3600 * 1000);
202+
const format = hideSubsecond ? "mm:ss" : "mm:ss.SS";
203+
return (hours ? `${hours}:` : "") + moment.utc(rest).format(format);
204204
}
205205

206206
export function formatCount(count, singular, plural = null) {

src/util.test.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { badWords, formatANoun, formatTime, parseDuration } from "./util";
1+
import { badWords, formatANoun, parseDuration } from "./util";
22

33
describe("bad words filter", () => {
44
it("sort of works", () => {
@@ -18,7 +18,7 @@ describe("bad words filter", () => {
1818
});
1919
});
2020

21-
it("formatANoun works", () => {
21+
it("parseDuration works", () => {
2222
expect(formatANoun("Set")).toBe("a Set");
2323
expect(formatANoun("UltraSet")).toBe("an UltraSet");
2424
expect(formatANoun("GhostSet")).toBe("a GhostSet");
@@ -37,23 +37,3 @@ it("parseDuration works", () => {
3737
expect(parseDuration("300")).toBe(null);
3838
expect(parseDuration("")).toBe(null);
3939
});
40-
41-
it("formatTime works", () => {
42-
const check = (ms, expected) => {
43-
expect(formatTime(ms)).toBe(expected);
44-
expect(formatTime(ms, true)).toBe(expected.slice(0, -3));
45-
};
46-
check(-12345, "00:00.00");
47-
check(0, "00:00.00");
48-
check(999, "00:00.99");
49-
check(12349, "00:12.34");
50-
check(59999, "00:59.99");
51-
check(123000, "02:03.00");
52-
check(123459, "02:03.45");
53-
check(1234599, "20:34.59");
54-
check(3599999, "59:59.99");
55-
check(3600000, "01:00:00.00");
56-
check(3725669, "01:02:05.66");
57-
check(37256699, "10:20:56.69");
58-
check(372566999, "4:07:29:26.99");
59-
});

0 commit comments

Comments
 (0)