diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 40f55c48..1be5c748 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -5,6 +5,7 @@ import { gameRepository } from "@main/repository"; import { getProcesses } from "@main/helpers"; import { WindowManager } from "./window-manager"; import { createGame, updateGamePlaytime } from "./library-sync"; +import { RunningGameEvent } from "@types"; const gamesPlaytime = new Map< number, @@ -96,10 +97,13 @@ export const watchProcesses = async () => { } if (WindowManager.mainWindow) { - const gamesRunningIds = Array.from(gamesPlaytime.keys()); + const runningGames = Array.from(gamesPlaytime.entries()).map((entry) => { + return { id: entry[0], sessionStartTimestamp: entry[1].firstTick }; + }); + WindowManager.mainWindow.webContents.send( "on-games-running", - gamesRunningIds + runningGames as RunningGameEvent ); } }; diff --git a/src/preload/index.ts b/src/preload/index.ts index a5378495..b5c46470 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -8,6 +8,7 @@ import type { UserPreferences, AppUpdaterEvent, StartGameDownloadPayload, + RunningGameEvent, } from "@types"; contextBridge.exposeInMainWorld("electron", { @@ -84,9 +85,11 @@ contextBridge.exposeInMainWorld("electron", { ipcRenderer.invoke("deleteGameFolder", gameId), getGameByObjectID: (objectID: string) => ipcRenderer.invoke("getGameByObjectID", objectID), - onGamesRunning: (cb: (gamesId: number[]) => void) => { - const listener = (_event: Electron.IpcRendererEvent, gamesId: number[]) => - cb(gamesId); + onRunningGames: (cb: (runningGames: RunningGameEvent) => void) => { + const listener = ( + _event: Electron.IpcRendererEvent, + runningGames: RunningGameEvent + ) => cb(runningGames); ipcRenderer.on("on-games-running", listener); return () => ipcRenderer.removeListener("on-games-running", listener); }, diff --git a/src/renderer/src/app.tsx b/src/renderer/src/app.tsx index 93e7eb81..af58497a 100644 --- a/src/renderer/src/app.tsx +++ b/src/renderer/src/app.tsx @@ -97,16 +97,18 @@ export function App() { }, [dispatch, fetchUserDetails]); useEffect(() => { - const unsubscribe = window.electron.onGamesRunning((gamesIds) => { - if (gamesIds.length) { - const lastGame = gamesIds.at(-1); - const libraryGame = library.find((library) => library.id == lastGame); + const unsubscribe = window.electron.onRunningGames((runningGames) => { + if (runningGames.length) { + const lastGame = runningGames[runningGames.length - 1]; + const libraryGame = library.find( + (library) => library.id === lastGame.id + ); if (libraryGame) { dispatch( setRunningGame({ ...libraryGame, - sessionStartTimestamp: new Date().getTime(), + sessionStartTimestamp: lastGame.sessionStartTimestamp, }) ); return; diff --git a/src/renderer/src/context/game-details/game-details.context.tsx b/src/renderer/src/context/game-details/game-details.context.tsx index e55a5340..1431190e 100644 --- a/src/renderer/src/context/game-details/game-details.context.tsx +++ b/src/renderer/src/context/game-details/game-details.context.tsx @@ -109,20 +109,19 @@ export function GameDetailsContextProvider({ }, [objectID, gameTitle, dispatch]); useEffect(() => { - const listeners = [ - window.electron.onGamesRunning((gamesIds) => { - const updatedIsGameRunning = !!game?.id && gamesIds.includes(game.id); + const unsubscribe = window.electron.onRunningGames((gamesIds) => { + const updatedIsGameRunning = + !!game?.id && + !!gamesIds.find((runningGame) => runningGame.id == game.id); - if (isGameRunning != updatedIsGameRunning) { - updateGame(); - } - - setisGameRunning(updatedIsGameRunning); - }), - ]; + if (isGameRunning != updatedIsGameRunning) { + updateGame(); + } + setisGameRunning(updatedIsGameRunning); + }); return () => { - listeners.forEach((unsubscribe) => unsubscribe()); + unsubscribe(); }; }, [game?.id, isGameRunning, updateGame]); diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 1e983738..cbc7b62e 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -14,6 +14,7 @@ import type { RealDebridUser, DownloadSource, UserProfile, + RunningGameEvent, } from "@types"; import type { DiskSpace } from "check-disk-space"; @@ -71,8 +72,8 @@ declare global { removeGame: (gameId: number) => Promise; deleteGameFolder: (gameId: number) => Promise; getGameByObjectID: (objectID: string) => Promise; - onGamesRunning: ( - cb: (gamesId: number[]) => void + onRunningGames: ( + cb: (runningGames: RunningGameEvent) => void ) => () => Electron.IpcRenderer; onLibraryBatchComplete: (cb: () => void) => () => Electron.IpcRenderer; diff --git a/src/renderer/src/helpers.ts b/src/renderer/src/helpers.ts index 9b6344a7..d37612d4 100644 --- a/src/renderer/src/helpers.ts +++ b/src/renderer/src/helpers.ts @@ -43,12 +43,5 @@ export const buildGameDetailsPath = ( return `/game/${game.shop}/${game.objectID}?${searchParams.toString()}`; }; -export const darkenColor = ( - color: string, - amount: number, - alpha: number = 1 -) => { - const newColor = new Color(color).darken(amount).alpha(alpha).toString(); - console.log(color, newColor); - return newColor; -}; +export const darkenColor = (color: string, amount: number, alpha: number = 1) => + new Color(color).darken(amount).alpha(alpha).toString(); diff --git a/src/renderer/src/hooks/use-user-details.ts b/src/renderer/src/hooks/use-user-details.ts index e17ad95f..d73e2a07 100644 --- a/src/renderer/src/hooks/use-user-details.ts +++ b/src/renderer/src/hooks/use-user-details.ts @@ -47,7 +47,10 @@ export function useUserDetails() { const profileBackground = `#151515e6`; dispatch(setProfileBackground(profileBackground)); - window.localStorage.setItem("userDetails", JSON.stringify(userDetails)); + window.localStorage.setItem( + "userDetails", + JSON.stringify({ ...userDetails, profileBackground }) + ); } }, [dispatch] diff --git a/src/renderer/src/pages/user/user-content.tsx b/src/renderer/src/pages/user/user-content.tsx index 2148f7e2..8557ba82 100644 --- a/src/renderer/src/pages/user/user-content.tsx +++ b/src/renderer/src/pages/user/user-content.tsx @@ -159,7 +159,7 @@ export function UserContent({ {t("playing_for", { amount: formatDistance( runningGame.sessionStartTimestamp, - new Date() + performance.now() ), })} diff --git a/src/types/index.ts b/src/types/index.ts index cda9bf72..ffc07b8a 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -127,7 +127,13 @@ export interface Game { export type LibraryGame = Omit; +export type RunningGameEvent = { + id: number; + sessionStartTimestamp: number; +}[]; + export interface RunningGame { + id: number; title: string; iconUrl: string; objectID: string;