refactor updateLastTimePlayed, setInterval seems unnecessary

This commit is contained in:
Zamitto 2024-05-01 17:39:17 -03:00
parent 1569c3eb73
commit f1c37a0aaf

View File

@ -1,5 +1,5 @@
import { format } from "date-fns"; import { format } from "date-fns";
import { useCallback, useEffect, useMemo, useState } from "react"; import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useDownload } from "@renderer/hooks"; import { useDownload } from "@renderer/hooks";
@ -47,30 +47,20 @@ export function HeroPanel({
} = useDownload(); } = useDownload();
const isGameDownloading = isDownloading && gameDownloading?.id === game?.id; const isGameDownloading = isDownloading && gameDownloading?.id === game?.id;
const updateLastTimePlayed = useCallback(() => { useEffect(() => {
if (game?.lastTimePlayed) {
setLastTimePlayed( setLastTimePlayed(
formatDistance(game.lastTimePlayed, new Date(), { formatDistance(game.lastTimePlayed, new Date(), {
addSuffix: true, addSuffix: true,
}) })
); );
}
}, [game?.lastTimePlayed, formatDistance]); }, [game?.lastTimePlayed, formatDistance]);
useEffect(() => {
if (game?.lastTimePlayed) {
updateLastTimePlayed();
const interval = setInterval(() => {
updateLastTimePlayed();
}, 1000);
const formatPlayTime = (milliseconds: number) => { const formatPlayTime = (milliseconds: number) => {
const seconds = milliseconds / 1000 const seconds = milliseconds / 1000
const minutes = seconds / 60 const minutes = seconds / 60
return () => {
clearInterval(interval);
};
}
}, [game?.lastTimePlayed, updateLastTimePlayed]);
if (minutes < 120) { if (minutes < 120) {
return minutes.toFixed(0) + " " + t("minutes") return minutes.toFixed(0) + " " + t("minutes")
} }