From 5ab51e45478b87d7d50fab724afaaeec00ff27c8 Mon Sep 17 00:00:00 2001 From: Hydra Date: Mon, 13 May 2024 09:35:29 +0100 Subject: [PATCH] fix: changing get random game to work with shop block --- src/main/events/catalogue/get-random-game.ts | 7 ++-- src/main/services/steam-250.ts | 5 +-- src/renderer/src/declaration.d.ts | 3 +- .../src/pages/game-details/game-details.tsx | 41 ++++++++++++------- src/renderer/src/pages/home/home.tsx | 39 +++++++++--------- src/types/index.ts | 5 +++ 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/main/events/catalogue/get-random-game.ts b/src/main/events/catalogue/get-random-game.ts index 72f9cd90c..dd3741e3c 100644 --- a/src/main/events/catalogue/get-random-game.ts +++ b/src/main/events/catalogue/get-random-game.ts @@ -1,9 +1,10 @@ import { shuffle } from "lodash-es"; -import { Steam250Game, getSteam250List } from "@main/services"; +import { getSteam250List } from "@main/services"; import { registerEvent } from "../register-event"; import { searchGames, searchRepacks } from "../helpers/search-games"; +import type { Steam250Game } from "@types"; const state = { games: Array(), index: 0 }; @@ -25,8 +26,6 @@ const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => { return ""; } - const resultObjectId = state.games[state.index].objectID; - state.index += 1; if (state.index == state.games.length) { @@ -34,7 +33,7 @@ const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => { state.games = shuffle(state.games); } - return resultObjectId; + return state.games[state.index]; }; registerEvent(getRandomGame, { diff --git a/src/main/services/steam-250.ts b/src/main/services/steam-250.ts index db505b477..9833c278c 100644 --- a/src/main/services/steam-250.ts +++ b/src/main/services/steam-250.ts @@ -1,10 +1,7 @@ import axios from "axios"; import { JSDOM } from "jsdom"; -export interface Steam250Game { - title: string; - objectID: string; -} +import type { Steam250Game } from "@types"; export const requestSteam250 = async (path: string) => { return axios diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 4412598a6..e07742320 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -6,6 +6,7 @@ import type { GameShop, HowLongToBeatCategory, ShopDetails, + Steam250Game, TorrentProgress, UserPreferences, } from "@types"; @@ -41,7 +42,7 @@ declare global { shop: GameShop, language: string ) => Promise; - getRandomGame: () => Promise; + getRandomGame: () => Promise; getHowLongToBeat: ( objectID: string, shop: GameShop, diff --git a/src/renderer/src/pages/game-details/game-details.tsx b/src/renderer/src/pages/game-details/game-details.tsx index c9b0256ca..1b481104c 100644 --- a/src/renderer/src/pages/game-details/game-details.tsx +++ b/src/renderer/src/pages/game-details/game-details.tsx @@ -3,11 +3,21 @@ import { average } from "color.js"; import { useCallback, useEffect, useState } from "react"; import { useNavigate, useParams, useSearchParams } from "react-router-dom"; -import type { Game, GameRepack, GameShop, ShopDetails } from "@types"; +import { + Steam250Game, + type Game, + type GameRepack, + type GameShop, + type ShopDetails, +} from "@types"; import { Button } from "@renderer/components"; import { setHeaderTitle } from "@renderer/features"; -import { getSteamLanguage, steamUrlBuilder } from "@renderer/helpers"; +import { + buildGameDetailsPath, + getSteamLanguage, + steamUrlBuilder, +} from "@renderer/helpers"; import { useAppDispatch, useDownload } from "@renderer/hooks"; import starsAnimation from "@renderer/assets/lottie/stars.json"; @@ -35,7 +45,7 @@ export function GameDetails() { const { objectID, shop } = useParams(); const [isLoading, setIsLoading] = useState(false); - const [isLoadingRandomGame, setIsLoadingRandomGame] = useState(false); + const [randomGame, setRandomGame] = useState(null); const [color, setColor] = useState({ dark: "", light: "" }); const [gameDetails, setGameDetails] = useState(null); const [repacks, setRepacks] = useState([]); @@ -87,6 +97,10 @@ export function GameDetails() { setIsGamePlaying(false); dispatch(setHeaderTitle(title)); + window.electron.getRandomGame().then((randomGame) => { + setRandomGame(randomGame); + }); + Promise.all([ window.electron.getGameShopDetails( objectID!, @@ -98,7 +112,6 @@ export function GameDetails() { .then(([appDetails, repacks]) => { if (appDetails) setGameDetails(appDetails); setRepacks(repacks); - setIsLoadingRandomGame(false); }) .finally(() => { setIsLoading(false); @@ -163,15 +176,15 @@ export function GameDetails() { }); }; - const handleRandomizerClick = async () => { - setIsLoadingRandomGame(true); - const randomGameObjectID = await window.electron.getRandomGame(); - - const searchParams = new URLSearchParams({ - fromRandomizer: "1", - }); - - navigate(`/game/steam/${randomGameObjectID}?${searchParams.toString()}`); + const handleRandomizerClick = () => { + if (randomGame) { + navigate( + buildGameDetailsPath( + { ...randomGame, shop: "steam" }, + { fromRandomizer: "1" } + ) + ); + } }; return ( @@ -254,7 +267,7 @@ export function GameDetails() { className={styles.randomizerButton} onClick={handleRandomizerClick} theme="outline" - disabled={isLoadingRandomGame} + disabled={!randomGame} >
(null); + const [randomGame, setRandomGame] = useState(null); const [searchParams] = useSearchParams(); @@ -57,24 +60,22 @@ export function Home() { }; const getRandomGame = useCallback(() => { - setIsLoadingRandomGame(true); - - window.electron.getRandomGame().then((objectID) => { - if (objectID) { - randomGameObjectID.current = objectID; - setIsLoadingRandomGame(false); - } + window.electron.getRandomGame().then((game) => { + if (game) setRandomGame(game); }); }, []); const handleRandomizerClick = () => { - const searchParams = new URLSearchParams({ - fromRandomizer: "1", - }); - - navigate( - `/game/steam/${randomGameObjectID.current}?${searchParams.toString()}` - ); + if (randomGame) { + navigate( + buildGameDetailsPath( + { ...randomGame, shop: "steam" }, + { + fromRandomizer: "1", + } + ) + ); + } }; useEffect(() => { @@ -106,7 +107,7 @@ export function Home() {