feat: added helper functions to the game status to keep it simple to read.

This commit is contained in:
lilezek 2024-04-30 09:29:29 +02:00
parent 666b1afcb6
commit f1d2a88442
8 changed files with 46 additions and 30 deletions

View File

@ -8,3 +8,19 @@ export enum GameStatus {
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;
}

View File

@ -20,6 +20,8 @@ const cancelGameDownload = async (
GameStatus.CheckingFiles,
GameStatus.Paused,
GameStatus.Seeding,
GameStatus.Finished,
GameStatus.Decompressing,
]),
},
});

View File

@ -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,

View File

@ -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<HTMLElement>(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,
})}
>
<button

View File

@ -64,10 +64,7 @@ export function useDownload() {
updateLibrary();
});
const isVerifying =
GameStatus.DownloadingMetadata == lastPacket?.game.status ||
GameStatus.CheckingFiles == lastPacket?.game.status ||
GameStatus.Decompressing == lastPacket?.game.status;
const isVerifying = GameStatus.isVerifying(lastPacket?.game.status);
const getETA = () => {
if (isVerifying || !isFinite(lastPacket?.timeRemaining)) {

View File

@ -11,6 +11,7 @@ import { BinaryNotFoundModal } from "../shared-modals/binary-not-found-modal";
import * as styles from "./downloads.css";
import { DeleteModal } from "./delete-modal";
import { formatBytes } from "@renderer/utils";
import { GameStatus } from "@globals";
export function Downloads() {
const { library, updateLibrary } = useLibrary();
@ -82,7 +83,7 @@ export function Downloads() {
<>
<p>{progress}</p>
{gameDownloading?.status !== "downloading" ? (
{gameDownloading?.status !== GameStatus.Downloading ? (
<p>{t(gameDownloading?.status)}</p>
) : (
<>
@ -99,7 +100,7 @@ export function Downloads() {
);
}
if (game?.status === "seeding") {
if (GameStatus.isReady(game?.status)) {
return (
<>
<p>{game?.repack.title}</p>
@ -107,11 +108,11 @@ export function Downloads() {
</>
);
}
if (game?.status === "cancelled") return <p>{t("cancelled")}</p>;
if (game?.status === "downloading_metadata")
if (game?.status === GameStatus.Cancelled) return <p>{t("cancelled")}</p>;
if (game?.status === GameStatus.DownloadingMetadata)
return <p>{t("starting_download")}</p>;
if (game?.status === "paused") {
if (game?.status === GameStatus.Paused) {
return (
<>
<p>{formatDownloadProgress(game.progress)}</p>
@ -144,7 +145,7 @@ export function Downloads() {
);
}
if (game?.status === "paused") {
if (game?.status === GameStatus.Paused) {
return (
<>
<Button onClick={() => resumeDownload(game.id)} theme="outline">
@ -157,7 +158,7 @@ export function Downloads() {
);
}
if (game?.status === "seeding") {
if (GameStatus.isReady(game?.status)) {
return (
<>
<Button
@ -175,7 +176,7 @@ export function Downloads() {
);
}
if (game?.status === "downloading_metadata") {
if (game?.status === GameStatus.DownloadingMetadata) {
return (
<Button onClick={() => cancelDownload(game.id)} theme="outline">
{t("cancel")}
@ -236,7 +237,7 @@ export function Downloads() {
<li
key={game.id}
className={styles.download({
cancelled: game.status === "cancelled",
cancelled: game.status === GameStatus.Cancelled,
})}
>
<AsyncImage

View File

@ -1,3 +1,4 @@
import { GameStatus } from "@globals";
import { NoEntryIcon, PlusCircleIcon } from "@primer/octicons-react";
import { Button } from "@renderer/components";
@ -135,7 +136,7 @@ export function HeroPanelActions({
);
}
if (game?.status === "paused") {
if (game?.status === GameStatus.Paused) {
return (
<>
<Button onClick={() => resumeDownload(game.id)} theme="outline">
@ -151,10 +152,10 @@ export function HeroPanelActions({
);
}
if (game?.status === "seeding" || (game && !game.status)) {
if (GameStatus.isReady(game?.status) || (game && !game.status)) {
return (
<>
{game?.status === "seeding" ? (
{GameStatus.isReady(game?.status) ? (
<Button
onClick={openGameInstaller}
theme="outline"
@ -183,7 +184,7 @@ export function HeroPanelActions({
);
}
if (game?.status === "cancelled") {
if (game?.status === GameStatus.Cancelled) {
return (
<>
<Button onClick={openRepacksModal} theme="outline" disabled={deleting}>

View File

@ -11,6 +11,7 @@ import * as styles from "./hero-panel.css";
import { useDate } from "@renderer/hooks/use-date";
import { formatBytes } from "@renderer/utils";
import { HeroPanelActions } from "./hero-panel-actions";
import { GameStatus } from "@globals";
export interface HeroPanelProps {
game: Game | null;
@ -94,7 +95,7 @@ export function HeroPanel({
{eta && <small>{t("eta", { eta })}</small>}
</p>
{gameDownloading?.status !== "downloading" ? (
{gameDownloading?.status !== GameStatus.Downloading ? (
<>
<p>{t(gameDownloading?.status)}</p>
{eta && <small>{t("eta", { eta })}</small>}
@ -112,7 +113,7 @@ export function HeroPanel({
);
}
if (game?.status === "paused") {
if (game?.status === GameStatus.Paused) {
return (
<>
<p>
@ -127,7 +128,7 @@ export function HeroPanel({
);
}
if (game?.status === "seeding" || (game && !game.status)) {
if (GameStatus.isReady(game?.status) || (game && !game.status)) {
if (!game.lastTimePlayed) {
return <p>{t("not_played_yet", { title: game.title })}</p>;
}