feat: create use details functions

This commit is contained in:
Zamitto 2024-09-16 13:50:58 -03:00
parent 09d0e5b4ef
commit ec16efed2f
9 changed files with 53 additions and 16 deletions

View File

@ -59,6 +59,7 @@ import "./profile/update-friend-request";
import "./profile/update-profile";
import "./profile/process-profile-image";
import "./profile/send-friend-request";
import "./profile/sync-friend-requests";
import { isPortableVersion } from "@main/helpers";
ipcMain.handle("ping", () => "pong");

View File

@ -0,0 +1,9 @@
import { registerEvent } from "../register-event";
import { HydraApi } from "@main/services";
import { FriendRequestSync } from "@types";
const syncFriendRequests = async (_event: Electron.IpcMainInvokeEvent) => {
return HydraApi.get<FriendRequestSync>(`/profile/friend-requests/sync`);
};
registerEvent("syncFriendRequests", syncFriendRequests);

View File

@ -150,6 +150,7 @@ contextBridge.exposeInMainWorld("electron", {
processProfileImage: (imagePath: string) =>
ipcRenderer.invoke("processProfileImage", imagePath),
getFriendRequests: () => ipcRenderer.invoke("getFriendRequests"),
syncFriendRequests: () => ipcRenderer.invoke("syncFriendRequests"),
updateFriendRequest: (userId: string, action: FriendRequestAction) =>
ipcRenderer.invoke("updateFriendRequest", userId, action),
sendFriendRequest: (userId: string) =>

View File

@ -43,7 +43,7 @@ export function App() {
isFriendsModalVisible,
friendRequetsModalTab,
friendModalUserId,
fetchFriendRequests,
syncFriendRequests,
hideFriendsModal,
} = useUserDetails();
@ -105,22 +105,22 @@ export function App() {
fetchUserDetails().then((response) => {
if (response) {
updateUserDetails(response);
fetchFriendRequests();
syncFriendRequests();
}
});
}, [fetchUserDetails, fetchFriendRequests, updateUserDetails, dispatch]);
}, [fetchUserDetails, syncFriendRequests, updateUserDetails, dispatch]);
const onSignIn = useCallback(() => {
fetchUserDetails().then((response) => {
if (response) {
updateUserDetails(response);
fetchFriendRequests();
syncFriendRequests();
showSuccessToast(t("successfully_signed_in"));
}
});
}, [
fetchUserDetails,
fetchFriendRequests,
syncFriendRequests,
t,
showSuccessToast,
updateUserDetails,

View File

@ -15,15 +15,15 @@ export function SidebarProfile() {
const { t } = useTranslation("sidebar");
const { userDetails, friendRequests, showFriendsModal, fetchFriendRequests } =
useUserDetails();
const {
userDetails,
FriendRequestCount,
showFriendsModal,
syncFriendRequests,
} = useUserDetails();
const { gameRunning } = useAppSelector((state) => state.gameRunning);
const receivedRequests = useMemo(() => {
return friendRequests.filter((request) => request.type === "RECEIVED");
}, [friendRequests]);
const handleProfileClick = () => {
if (userDetails === null) {
window.electron.openAuthWindow();
@ -35,7 +35,7 @@ export function SidebarProfile() {
useEffect(() => {
pollingInterval.current = setInterval(() => {
fetchFriendRequests();
syncFriendRequests();
}, LONG_POLLING_INTERVAL);
return () => {
@ -43,7 +43,7 @@ export function SidebarProfile() {
clearInterval(pollingInterval.current);
}
};
}, [fetchFriendRequests]);
}, [syncFriendRequests]);
const friendsButton = useMemo(() => {
if (!userDetails) return null;
@ -57,16 +57,16 @@ export function SidebarProfile() {
}
title={t("friends")}
>
{receivedRequests.length > 0 && (
{FriendRequestCount > 0 && (
<small className={styles.friendsButtonBadge}>
{receivedRequests.length > 99 ? "99+" : receivedRequests.length}
{FriendRequestCount > 99 ? "99+" : FriendRequestCount}
</small>
)}
<PeopleIcon size={16} />
</button>
);
}, [userDetails, t, receivedRequests, showFriendsModal]);
}, [userDetails, t, FriendRequestCount, showFriendsModal]);
return (
<div className={styles.profileContainer}>

View File

@ -24,6 +24,7 @@ import type {
TrendingGame,
UserStats,
UserDetails,
FriendRequestSync,
} from "@types";
import type { DiskSpace } from "check-disk-space";
@ -164,6 +165,7 @@ declare global {
path: string
) => Promise<{ imagePath: string; mimeType: string }>;
getFriendRequests: () => Promise<FriendRequest[]>;
syncFriendRequests: () => Promise<FriendRequestSync>;
updateFriendRequest: (
userId: string,
action: FriendRequestAction

View File

@ -6,6 +6,7 @@ export interface UserDetailsState {
userDetails: UserDetails | null;
profileBackground: null | string;
friendRequests: FriendRequest[];
FriendRequestCount: number;
isFriendsModalVisible: boolean;
friendRequetsModalTab: UserFriendModalTab | null;
friendModalUserId: string;
@ -15,6 +16,7 @@ const initialState: UserDetailsState = {
userDetails: null,
profileBackground: null,
friendRequests: [],
FriendRequestCount: 0,
isFriendsModalVisible: false,
friendRequetsModalTab: null,
friendModalUserId: "",
@ -33,6 +35,9 @@ export const userDetailsSlice = createSlice({
setFriendRequests: (state, action: PayloadAction<FriendRequest[]>) => {
state.friendRequests = action.payload;
},
setFriendRequestCount: (state, action: PayloadAction<number>) => {
state.FriendRequestCount = action.payload;
},
setFriendsModalVisible: (
state,
action: PayloadAction<{ initialTab: UserFriendModalTab; userId: string }>
@ -52,6 +57,7 @@ export const {
setUserDetails,
setProfileBackground,
setFriendRequests,
setFriendRequestCount,
setFriendsModalVisible,
setFriendsModalHidden,
} = userDetailsSlice.actions;

View File

@ -6,6 +6,7 @@ import {
setFriendRequests,
setFriendsModalVisible,
setFriendsModalHidden,
setFriendRequestCount,
} from "@renderer/features";
import type {
FriendRequestAction,
@ -21,6 +22,7 @@ export function useUserDetails() {
userDetails,
profileBackground,
friendRequests,
FriendRequestCount,
isFriendsModalVisible,
friendModalUserId,
friendRequetsModalTab,
@ -95,11 +97,21 @@ export function useUserDetails() {
return window.electron
.getFriendRequests()
.then((friendRequests) => {
syncFriendRequests();
dispatch(setFriendRequests(friendRequests));
})
.catch(() => {});
}, [dispatch]);
const syncFriendRequests = useCallback(async () => {
return window.electron
.syncFriendRequests()
.then((sync) => {
dispatch(setFriendRequestCount(sync.friendRequestCount));
})
.catch(() => {});
}, [dispatch]);
const showFriendsModal = useCallback(
(initialTab: UserFriendModalTab, userId: string) => {
dispatch(setFriendsModalVisible({ initialTab, userId }));
@ -143,6 +155,7 @@ export function useUserDetails() {
userDetails,
profileBackground,
friendRequests,
FriendRequestCount,
friendRequetsModalTab,
isFriendsModalVisible,
friendModalUserId,
@ -155,6 +168,7 @@ export function useUserDetails() {
patchUser,
sendFriendRequest,
fetchFriendRequests,
syncFriendRequests,
updateFriendRequestState,
blockUser,
unblockUser,

View File

@ -170,6 +170,10 @@ export interface UserBlocks {
blocks: UserFriend[];
}
export interface FriendRequestSync {
friendRequestCount: number;
}
export interface FriendRequest {
id: string;
displayName: string;