From 1569c3eb73cc93f97e0025cfc103c825fe1477f8 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Wed, 1 May 2024 17:38:46 -0300 Subject: [PATCH 01/10] feat: format play time in hours --- src/renderer/src/pages/game-details/hero-panel.tsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/pages/game-details/hero-panel.tsx b/src/renderer/src/pages/game-details/hero-panel.tsx index 09a04970a..cce85c4f9 100644 --- a/src/renderer/src/pages/game-details/hero-panel.tsx +++ b/src/renderer/src/pages/game-details/hero-panel.tsx @@ -62,12 +62,22 @@ export function HeroPanel({ const interval = setInterval(() => { updateLastTimePlayed(); }, 1000); + const formatPlayTime = (milliseconds: number) => { + const seconds = milliseconds / 1000 + const minutes = seconds / 60 return () => { clearInterval(interval); }; } }, [game?.lastTimePlayed, updateLastTimePlayed]); + if (minutes < 120) { + return minutes.toFixed(0) + " " + t("minutes") + } + + const hours = minutes / 60 + return hours.toFixed(0) + " " + t("hours") + } const finalDownloadSize = useMemo(() => { if (!game) return "N/A"; @@ -136,7 +146,7 @@ export function HeroPanel({ <>

{t("play_time", { - amount: formatDistance(0, game.playTimeInMilliseconds), + amount: formatPlayTime(game.playTimeInMilliseconds), })}

From f1c37a0aafa895ab1807471e9ef7874c4ba8150f Mon Sep 17 00:00:00 2001 From: Zamitto Date: Wed, 1 May 2024 17:39:17 -0300 Subject: [PATCH 02/10] refactor updateLastTimePlayed, setInterval seems unnecessary --- .../src/pages/game-details/hero-panel.tsx | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/renderer/src/pages/game-details/hero-panel.tsx b/src/renderer/src/pages/game-details/hero-panel.tsx index cce85c4f9..3fc4a4b32 100644 --- a/src/renderer/src/pages/game-details/hero-panel.tsx +++ b/src/renderer/src/pages/game-details/hero-panel.tsx @@ -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"; @@ -47,30 +47,20 @@ 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 formatPlayTime = (milliseconds: number) => { const seconds = milliseconds / 1000 const minutes = seconds / 60 - return () => { - clearInterval(interval); - }; - } - }, [game?.lastTimePlayed, updateLastTimePlayed]); if (minutes < 120) { return minutes.toFixed(0) + " " + t("minutes") } From cce54f99371999230a259b027a2953ed9c036b2a Mon Sep 17 00:00:00 2001 From: Zamitto Date: Wed, 1 May 2024 17:44:06 -0300 Subject: [PATCH 03/10] feat: show decimal part of hour --- src/renderer/src/pages/game-details/hero-panel.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/src/pages/game-details/hero-panel.tsx b/src/renderer/src/pages/game-details/hero-panel.tsx index 3fc4a4b32..826142ef4 100644 --- a/src/renderer/src/pages/game-details/hero-panel.tsx +++ b/src/renderer/src/pages/game-details/hero-panel.tsx @@ -66,7 +66,7 @@ export function HeroPanel({ } const hours = minutes / 60 - return hours.toFixed(0) + " " + t("hours") + return hours.toFixed(1) + " " + t("hours") } const finalDownloadSize = useMemo(() => { From 7a1d0b2eaf269c26231361a6c97cd9b4dac18513 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Wed, 1 May 2024 18:53:10 -0300 Subject: [PATCH 04/10] feat: increase process watcher sleep time to 5s --- src/main/services/process-watcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index fc81e8d33..9cce8a7a1 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -8,7 +8,7 @@ import { WindowManager } from "./window-manager"; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); export const startProcessWatcher = async () => { - const sleepTime = 300; + const sleepTime = 5000; const gamesPlaytime = new Map(); // eslint-disable-next-line no-constant-condition From cfdd09d9acd812efc349785ec85854cce4cb00c3 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Wed, 1 May 2024 19:11:05 -0300 Subject: [PATCH 05/10] feat: move sleep to end of function on process-watcher --- src/main/services/process-watcher.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 9cce8a7a1..74811735e 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -13,15 +13,13 @@ export const startProcessWatcher = async () => { // 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); } }; From f4ee88c075541e099d92e75e07dd4b2b204517d3 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Wed, 1 May 2024 19:55:31 -0300 Subject: [PATCH 06/10] format date based on user language --- src/renderer/src/pages/game-details/hero-panel.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/pages/game-details/hero-panel.tsx b/src/renderer/src/pages/game-details/hero-panel.tsx index 826142ef4..fafd0aaa0 100644 --- a/src/renderer/src/pages/game-details/hero-panel.tsx +++ b/src/renderer/src/pages/game-details/hero-panel.tsx @@ -29,7 +29,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(""); @@ -65,8 +65,10 @@ export function HeroPanel({ return minutes.toFixed(0) + " " + t("minutes") } + const numberFormat = new Intl.NumberFormat(i18n.language, { maximumFractionDigits: 1 }) + const hours = minutes / 60 - return hours.toFixed(1) + " " + t("hours") + return numberFormat.format(hours) + " " + t("hours") } const finalDownloadSize = useMemo(() => { From a98a0eadd886b10a1a8f03a5ba5a3b9841e474c4 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Fri, 3 May 2024 09:07:05 -0300 Subject: [PATCH 07/10] feat: lower process-watcher sleep time to 500ms --- src/main/services/process-watcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 74811735e..ffed95e99 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -8,7 +8,7 @@ import { WindowManager } from "./window-manager"; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); export const startProcessWatcher = async () => { - const sleepTime = 5000; + const sleepTime = 500; const gamesPlaytime = new Map(); // eslint-disable-next-line no-constant-condition From 30ed41be61157b0c78a3fa504950219f6b9c919c Mon Sep 17 00:00:00 2001 From: Zamitto Date: Fri, 3 May 2024 09:19:56 -0300 Subject: [PATCH 08/10] feat: use i18n interpolation for minutes and hours --- src/locales/pt/translation.json | 2 ++ .../src/pages/game-details/hero-panel.tsx | 27 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/locales/pt/translation.json b/src/locales/pt/translation.json index 270e04518..ec126093a 100644 --- a/src/locales/pt/translation.json +++ b/src/locales/pt/translation.json @@ -61,6 +61,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", diff --git a/src/renderer/src/pages/game-details/hero-panel.tsx b/src/renderer/src/pages/game-details/hero-panel.tsx index fafd0aaa0..ae6d144e5 100644 --- a/src/renderer/src/pages/game-details/hero-panel.tsx +++ b/src/renderer/src/pages/game-details/hero-panel.tsx @@ -57,19 +57,24 @@ export function HeroPanel({ } }, [game?.lastTimePlayed, formatDistance]); - const formatPlayTime = (milliseconds: number) => { - const seconds = milliseconds / 1000 - const minutes = seconds / 60 - - if (minutes < 120) { - return minutes.toFixed(0) + " " + t("minutes") - } + const formatPlayTime = () => { + const milliseconds = game?.playTimeInMilliseconds || 0; + const seconds = milliseconds / 1000; + const minutes = seconds / 60; + + if (minutes < 120) { + return t("amount_minutes", { + amount: minutes.toFixed(0), + }); + } - const numberFormat = new Intl.NumberFormat(i18n.language, { maximumFractionDigits: 1 }) + const numberFormat = new Intl.NumberFormat(i18n.language, { + maximumFractionDigits: 1, + }); - const hours = minutes / 60 - return numberFormat.format(hours) + " " + t("hours") - } + const hours = minutes / 60; + return t("amount_hours", { amount: numberFormat.format(hours) }); + }; const finalDownloadSize = useMemo(() => { if (!game) return "N/A"; From 1098418a3e9897b3c19317083c04beb3130fef49 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Fri, 3 May 2024 09:21:16 -0300 Subject: [PATCH 09/10] feat: add translations --- src/locales/en/translation.json | 2 ++ src/locales/es/translation.json | 2 ++ src/locales/fr/translation.json | 2 ++ src/locales/hu/translation.json | 2 ++ src/locales/it/translation.json | 2 ++ 5 files changed, 10 insertions(+) diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index 03dd4b6da..d1df259b3 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -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", diff --git a/src/locales/es/translation.json b/src/locales/es/translation.json index e6f114c4e..8f36c12ff 100644 --- a/src/locales/es/translation.json +++ b/src/locales/es/translation.json @@ -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", diff --git a/src/locales/fr/translation.json b/src/locales/fr/translation.json index 3e247b07e..2e17f492b 100644 --- a/src/locales/fr/translation.json +++ b/src/locales/fr/translation.json @@ -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", diff --git a/src/locales/hu/translation.json b/src/locales/hu/translation.json index 901574a14..2f5dc79ba 100644 --- a/src/locales/hu/translation.json +++ b/src/locales/hu/translation.json @@ -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", diff --git a/src/locales/it/translation.json b/src/locales/it/translation.json index 0cc95e2aa..b4ff37237 100644 --- a/src/locales/it/translation.json +++ b/src/locales/it/translation.json @@ -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", From 2ae3f95a7321a9b8c96f5197f5b95e8dd8228232 Mon Sep 17 00:00:00 2001 From: Zamitto Date: Fri, 3 May 2024 09:35:13 -0300 Subject: [PATCH 10/10] useMemo to create numberFormater instance --- .../src/pages/game-details/hero/hero-panel.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/pages/game-details/hero/hero-panel.tsx b/src/renderer/src/pages/game-details/hero/hero-panel.tsx index 0445dda3b..a40c3aa47 100644 --- a/src/renderer/src/pages/game-details/hero/hero-panel.tsx +++ b/src/renderer/src/pages/game-details/hero/hero-panel.tsx @@ -22,6 +22,8 @@ export interface HeroPanelProps { getGame: () => void; } +const MAX_MINUTES_TO_SHOW_IN_PLAYTIME = 120; + export function HeroPanel({ game, gameDetails, @@ -58,23 +60,25 @@ export function HeroPanel({ } }, [game?.lastTimePlayed, formatDistance]); + const numberFormatter = useMemo(() => { + return new Intl.NumberFormat(i18n.language, { + maximumFractionDigits: 1, + }); + }, [i18n]); + const formatPlayTime = () => { const milliseconds = game?.playTimeInMilliseconds || 0; const seconds = milliseconds / 1000; const minutes = seconds / 60; - if (minutes < 120) { + if (minutes < MAX_MINUTES_TO_SHOW_IN_PLAYTIME) { return t("amount_minutes", { amount: minutes.toFixed(0), }); } - const numberFormat = new Intl.NumberFormat(i18n.language, { - maximumFractionDigits: 1, - }); - const hours = minutes / 60; - return t("amount_hours", { amount: numberFormat.format(hours) }); + return t("amount_hours", { amount: numberFormatter.format(hours) }); }; const finalDownloadSize = useMemo(() => {