From 75c3bbf8580a69f56b0b55446d288e882b15a393 Mon Sep 17 00:00:00 2001 From: Hachi-R Date: Sat, 12 Apr 2025 14:23:02 -0300 Subject: [PATCH] feat: add option to show download speed in megabits --- src/locales/en/translation.json | 3 ++- src/locales/pt-BR/translation.json | 3 ++- src/renderer/src/hooks/use-download.ts | 12 ++++++++++-- .../src/pages/settings/settings-behavior.tsx | 13 +++++++++++++ src/shared/index.ts | 6 ++++++ src/types/level.types.ts | 1 + 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index d6a1f687..57a43435 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -354,7 +354,8 @@ "common_redist": "Common redistributables", "common_redist_description": "Common redistributables are required to run some games. Installing them is recommended to avoid issues.", "install_common_redist": "Install", - "installing_common_redist": "Installing…" + "installing_common_redist": "Installing…", + "show_download_speed_in_megabits": "Show download speed in megabits per second" }, "notifications": { "download_complete": "Download complete", diff --git a/src/locales/pt-BR/translation.json b/src/locales/pt-BR/translation.json index 5f2b62cc..4f27bcf9 100644 --- a/src/locales/pt-BR/translation.json +++ b/src/locales/pt-BR/translation.json @@ -341,7 +341,8 @@ "common_redist": "Componentes recomendados", "common_redist_description": "Componentes recomendados são necessários para executar alguns jogos. A instalação deles é recomendada para evitar problemas.", "install_common_redist": "Instalar", - "installing_common_redist": "Instalando…" + "installing_common_redist": "Instalando…", + "show_download_speed_in_megabits": "Exibir taxas de download em megabits por segundo" }, "notifications": { "download_complete": "Download concluído", diff --git a/src/renderer/src/hooks/use-download.ts b/src/renderer/src/hooks/use-download.ts index 6d9e04d3..a3b655d1 100644 --- a/src/renderer/src/hooks/use-download.ts +++ b/src/renderer/src/hooks/use-download.ts @@ -15,12 +15,14 @@ import type { StartGameDownloadPayload, } from "@types"; import { useDate } from "./use-date"; -import { formatBytes } from "@shared"; +import { formatBytes, formatBytesToMbps } from "@shared"; export function useDownload() { const { updateLibrary } = useLibrary(); const { formatDistance } = useDate(); + const userPrefs = useAppSelector((state) => state.userPreferences.value); + const { lastPacket, gamesWithDeletionInProgress } = useAppSelector( (state) => state.download ); @@ -99,8 +101,14 @@ export function useDownload() { return gamesWithDeletionInProgress.includes(objectId); }; + const formatDownloadSpeed = (downloadSpeed: number): string => { + return userPrefs?.showDownloadSpeedInMegabits + ? `${formatBytes(downloadSpeed)}/s` + : formatBytesToMbps(downloadSpeed); + }; + return { - downloadSpeed: `${formatBytes(lastPacket?.downloadSpeed ?? 0)}/s`, + downloadSpeed: formatDownloadSpeed(lastPacket?.downloadSpeed ?? 0), progress: formatDownloadProgress(lastPacket?.progress ?? 0), lastPacket, eta: calculateETA(), diff --git a/src/renderer/src/pages/settings/settings-behavior.tsx b/src/renderer/src/pages/settings/settings-behavior.tsx index 54b1a3c8..230bd065 100644 --- a/src/renderer/src/pages/settings/settings-behavior.tsx +++ b/src/renderer/src/pages/settings/settings-behavior.tsx @@ -23,6 +23,7 @@ export function SettingsBehavior() { enableAutoInstall: false, seedAfterDownloadComplete: false, showHiddenAchievementsDescription: false, + showDownloadSpeedInMegabits: false, }); const { t } = useTranslation("settings"); @@ -40,6 +41,8 @@ export function SettingsBehavior() { userPreferences.seedAfterDownloadComplete ?? false, showHiddenAchievementsDescription: userPreferences.showHiddenAchievementsDescription ?? false, + showDownloadSpeedInMegabits: + userPreferences.showDownloadSpeedInMegabits ?? false, }); } }, [userPreferences]); @@ -139,6 +142,16 @@ export function SettingsBehavior() { }) } /> + + + handleChange({ + showDownloadSpeedInMegabits: !form.showDownloadSpeedInMegabits, + }) + } + /> ); } diff --git a/src/shared/index.ts b/src/shared/index.ts index 0470728c..e679fdac 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -49,6 +49,12 @@ export const formatBytes = (bytes: number): string => { return `${Math.trunc(formatedByte * 10) / 10} ${FORMAT[base]}`; }; +export const formatBytesToMbps = (bytesPerSecond: number): string => { + const bitsPerSecond = bytesPerSecond * 8; + const mbps = bitsPerSecond / (1024 * 1024); + return `${Math.trunc(mbps * 10) / 10} Mbps`; +}; + export const pipe = (...fns: ((arg: T) => any)[]) => (arg: T) => diff --git a/src/types/level.types.ts b/src/types/level.types.ts index f98842a2..cc5b1d8a 100644 --- a/src/types/level.types.ts +++ b/src/types/level.types.ts @@ -85,6 +85,7 @@ export interface UserPreferences { repackUpdatesNotificationsEnabled?: boolean; achievementNotificationsEnabled?: boolean; friendRequestNotificationsEnabled?: boolean; + showDownloadSpeedInMegabits?: boolean; } export interface ScreenState {