From f1d2a8844281ef99f75dfb06b73d544fe7e63951 Mon Sep 17 00:00:00 2001 From: lilezek Date: Tue, 30 Apr 2024 09:29:29 +0200 Subject: [PATCH] feat: added helper functions to the game status to keep it simple to read. --- src/globals.ts | 16 ++++++++++++++++ .../events/torrenting/cancel-game-download.ts | 2 ++ .../components/bottom-panel/bottom-panel.tsx | 5 +++-- src/renderer/components/sidebar/sidebar.tsx | 13 +++++-------- src/renderer/hooks/use-download.ts | 5 +---- src/renderer/pages/downloads/downloads.tsx | 19 ++++++++++--------- .../pages/game-details/hero-panel-actions.tsx | 9 +++++---- .../pages/game-details/hero-panel.tsx | 7 ++++--- 8 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/globals.ts b/src/globals.ts index e4675aa7..b240172a 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -7,4 +7,20 @@ export enum GameStatus { Cancelled = "cancelled", Finished = "finished", Decompressing = "decompressing", +} + +export namespace GameStatus { + export const isDownloading = (status: GameStatus | "") => + status === GameStatus.Downloading || + status === GameStatus.DownloadingMetadata || + status === GameStatus.CheckingFiles; + + export const isVerifying = (status: GameStatus | "") => + GameStatus.DownloadingMetadata == status || + GameStatus.CheckingFiles == status || + GameStatus.Decompressing == status; + + export const isReady = (status: GameStatus | "") => + status === GameStatus.Finished || + status === GameStatus.Seeding; } \ No newline at end of file diff --git a/src/main/events/torrenting/cancel-game-download.ts b/src/main/events/torrenting/cancel-game-download.ts index 32aa79ae..af8ae89f 100644 --- a/src/main/events/torrenting/cancel-game-download.ts +++ b/src/main/events/torrenting/cancel-game-download.ts @@ -20,6 +20,8 @@ const cancelGameDownload = async ( GameStatus.CheckingFiles, GameStatus.Paused, GameStatus.Seeding, + GameStatus.Finished, + GameStatus.Decompressing, ]), }, }); diff --git a/src/renderer/components/bottom-panel/bottom-panel.tsx b/src/renderer/components/bottom-panel/bottom-panel.tsx index c870823e..46e278ca 100644 --- a/src/renderer/components/bottom-panel/bottom-panel.tsx +++ b/src/renderer/components/bottom-panel/bottom-panel.tsx @@ -7,6 +7,7 @@ import { vars } from "@renderer/theme.css"; import { useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import { VERSION_CODENAME } from "@renderer/constants"; +import { GameStatus } from "@globals"; export function BottomPanel() { const { t } = useTranslation("bottom_panel"); @@ -23,10 +24,10 @@ export function BottomPanel() { const status = useMemo(() => { if (isDownloading) { - if (game.status === "downloading_metadata") + if (game.status === GameStatus.DownloadingMetadata) return t("downloading_metadata", { title: game.title }); - if (game.status === "checking_files") + if (game.status === GameStatus.CheckingFiles) return t("checking_files", { title: game.title, percentage: progress, diff --git a/src/renderer/components/sidebar/sidebar.tsx b/src/renderer/components/sidebar/sidebar.tsx index 2d853553..cd4aff92 100644 --- a/src/renderer/components/sidebar/sidebar.tsx +++ b/src/renderer/components/sidebar/sidebar.tsx @@ -14,6 +14,7 @@ import { MarkGithubIcon } from "@primer/octicons-react"; import DiscordLogo from "@renderer/assets/discord-icon.svg"; import XLogo from "@renderer/assets/x-icon.svg"; import * as styles from "./sidebar.css"; +import { GameStatus } from "@globals"; const socials = [ { @@ -57,9 +58,7 @@ export function Sidebar() { }, [gameDownloading?.id, updateLibrary]); const isDownloading = library.some((game) => - ["downloading", "checking_files", "downloading_metadata"].includes( - game.status - ) + GameStatus.isDownloading(game.status) ); const sidebarRef = useRef(null); @@ -118,12 +117,10 @@ export function Sidebar() { }, [isResizing]); const getGameTitle = (game: Game) => { - if (game.status === "paused") return t("paused", { title: game.title }); + if (game.status === GameStatus.Paused) return t("paused", { title: game.title }); if (gameDownloading?.id === game.id) { - const isVerifying = ["downloading_metadata", "checking_files"].includes( - gameDownloading?.status - ); + const isVerifying = GameStatus.isVerifying(gameDownloading.status); if (isVerifying) return t(gameDownloading.status, { @@ -203,7 +200,7 @@ export function Sidebar() { className={styles.menuItem({ active: location.pathname === `/game/${game.shop}/${game.objectID}`, - muted: game.status === "cancelled", + muted: game.status === GameStatus.Cancelled, })} >