feat: loading from me endpoint and updating sidebar profile

This commit is contained in:
Zamitto
2024-06-15 19:52:37 -03:00
parent aeaeb1f086
commit 76259c2b54
12 changed files with 178 additions and 59 deletions

View File

@@ -12,20 +12,3 @@ export const downloadSourceSchema = z.object({
})
),
});
const gamesArray = z.array(
z.object({
id: z.string().length(8),
objectId: z.string().max(255),
playTimeInSeconds: z.number().int(),
shop: z.enum(["steam", "epic"]),
lastTimePlayed: z.coerce.date().nullable(),
})
);
export const userProfileSchema = z.object({
displayName: z.string(),
profileImageUrl: z.string().url().nullable(),
libraryGames: gamesArray,
recentGames: gamesArray,
});

View File

@@ -1,5 +1,6 @@
import { defaultDownloadsPath } from "@main/constants";
import { app, ipcMain } from "electron";
import { HydraApi } from "@main/services/hydra-api";
import "./catalogue/get-catalogue";
import "./catalogue/get-game-shop-details";
@@ -40,7 +41,9 @@ import "./download-sources/add-download-source";
import "./download-sources/remove-download-source";
import "./download-sources/sync-download-sources";
import "./profile/get-user-profile";
import "./profile/get-me";
ipcMain.handle("ping", () => "pong");
ipcMain.handle("getVersion", () => app.getVersion());
ipcMain.handle("isUserLoggedIn", () => HydraApi.isLoggedIn());
ipcMain.handle("getDefaultDownloadsPath", () => defaultDownloadsPath);

View File

@@ -0,0 +1,30 @@
import { registerEvent } from "../register-event";
import { HydraApi } from "@main/services/hydra-api";
import { UserProfile } from "@types";
import { userAuthRepository } from "@main/repository";
const getMe = async (
_event: Electron.IpcMainInvokeEvent
): Promise<UserProfile | null> => {
return HydraApi.get(`/profile/me`)
.then((response) => {
const me = response.data;
userAuthRepository.upsert(
{
id: 1,
displayName: me.displayName,
profileImageUrl: me.displayName,
userId: me.id,
},
["id"]
);
return me;
})
.catch(() => {
return userAuthRepository.findOne({ where: { id: 1 } });
});
};
registerEvent("getMe", getMe);

View File

@@ -1,6 +1,4 @@
import { registerEvent } from "../register-event";
import { userProfileSchema } from "../helpers/validators";
import { logger } from "@main/services";
import { HydraApi } from "@main/services/hydra-api";
import { steamGamesWorker } from "@main/workers";
import { UserProfile } from "@types";
@@ -13,7 +11,7 @@ const getUserProfile = async (
): Promise<UserProfile | null> => {
try {
const response = await HydraApi.get(`/user/${username}`);
const profile = userProfileSchema.parse(response.data);
const profile = response.data;
const recentGames = await Promise.all(
profile.recentGames.map(async (game) => {
@@ -51,7 +49,6 @@ const getUserProfile = async (
return { ...profile, libraryGames, recentGames };
} catch (err) {
logger.error(`getUserProfile: ${username}`, err);
return null;
}
};