From 3199e56661880d218038ce6fa94f8b4699d0d8e0 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 12 Sep 2024 20:39:49 -0300 Subject: [PATCH 1/3] feat: get trending games from api --- .../events/catalogue/get-trending-games.ts | 22 +++++ src/main/events/index.ts | 1 + src/preload/index.ts | 1 + src/renderer/src/components/hero/hero.tsx | 82 ++++++++----------- src/renderer/src/declaration.d.ts | 2 + src/types/index.ts | 6 ++ 6 files changed, 66 insertions(+), 48 deletions(-) create mode 100644 src/main/events/catalogue/get-trending-games.ts diff --git a/src/main/events/catalogue/get-trending-games.ts b/src/main/events/catalogue/get-trending-games.ts new file mode 100644 index 00000000..121549f3 --- /dev/null +++ b/src/main/events/catalogue/get-trending-games.ts @@ -0,0 +1,22 @@ +import { registerEvent } from "../register-event"; +import { HydraApi } from "@main/services"; +import { userPreferencesRepository } from "@main/repository"; +import { TrendingGame } from "@types"; + +const getTrendingGames = async (_event: Electron.IpcMainInvokeEvent) => { + const userPreferences = await userPreferencesRepository.findOne({ + where: { id: 1 }, + }); + + const language = userPreferences?.language || "en"; + + const trendingGames = await HydraApi.get( + "/games/trending", + { language }, + { needsAuth: false } + ); + + return trendingGames; +}; + +registerEvent("getTrendingGames", getTrendingGames); diff --git a/src/main/events/index.ts b/src/main/events/index.ts index 3963e4b0..b876c944 100644 --- a/src/main/events/index.ts +++ b/src/main/events/index.ts @@ -8,6 +8,7 @@ import "./catalogue/get-how-long-to-beat"; import "./catalogue/get-random-game"; import "./catalogue/search-games"; import "./catalogue/search-game-repacks"; +import "./catalogue/get-trending-games"; import "./hardware/get-disk-free-space"; import "./library/add-game-to-library"; import "./library/create-game-shortcut"; diff --git a/src/preload/index.ts b/src/preload/index.ts index 087d573a..c98ed25f 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -44,6 +44,7 @@ contextBridge.exposeInMainWorld("electron", { ipcRenderer.invoke("getGames", take, prevCursor), searchGameRepacks: (query: string) => ipcRenderer.invoke("searchGameRepacks", query), + getTrendingGames: () => ipcRenderer.invoke("getTrendingGames"), /* User preferences */ getUserPreferences: () => ipcRenderer.invoke("getUserPreferences"), diff --git a/src/renderer/src/components/hero/hero.tsx b/src/renderer/src/components/hero/hero.tsx index f7383041..55615e74 100644 --- a/src/renderer/src/components/hero/hero.tsx +++ b/src/renderer/src/components/hero/hero.tsx @@ -1,20 +1,15 @@ import { useNavigate } from "react-router-dom"; import * as styles from "./hero.css"; import { useEffect, useState } from "react"; -import { ShopDetails } from "@types"; -import { - buildGameDetailsPath, - getSteamLanguage, - steamUrlBuilder, -} from "@renderer/helpers"; +import { TrendingGame } from "@types"; import { useTranslation } from "react-i18next"; - -const FEATURED_GAME_TITLE = "ELDEN RING"; -const FEATURED_GAME_ID = "1245620"; +import Skeleton, { SkeletonTheme } from "react-loading-skeleton"; +import { vars } from "@renderer/theme.css"; export function Hero() { - const [featuredGameDetails, setFeaturedGameDetails] = - useState(null); + const [featuredGameDetails, setFeaturedGameDetails] = useState< + TrendingGame[] | null + >(null); const [isLoading, setIsLoading] = useState(false); const { i18n } = useTranslation(); @@ -25,11 +20,7 @@ export function Hero() { setIsLoading(true); window.electron - .getGameShopDetails( - FEATURED_GAME_ID, - "steam", - getSteamLanguage(i18n.language) - ) + .getTrendingGames() .then((result) => { setFeaturedGameDetails(result); }) @@ -38,41 +29,36 @@ export function Hero() { }); }, [i18n.language]); - return ( - - ); + + )); + } + + return null; } diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 29e4dcbb..f1f6eba9 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -18,6 +18,7 @@ import type { FriendRequestAction, UserFriends, UserBlocks, + TrendingGame, } from "@types"; import type { DiskSpace } from "check-disk-space"; @@ -56,6 +57,7 @@ declare global { prevCursor?: number ) => Promise<{ results: CatalogueEntry[]; cursor: number }>; searchGameRepacks: (query: string) => Promise; + getTrendingGames: () => Promise; /* Library */ addGameToLibrary: ( diff --git a/src/types/index.ts b/src/types/index.ts index 3260d274..8a1708c6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -339,3 +339,9 @@ export interface DownloadSource { createdAt: Date; updatedAt: Date; } + +export interface TrendingGame { + uri: string; + description: string; + background: string; +} From 813a46bf3e4f2786a619b1576ea87c8b2557707e Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 12 Sep 2024 20:52:54 -0300 Subject: [PATCH 2/3] feat: refactor --- src/renderer/src/components/hero/hero.tsx | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/renderer/src/components/hero/hero.tsx b/src/renderer/src/components/hero/hero.tsx index 55615e74..df0f8a7f 100644 --- a/src/renderer/src/components/hero/hero.tsx +++ b/src/renderer/src/components/hero/hero.tsx @@ -3,8 +3,7 @@ import * as styles from "./hero.css"; import { useEffect, useState } from "react"; import { TrendingGame } from "@types"; import { useTranslation } from "react-i18next"; -import Skeleton, { SkeletonTheme } from "react-loading-skeleton"; -import { vars } from "@renderer/theme.css"; +import Skeleton from "react-loading-skeleton"; export function Hero() { const [featuredGameDetails, setFeaturedGameDetails] = useState< @@ -30,11 +29,7 @@ export function Hero() { }, [i18n.language]); if (isLoading) { - return ( - - - - ); + return ; } if (featuredGameDetails?.length) { From b0bc754ffc888829f200938c4c7d63340ff926eb Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 12 Sep 2024 20:56:10 -0300 Subject: [PATCH 3/3] feat: catch error --- src/main/events/catalogue/get-trending-games.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/events/catalogue/get-trending-games.ts b/src/main/events/catalogue/get-trending-games.ts index 121549f3..8f8e02c0 100644 --- a/src/main/events/catalogue/get-trending-games.ts +++ b/src/main/events/catalogue/get-trending-games.ts @@ -14,7 +14,7 @@ const getTrendingGames = async (_event: Electron.IpcMainInvokeEvent) => { "/games/trending", { language }, { needsAuth: false } - ); + ).catch(() => []); return trendingGames; };