From 81a3ede4bdf8fc63ef68d141a91d54304dd028ed Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:31:37 -0300 Subject: [PATCH] refactor hero panel --- .../game-details/hero/hero-panel-actions.tsx | 97 +------------------ .../game-details/hero/hero-panel-playtime.tsx | 55 +++++++++-- .../pages/game-details/hero/hero-panel.tsx | 95 +++--------------- .../modals/game-options-modal.tsx | 7 +- 4 files changed, 62 insertions(+), 192 deletions(-) diff --git a/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx b/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx index e75c8807..ca1110d2 100644 --- a/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx +++ b/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx @@ -1,30 +1,18 @@ import { GearIcon, NoEntryIcon, PlusCircleIcon } from "@primer/octicons-react"; - -import { BinaryNotFoundModal } from "../../shared-modals/binary-not-found-modal"; - import { Button } from "@renderer/components"; import { useAppSelector, useDownload, useLibrary } from "@renderer/hooks"; import { useContext, useState } from "react"; import { useTranslation } from "react-i18next"; - import * as styles from "./hero-panel-actions.css"; import { gameDetailsContext } from "../game-details.context"; -import { Downloader } from "@shared"; import { GameOptionsModal } from "../modals/game-options-modal"; export function HeroPanelActions() { const [toggleLibraryGameDisabled, setToggleLibraryGameDisabled] = useState(false); - const [showBinaryNotFoundModal, setShowBinaryNotFoundModal] = useState(false); const [showGameOptionsModal, setShowGameOptionsModal] = useState(false); - const { - resumeDownload, - pauseDownload, - cancelDownload, - removeGameFromLibrary, - isGameDeleting, - } = useDownload(); + const { removeGameFromLibrary, isGameDeleting } = useDownload(); const { game, @@ -89,15 +77,6 @@ export function HeroPanelActions() { } }; - const openGameInstaller = () => { - if (game) { - window.electron.openGameInstaller(game.id).then((isBinaryInPath) => { - if (!isBinaryInPath) setShowBinaryNotFoundModal(true); - updateLibrary(); - }); - } - }; - const openGame = async () => { if (game) { if (game.executablePath) { @@ -140,52 +119,6 @@ export function HeroPanelActions() { ); - if (game?.status === "active" && game?.progress !== 1) { - return ( - <> - - - > - ); - } - - if (game?.status === "paused") { - return ( - <> - - - > - ); - } - if (game?.status === "removed") { return ( <> @@ -207,13 +140,7 @@ export function HeroPanelActions() { return ( <> {toggleGameOnLibraryButton} - + {showDownloadOptionsButton} > ); } @@ -242,26 +169,6 @@ export function HeroPanelActions() {
- {game?.progress === 1 && game?.folderName && ( - <> -{t("not_played_yet", { title: game?.title })}
; + const { progress, lastPacket } = useDownload(); + + const isGameDownloading = + game?.status === "active" && lastPacket?.game.id === game?.id; + + if (!game) return; + + let downloadContent: JSX.Element | null = null; + + if (game.status === "active") { + if (lastPacket?.isDownloadingMetadata && isGameDownloading) { + downloadContent ={t("downloading_metadata")}
; + } else if (game.progress !== 1) { + downloadContent = ( ++ {isGameDownloading ? progress : formatDownloadProgress(game.progress)} +
+ ); + } + } + + if (!game.lastTimePlayed) { + return ( + <> +{t("not_played_yet", { title: game?.title })}
+ {downloadContent} + > + ); + } + + if (isGameRunning) { + return ( + <> + {downloadContent || ( ++ {t("play_time", { + amount: formatPlayTime(), + })} +
+ )} + +{t("playing_now")}
+ > + ); } return ( @@ -58,9 +101,7 @@ export function HeroPanelPlaytime() { })} - {isGameRunning ? ( -{t("playing_now")}
- ) : ( + {downloadContent || ({t("last_time_played", { period: lastTimePlayed, 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 12ed84d5..22448116 100644 --- a/src/renderer/src/pages/game-details/hero/hero-panel.tsx +++ b/src/renderer/src/pages/game-details/hero/hero-panel.tsx @@ -1,14 +1,9 @@ import { format } from "date-fns"; -import { useContext, useMemo } from "react"; +import { useContext } from "react"; import { useTranslation } from "react-i18next"; import Color from "color"; - import { useDownload } from "@renderer/hooks"; - -import { formatDownloadProgress } from "@renderer/helpers"; import { HeroPanelActions } from "./hero-panel-actions"; -import { Downloader, formatBytes } from "@shared"; - import * as styles from "./hero-panel.css"; import { HeroPanelPlaytime } from "./hero-panel-playtime"; import { gameDetailsContext } from "../game-details.context"; @@ -18,99 +13,31 @@ export function HeroPanel() { const { game, repacks, gameColor } = useContext(gameDetailsContext); - const { progress, eta, lastPacket, isGameDeleting } = useDownload(); - - const finalDownloadSize = useMemo(() => { - if (game?.fileSize) return formatBytes(game.fileSize); - - if (lastPacket?.game.fileSize && game?.status === "active") - return formatBytes(lastPacket?.game.fileSize); - - return "N/A"; - }, [game, lastPacket?.game]); + const { lastPacket } = useDownload(); const isGameDownloading = game?.status === "active" && lastPacket?.game.id === game?.id; const getInfo = () => { - if (isGameDeleting(game?.id ?? -1)) return
{t("deleting")}
; + if (!game) { + const [latestRepack] = repacks; - if (game && (game.progress === 1 || !game.status)) { - return{progress}
-{t("downloading_metadata")}
+{t("updated_at", { updated_at: lastUpdate })}
+{t("download_options", { count: repacksCount })}
> ); } - const sizeDownloaded = formatBytes( - lastPacket?.game?.bytesDownloaded ?? game?.bytesDownloaded - ); - - const showPeers = - game?.downloader === Downloader.Torrent && - lastPacket?.numPeers !== undefined; - - return ( - <> -- {isGameDownloading - ? progress - : formatDownloadProgress(game?.progress)} - - {eta ? t("eta", { eta }) : t("calculating_eta")} -
- -- - {sizeDownloaded} / {finalDownloadSize} - - {showPeers && ( - - {lastPacket?.numPeers} peers / {lastPacket?.numSeeds} seeds - - )} -
- > - ); + return{t("no_downloads")}
; } - if (game?.status === "paused") { - const formattedProgress = formatDownloadProgress(game.progress); - - return ( - <> -- {formattedProgress} {t("paused")} -
-- {formatBytes(game.bytesDownloaded)} / {finalDownloadSize} -
- > - ); - } - - const [latestRepack] = repacks; - - if (latestRepack) { - const lastUpdate = format(latestRepack.uploadDate!, "dd/MM/yyyy"); - const repacksCount = repacks.length; - - return ( - <> -{t("updated_at", { updated_at: lastUpdate })}
-{t("download_options", { count: repacksCount })}
- > - ); - } - - return{t("no_downloads")}
; + return