feat: grayscale and update game details context on achievement event

This commit is contained in:
Zamitto 2024-09-26 17:37:07 -03:00
parent 50b34dc864
commit 54dae87a58
7 changed files with 43 additions and 1 deletions

BIN
ludusavi/ludusavi.exe Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,7 @@
import { gameAchievementRepository, gameRepository } from "@main/repository"; import { gameAchievementRepository, gameRepository } from "@main/repository";
import { publishNewAchievementNotification } from "../notifications"; import { publishNewAchievementNotification } from "../notifications";
import type { GameShop, UnlockedAchievement } from "@types"; import type { GameShop, UnlockedAchievement } from "@types";
import { WindowManager } from "../window-manager";
export const mergeAchievements = async ( export const mergeAchievements = async (
objectId: string, objectId: string,
@ -28,6 +29,14 @@ export const mergeAchievements = async (
}); });
}); });
if (!newAchievements.length) return;
WindowManager.mainWindow?.webContents.send(
"on-achievement-unlocked",
objectId,
shop
);
for (const achievement of newAchievements) { for (const achievement of newAchievements) {
const completeAchievement = JSON.parse( const completeAchievement = JSON.parse(
localGameAchievement?.achievements || "[]" localGameAchievement?.achievements || "[]"

View File

@ -51,6 +51,16 @@ contextBridge.exposeInMainWorld("electron", {
getTrendingGames: () => ipcRenderer.invoke("getTrendingGames"), getTrendingGames: () => ipcRenderer.invoke("getTrendingGames"),
getGameAchievements: (objectId: string, shop: GameShop) => getGameAchievements: (objectId: string, shop: GameShop) =>
ipcRenderer.invoke("getGameAchievements", objectId, shop), ipcRenderer.invoke("getGameAchievements", objectId, shop),
onAchievementUnlocked: (cb: (objectId: string, shop: GameShop) => void) => {
const listener = (
_event: Electron.IpcRendererEvent,
objectId: string,
shop: GameShop
) => cb(objectId, shop);
ipcRenderer.on("on-achievement-unlocked", listener);
return () =>
ipcRenderer.removeListener("on-achievement-unlocked", listener);
},
/* User preferences */ /* User preferences */
getUserPreferences: () => ipcRenderer.invoke("getUserPreferences"), getUserPreferences: () => ipcRenderer.invoke("getUserPreferences"),

View File

@ -138,6 +138,7 @@ export function GameDetailsContextProvider({
setGame(null); setGame(null);
setIsLoading(true); setIsLoading(true);
setisGameRunning(false); setisGameRunning(false);
setAchievements([]);
dispatch(setHeaderTitle(gameTitle)); dispatch(setHeaderTitle(gameTitle));
}, [objectID, gameTitle, dispatch]); }, [objectID, gameTitle, dispatch]);
@ -158,6 +159,22 @@ export function GameDetailsContextProvider({
}; };
}, [game?.id, isGameRunning, updateGame]); }, [game?.id, isGameRunning, updateGame]);
useEffect(() => {
const unsubscribe = window.electron.onAchievementUnlocked(
(objectId, shop) => {
if (objectID !== objectId || shop !== shop) return;
window.electron
.getGameAchievements(objectID!, shop as GameShop)
.then(setAchievements);
}
);
return () => {
unsubscribe();
};
}, [objectID, shop]);
const getDownloadsPath = async () => { const getDownloadsPath = async () => {
if (userPreferences?.downloadsPath) return userPreferences.downloadsPath; if (userPreferences?.downloadsPath) return userPreferences.downloadsPath;
return window.electron.getDefaultDownloadsPath(); return window.electron.getDefaultDownloadsPath();

View File

@ -70,6 +70,9 @@ declare global {
objectId: string, objectId: string,
shop: GameShop shop: GameShop
) => Promise<GameAchievement[]>; ) => Promise<GameAchievement[]>;
onAchievementUnlocked: (
cb: (objectId: string, shop: GameShop) => void
) => () => Electron.IpcRenderer;
/* Library */ /* Library */
addGameToLibrary: ( addGameToLibrary: (

View File

@ -48,7 +48,7 @@ export function Sidebar() {
isLoading={howLongToBeat.isLoading} isLoading={howLongToBeat.isLoading}
/> */} /> */}
{achievements.length && ( {achievements.length > 0 && (
<div <div
style={{ style={{
display: "flex", display: "flex",
@ -71,11 +71,14 @@ export function Sidebar() {
<img <img
style={{ style={{
height: "72px", height: "72px",
width: "72px",
filter: achievement.unlocked ? "none" : "grayscale(100%)",
}} }}
src={ src={
achievement.unlocked ? achievement.icon : achievement.icongray achievement.unlocked ? achievement.icon : achievement.icongray
} }
alt={achievement.displayName} alt={achievement.displayName}
loading="lazy"
/> />
<div> <div>
<p>{achievement.displayName}</p> <p>{achievement.displayName}</p>