diff --git a/src/main/events/user/get-user.ts b/src/main/events/user/get-user.ts index d3be856a..68d69969 100644 --- a/src/main/events/user/get-user.ts +++ b/src/main/events/user/get-user.ts @@ -1,7 +1,7 @@ import { registerEvent } from "../register-event"; import { HydraApi } from "@main/services"; import { steamGamesWorker } from "@main/workers"; -import { UserGame, UserProfile } from "@types"; +import { GameRunning, UserGame, UserProfile } from "@types"; import { convertSteamGameToCatalogueEntry } from "../helpers/search-games"; import { getSteamAppAsset } from "@main/helpers"; import { getUserFriends } from "./get-user-friends"; @@ -30,21 +30,34 @@ const getUser = async ( }) ); + const currentGame = await getGameRunning(profile.currentGame); + return { ...profile, libraryGames, recentGames, friends: friends.friends, totalFriends: friends.totalFriends, - currentGame: profile.currentGame - ? getSteamUserGame(profile.currentGame) - : null, + currentGame, }; } catch (err) { return null; } }; +const getGameRunning = async (currentGame): Promise => { + if (!currentGame) { + return null; + } + + const gameRunning = await getSteamUserGame(currentGame); + + return { + ...gameRunning, + sessionDurationInMillis: currentGame.sessionDurationInSeconds * 1000, + }; +}; + const getSteamUserGame = async (game): Promise => { const steamGame = await steamGamesWorker.run(Number(game.objectId), { name: "getById", diff --git a/src/renderer/src/components/sidebar/sidebar-profile.tsx b/src/renderer/src/components/sidebar/sidebar-profile.tsx index 81736e37..069831cb 100644 --- a/src/renderer/src/components/sidebar/sidebar-profile.tsx +++ b/src/renderer/src/components/sidebar/sidebar-profile.tsx @@ -78,7 +78,7 @@ export function SidebarProfile() { )} - {userDetails && gameRunning && ( + {userDetails && gameRunning?.iconUrl && ( {gameRunning.title}(null); const { gameRunning } = useAppSelector((state) => state.gameRunning); @@ -113,6 +118,15 @@ export function UserContent({ const isMe = userDetails?.id == userProfile.id; + useEffect(() => { + if (isMe && gameRunning) { + setCurrentGame(gameRunning); + return; + } + + setCurrentGame(userProfile.currentGame); + }, [gameRunning, isMe]); + useEffect(() => { if (isMe) fetchFriendRequests(); }, [isMe, fetchFriendRequests]); @@ -284,10 +298,10 @@ export function UserContent({ position: "relative", }} > - {gameRunning && isMe && ( + {currentGame && ( {gameRunning.title} )} @@ -315,7 +329,7 @@ export function UserContent({

{userProfile.displayName}

- {isMe && gameRunning && ( + {currentGame && (
- - {gameRunning.title} + + {currentGame.title}
{t("playing_for", { amount: formatDiffInMillis( - gameRunning.sessionDurationInMillis, + currentGame.sessionDurationInMillis, new Date() ), })} diff --git a/src/types/index.ts b/src/types/index.ts index d97433ae..65c73e99 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -141,9 +141,8 @@ export interface Game { export type LibraryGame = Omit; export interface GameRunning { - id: number; title: string; - iconUrl: string; + iconUrl: string | null; objectID: string; shop: GameShop; sessionDurationInMillis: number; @@ -318,7 +317,7 @@ export interface UserProfile { friends: UserFriend[]; totalFriends: number; relation: UserRelation | null; - currentGame: UserGame | null; + currentGame: GameRunning | null; } export interface UpdateProfileProps {