From 1de3a9836c188944293ca416eb7125c40379ff0e Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Mon, 16 Sep 2024 11:22:26 -0300 Subject: [PATCH] feat: update typing to match get me endpoint --- src/main/events/profile/get-me.ts | 47 +++++-------------- src/renderer/src/declaration.d.ts | 3 +- .../src/features/user-details-slice.ts | 6 +-- src/renderer/src/hooks/use-user-details.ts | 9 ++-- .../pages/game-details/sidebar/sidebar.tsx | 33 +++++++------ src/types/index.ts | 17 +++++-- 6 files changed, 53 insertions(+), 62 deletions(-) diff --git a/src/main/events/profile/get-me.ts b/src/main/events/profile/get-me.ts index 5154da8d..1eeecbf9 100644 --- a/src/main/events/profile/get-me.ts +++ b/src/main/events/profile/get-me.ts @@ -1,32 +1,14 @@ import { registerEvent } from "../register-event"; import * as Sentry from "@sentry/electron/main"; -import { HydraApi, logger } from "@main/services"; -import { UserProfile } from "@types"; +import { HydraApi } from "@main/services"; +import { ProfileVisibility, UserDetails } from "@types"; import { userAuthRepository } from "@main/repository"; -import { steamUrlBuilder, UserNotLoggedInError } from "@shared"; -import { steamGamesWorker } from "@main/workers"; - -const getSteamGame = async (objectId: string) => { - try { - const steamGame = await steamGamesWorker.run(Number(objectId), { - name: "getById", - }); - - return { - title: steamGame.name, - iconUrl: steamUrlBuilder.icon(objectId, steamGame.clientIcon), - }; - } catch (err) { - logger.error("Failed to get Steam game", err); - - return null; - } -}; +import { UserNotLoggedInError } from "@shared"; const getMe = async ( _event: Electron.IpcMainInvokeEvent -): Promise => { - return HydraApi.get(`/profile/me`) +): Promise => { + return HydraApi.get(`/profile/me`) .then(async (me) => { userAuthRepository.upsert( { @@ -38,17 +20,6 @@ const getMe = async ( ["id"] ); - if (me.currentGame) { - const steamGame = await getSteamGame(me.currentGame.objectId); - - if (steamGame) { - me.currentGame = { - ...me.currentGame, - ...steamGame, - }; - } - } - Sentry.setUser({ id: me.id, username: me.username }); return me; @@ -61,7 +32,13 @@ const getMe = async ( const loggedUser = await userAuthRepository.findOne({ where: { id: 1 } }); if (loggedUser) { - return { ...loggedUser, id: loggedUser.userId }; + return { + ...loggedUser, + id: loggedUser.userId, + username: "", + bio: "", + profileVisibility: "PUBLIC" as ProfileVisibility, + }; } return null; diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 8e341ed8..26169d67 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -23,6 +23,7 @@ import type { GameStats, TrendingGame, UserStats, + UserDetails, } from "@types"; import type { DiskSpace } from "check-disk-space"; @@ -153,7 +154,7 @@ declare global { ) => Promise; /* Profile */ - getMe: () => Promise; + getMe: () => Promise; undoFriendship: (userId: string) => Promise; updateProfile: ( updateProfile: UpdateProfileRequest diff --git a/src/renderer/src/features/user-details-slice.ts b/src/renderer/src/features/user-details-slice.ts index 00542020..d559de09 100644 --- a/src/renderer/src/features/user-details-slice.ts +++ b/src/renderer/src/features/user-details-slice.ts @@ -1,9 +1,9 @@ import { PayloadAction, createSlice } from "@reduxjs/toolkit"; import { UserFriendModalTab } from "@renderer/pages/shared-modals/user-friend-modal"; -import type { FriendRequest, UserProfile } from "@types"; +import type { FriendRequest, UserDetails } from "@types"; export interface UserDetailsState { - userDetails: UserProfile | null; + userDetails: UserDetails | null; profileBackground: null | string; friendRequests: FriendRequest[]; isFriendsModalVisible: boolean; @@ -24,7 +24,7 @@ export const userDetailsSlice = createSlice({ name: "user-details", initialState, reducers: { - setUserDetails: (state, action: PayloadAction) => { + setUserDetails: (state, action: PayloadAction) => { state.userDetails = action.payload; }, setProfileBackground: (state, action: PayloadAction) => { diff --git a/src/renderer/src/hooks/use-user-details.ts b/src/renderer/src/hooks/use-user-details.ts index a826b008..0ae196dc 100644 --- a/src/renderer/src/hooks/use-user-details.ts +++ b/src/renderer/src/hooks/use-user-details.ts @@ -10,7 +10,7 @@ import { import type { FriendRequestAction, UpdateProfileRequest, - UserProfile, + UserDetails, } from "@types"; import { UserFriendModalTab } from "@renderer/pages/shared-modals/user-friend-modal"; @@ -40,7 +40,7 @@ export function useUserDetails() { }, [clearUserDetails]); const updateUserDetails = useCallback( - async (userDetails: UserProfile) => { + async (userDetails: UserDetails) => { dispatch(setUserDetails(userDetails)); if (userDetails.profileImageUrl) { @@ -83,7 +83,10 @@ export function useUserDetails() { const patchUser = useCallback( async (values: UpdateProfileRequest) => { const response = await window.electron.updateProfile(values); - return updateUserDetails(response); + return updateUserDetails({ + ...response, + username: userDetails?.username || "", + }); }, [updateUserDetails] ); diff --git a/src/renderer/src/pages/game-details/sidebar/sidebar.tsx b/src/renderer/src/pages/game-details/sidebar/sidebar.tsx index 1922403c..e56f0764 100644 --- a/src/renderer/src/pages/game-details/sidebar/sidebar.tsx +++ b/src/renderer/src/pages/game-details/sidebar/sidebar.tsx @@ -1,4 +1,4 @@ -import { useContext, useEffect, useState } from "react"; +import { useContext, useState } from "react"; import type { HowLongToBeatCategory, SteamAppDetails } from "@types"; import { useTranslation } from "react-i18next"; import { Button } from "@renderer/components"; @@ -9,7 +9,7 @@ import { useFormat } from "@renderer/hooks"; import { DownloadIcon, PeopleIcon } from "@primer/octicons-react"; export function Sidebar() { - const [_howLongToBeat, setHowLongToBeat] = useState<{ + const [_howLongToBeat, _setHowLongToBeat] = useState<{ isLoading: boolean; data: HowLongToBeatCategory[] | null; }>({ isLoading: true, data: null }); @@ -17,27 +17,26 @@ export function Sidebar() { const [activeRequirement, setActiveRequirement] = useState("minimum"); - const { gameTitle, shopDetails, objectID, stats } = - useContext(gameDetailsContext); + const { gameTitle, shopDetails, stats } = useContext(gameDetailsContext); const { t } = useTranslation("game_details"); const { numberFormatter } = useFormat(); - useEffect(() => { - if (objectID) { - setHowLongToBeat({ isLoading: true, data: null }); + // useEffect(() => { + // if (objectID) { + // setHowLongToBeat({ isLoading: true, data: null }); - window.electron - .getHowLongToBeat(objectID, "steam", gameTitle) - .then((howLongToBeat) => { - setHowLongToBeat({ isLoading: false, data: howLongToBeat }); - }) - .catch(() => { - setHowLongToBeat({ isLoading: false, data: null }); - }); - } - }, [objectID, gameTitle]); + // window.electron + // .getHowLongToBeat(objectID, "steam", gameTitle) + // .then((howLongToBeat) => { + // setHowLongToBeat({ isLoading: false, data: howLongToBeat }); + // }) + // .catch(() => { + // setHowLongToBeat({ isLoading: false, data: null }); + // }); + // } + // }, [objectID, gameTitle]); return (