Merge pull request #154 from hydralauncher/feat/format-playtime-in-hours

Feat/format playtime in hours
This commit is contained in:
Hydra 2024-05-03 19:37:20 +01:00 committed by GitHub
commit d7b92b472d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 45 additions and 24 deletions

View file

@ -69,6 +69,8 @@
"copied_link_to_clipboard": "Link copied",
"hours": "hours",
"minutes": "minutes",
"amount_hours": "{{amount}} hours",
"amount_minutes": "{{amount}} minutes",
"accuracy": "{{accuracy}}% accuracy",
"add_to_library": "Add to library",
"remove_from_library": "Remove from library",

View file

@ -61,6 +61,8 @@
"copied_link_to_clipboard": "Enlace copiado",
"hours": "horas",
"minutes": "minutos",
"amount_hours": "{{amount}} horas",
"amount_minutes": "{{amount}} minutos",
"accuracy": "{{accuracy}}% precisión",
"add_to_library": "Agregar a la biblioteca",
"remove_from_library": "Eliminar de la biblioteca",

View file

@ -61,6 +61,8 @@
"copied_link_to_clipboard": "Lien copié",
"hours": "heures",
"minutes": "minutes",
"amount_hours": "{{amount}} heures",
"amount_minutes": "{{amount}} minutes",
"accuracy": "{{accuracy}}% précision",
"add_to_library": "Ajouter à la bibliothèque",
"remove_from_library": "Supprimer de la bibliothèque",

View file

@ -66,6 +66,8 @@
"copied_link_to_clipboard": "Link másolva",
"hours": "óra",
"minutes": "perc",
"amount_hours": "{{amount}} óra",
"amount_minutes": "{{amount}} perc",
"accuracy": "{{accuracy}}% pontosság",
"add_to_library": "Hozzáadás a könyvtárhoz",
"remove_from_library": "Eltávolítás a könyvtárból",

View file

@ -69,6 +69,8 @@
"copied_link_to_clipboard": "Link copiato",
"hours": "ore",
"minutes": "minuti",
"amount_hours": "{{amount}} ore",
"amount_minutes": "{{amount}} minuti",
"accuracy": "{{accuratezza}}% di accuratezza",
"add_to_library": "Aggiungi alla libreria",
"remove_from_library": "Rimuovi dalla libreria",

View file

@ -64,6 +64,8 @@
"copied_link_to_clipboard": "Link copiado",
"hours": "horas",
"minutes": "minutos",
"amount_hours": "{{amount}} horas",
"amount_minutes": "{{amount}} minutos",
"accuracy": "{{accuracy}}% de precisão",
"add_to_library": "Adicionar à biblioteca",
"remove_from_library": "Remover da biblioteca",

View file

@ -8,20 +8,18 @@ import { WindowManager } from "./window-manager";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export const startProcessWatcher = async () => {
const sleepTime = 300;
const sleepTime = 500;
const gamesPlaytime = new Map<number, number>();
// eslint-disable-next-line no-constant-condition
while (true) {
await sleep(sleepTime);
const games = await gameRepository.find({
where: {
executablePath: Not(IsNull()),
},
});
if (games.length == 0) {
if (games.length === 0) {
continue;
}
@ -71,5 +69,7 @@ export const startProcessWatcher = async () => {
}
}
}
await sleep(sleepTime);
}
};

View file

@ -1,5 +1,5 @@
import { format } from "date-fns";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useDownload } from "@renderer/hooks";
@ -22,6 +22,8 @@ export interface HeroPanelProps {
getGame: () => void;
}
const MAX_MINUTES_TO_SHOW_IN_PLAYTIME = 120;
export function HeroPanel({
game,
gameDetails,
@ -30,7 +32,7 @@ export function HeroPanel({
getGame,
isGamePlaying,
}: HeroPanelProps) {
const { t } = useTranslation("game_details");
const { t, i18n } = useTranslation("game_details");
const [showBinaryNotFoundModal, setShowBinaryNotFoundModal] = useState(false);
const [lastTimePlayed, setLastTimePlayed] = useState("");
@ -48,29 +50,36 @@ export function HeroPanel({
} = useDownload();
const isGameDownloading = isDownloading && gameDownloading?.id === game?.id;
const updateLastTimePlayed = useCallback(() => {
setLastTimePlayed(
formatDistance(game.lastTimePlayed, new Date(), {
addSuffix: true,
})
);
}, [game?.lastTimePlayed, formatDistance]);
useEffect(() => {
if (game?.lastTimePlayed) {
updateLastTimePlayed();
setLastTimePlayed(
formatDistance(game.lastTimePlayed, new Date(), {
addSuffix: true,
})
);
}
}, [game?.lastTimePlayed, formatDistance]);
const interval = setInterval(() => {
updateLastTimePlayed();
}, 1000);
const numberFormatter = useMemo(() => {
return new Intl.NumberFormat(i18n.language, {
maximumFractionDigits: 1,
});
}, [i18n]);
return () => {
clearInterval(interval);
};
const formatPlayTime = () => {
const milliseconds = game?.playTimeInMilliseconds || 0;
const seconds = milliseconds / 1000;
const minutes = seconds / 60;
if (minutes < MAX_MINUTES_TO_SHOW_IN_PLAYTIME) {
return t("amount_minutes", {
amount: minutes.toFixed(0),
});
}
return () => {};
}, [game?.lastTimePlayed, updateLastTimePlayed]);
const hours = minutes / 60;
return t("amount_hours", { amount: numberFormatter.format(hours) });
};
const finalDownloadSize = useMemo(() => {
if (!game) return "N/A";
@ -139,7 +148,7 @@ export function HeroPanel({
<>
<p>
{t("play_time", {
amount: formatDistance(0, game.playTimeInMilliseconds),
amount: formatPlayTime(game.playTimeInMilliseconds),
})}
</p>