Skip to content

Commit

Permalink
refactor: use useToggle instead of useState
Browse files Browse the repository at this point in the history
  • Loading branch information
CostasAK committed Jul 9, 2024
1 parent 684c701 commit 5b4beeb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
}
},
"dependencies": {
"@uidotdev/usehooks": "^2.4.1",
"clsx": "^2.1.1",
"dayjs": "^1.11.11",
"eslint-config-prettier": "^9.1.0",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

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

15 changes: 8 additions & 7 deletions src/layout/footer.jsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
import { useToggle } from "@uidotdev/usehooks";
import PropTypes from "prop-types";
import { useEffect, useState } from "react";
import { useEffect } from "react";
import { MINUTE } from "../constants/time";
import { cn } from "../utils/cn";
import { formatTime } from "../utils/format-time";

function useLocalTime() {
const [flipFlop, setFlipFlop] = useState(false);
const [on, toggle] = useToggle();

useEffect(() => {
const timer = setTimeout(
() => {
setFlipFlop(!flipFlop);
toggle();
},
MINUTE - (Date.now() % MINUTE),
);

return () => clearTimeout(timer);
}, [flipFlop]);
}, [on, toggle]);

return formatTime();
}

function useEorzeanTime() {
const [flipFlop, setFlipFlop] = useState(false);
const [on, toggle] = useToggle();

const eorzeanFactor = 144 / 7;

useEffect(() => {
const timer = setTimeout(
() => {
setFlipFlop(!flipFlop);
toggle();
},
(MINUTE - ((Date.now() * eorzeanFactor) % MINUTE)) / eorzeanFactor,
);

return () => clearTimeout(timer);
}, [flipFlop, eorzeanFactor]);
}, [on, toggle, eorzeanFactor]);

return formatTime(Date.now() * eorzeanFactor, true);
}
Expand Down

0 comments on commit 5b4beeb

Please sign in to comment.