Skip to content

Commit

Permalink
Merge pull request #949 from hydralauncher/feat/get-trending-games-fr…
Browse files Browse the repository at this point in the history
…om-api

feat: get trending games from api
  • Loading branch information
thegrannychaseroperation authored Sep 13, 2024
2 parents 87cacdf + b0bc754 commit 02ca506
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 49 deletions.
22 changes: 22 additions & 0 deletions src/main/events/catalogue/get-trending-games.ts
Original file line number Diff line number Diff line change
@@ -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<TrendingGame[]>(
"/games/trending",
{ language },
{ needsAuth: false }
).catch(() => []);

return trendingGames;
};

registerEvent("getTrendingGames", getTrendingGames);
1 change: 1 addition & 0 deletions src/main/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
79 changes: 30 additions & 49 deletions src/renderer/src/components/hero/hero.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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 from "react-loading-skeleton";

export function Hero() {
const [featuredGameDetails, setFeaturedGameDetails] =
useState<ShopDetails | null>(null);
const [featuredGameDetails, setFeaturedGameDetails] = useState<
TrendingGame[] | null
>(null);
const [isLoading, setIsLoading] = useState(false);

const { i18n } = useTranslation();
Expand All @@ -25,11 +19,7 @@ export function Hero() {
setIsLoading(true);

window.electron
.getGameShopDetails(
FEATURED_GAME_ID,
"steam",
getSteamLanguage(i18n.language)
)
.getTrendingGames()
.then((result) => {
setFeaturedGameDetails(result);
})
Expand All @@ -38,41 +28,32 @@ export function Hero() {
});
}, [i18n.language]);

return (
<button
type="button"
onClick={() =>
navigate(
buildGameDetailsPath({
title: FEATURED_GAME_TITLE,
objectID: FEATURED_GAME_ID,
shop: "steam",
})
)
}
className={styles.hero}
>
<div className={styles.backdrop}>
<img
src="https://cdn2.steamgriddb.com/hero/95eb39b541856d43649b208b65b6ca9f.jpg"
alt={FEATURED_GAME_TITLE}
className={styles.heroMedia}
/>

<div className={styles.content}>
if (isLoading) {
return <Skeleton className={styles.hero} />;
}

if (featuredGameDetails?.length) {
return featuredGameDetails.map((game, index) => (
<button
type="button"
onClick={() => navigate(game.uri)}
className={styles.hero}
key={index}
>
<div className={styles.backdrop}>
<img
src={steamUrlBuilder.logo(FEATURED_GAME_ID)}
width="250px"
alt={FEATURED_GAME_TITLE}
src={game.background}
alt={game.description}
className={styles.heroMedia}
/>

{!isLoading && featuredGameDetails && (
<p className={styles.description}>
{featuredGameDetails?.short_description}
</p>
)}
<div className={styles.content}>
<p className={styles.description}>{game.description}</p>
</div>
</div>
</div>
</button>
);
</button>
));
}

return null;
}
2 changes: 2 additions & 0 deletions src/renderer/src/declaration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
FriendRequestAction,
UserFriends,
UserBlocks,
TrendingGame,
} from "@types";
import type { DiskSpace } from "check-disk-space";

Expand Down Expand Up @@ -56,6 +57,7 @@ declare global {
prevCursor?: number
) => Promise<{ results: CatalogueEntry[]; cursor: number }>;
searchGameRepacks: (query: string) => Promise<GameRepack[]>;
getTrendingGames: () => Promise<TrendingGame[]>;

/* Library */
addGameToLibrary: (
Expand Down
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,9 @@ export interface DownloadSource {
createdAt: Date;
updatedAt: Date;
}

export interface TrendingGame {
uri: string;
description: string;
background: string;
}

0 comments on commit 02ca506

Please sign in to comment.