mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-03 00:33:49 +03:00
feat: code review
This commit is contained in:
parent
10d7e0e8aa
commit
d60242a62c
@ -50,7 +50,13 @@ const getUser = async (
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
return { ...profile, libraryGames, recentGames, friends };
|
return {
|
||||||
|
...profile,
|
||||||
|
libraryGames,
|
||||||
|
recentGames,
|
||||||
|
friends: friends.friends,
|
||||||
|
totalFriends: friends.totalFriends,
|
||||||
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { UserGame, UserProfile, UserRelation } from "@types";
|
import { FriendRequestAction, UserGame, UserProfile } from "@types";
|
||||||
import cn from "classnames";
|
import cn from "classnames";
|
||||||
import * as styles from "./user.css";
|
import * as styles from "./user.css";
|
||||||
import { SPACING_UNIT, vars } from "@renderer/theme.css";
|
import { SPACING_UNIT, vars } from "@renderer/theme.css";
|
||||||
@ -26,7 +26,7 @@ import {
|
|||||||
} from "@primer/octicons-react";
|
} from "@primer/octicons-react";
|
||||||
import { Button, Link } from "@renderer/components";
|
import { Button, Link } from "@renderer/components";
|
||||||
import { UserEditProfileModal } from "./user-edit-modal";
|
import { UserEditProfileModal } from "./user-edit-modal";
|
||||||
import { UserSignOutModal } from "./user-signout-modal";
|
import { UserSignOutModal } from "./user-sign-out-modal";
|
||||||
import { UserFriendModalTab } from "../shared-modals/user-friend-modal";
|
import { UserFriendModalTab } from "../shared-modals/user-friend-modal";
|
||||||
import { UserBlockModal } from "./user-block-modal";
|
import { UserBlockModal } from "./user-block-modal";
|
||||||
|
|
||||||
@ -37,6 +37,8 @@ export interface ProfileContentProps {
|
|||||||
updateUserProfile: () => Promise<void>;
|
updateUserProfile: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FriendAction = FriendRequestAction | ("BLOCK" | "UNDO" | "SEND");
|
||||||
|
|
||||||
export function UserContent({
|
export function UserContent({
|
||||||
userProfile,
|
userProfile,
|
||||||
updateUserProfile,
|
updateUserProfile,
|
||||||
@ -128,62 +130,35 @@ export function UserContent({
|
|||||||
}
|
}
|
||||||
}, [profileBackground, isMe]);
|
}, [profileBackground, isMe]);
|
||||||
|
|
||||||
const handleUndoFriendship = (userRelation: UserRelation) => {
|
const handleFriendAction = (userId: string, action: FriendAction) => {
|
||||||
const userId =
|
try {
|
||||||
userRelation.AId === userDetails?.id
|
if (action === "UNDO") {
|
||||||
? userRelation.BId
|
undoFriendship(userId).then(updateUserProfile);
|
||||||
: userRelation.AId;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
undoFriendship(userId)
|
if (action === "BLOCK") {
|
||||||
.then(updateUserProfile)
|
blockUser(userId).then(() => {
|
||||||
.catch(() => {
|
setShowUserBlockModal(false);
|
||||||
showErrorToast(t("try_again"));
|
showSuccessToast(t("user_blocked_successfully"));
|
||||||
});
|
navigate(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action === "SEND") {
|
||||||
|
sendFriendRequest(userProfile.id).then(updateUserProfile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateFriendRequestState(userId, action).then(updateUserProfile);
|
||||||
|
} catch (err) {
|
||||||
|
showErrorToast(t("try_again"));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSendFriendRequest = () => {
|
const showFriends = isMe || userProfile.totalFriends > 0;
|
||||||
sendFriendRequest(userProfile.id)
|
|
||||||
.then(updateUserProfile)
|
|
||||||
.catch(() => {
|
|
||||||
showErrorToast(t("try_again"));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleBlockUser = () => {
|
|
||||||
blockUser(userProfile.id)
|
|
||||||
.then(() => {
|
|
||||||
setShowUserBlockModal(false);
|
|
||||||
showSuccessToast(t("user_blocked_successfully"));
|
|
||||||
navigate(-1);
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
showErrorToast(t("try_again"));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleCancelFriendRequest = (userId: string) => {
|
|
||||||
updateFriendRequestState(userId, "CANCEL")
|
|
||||||
.then(updateUserProfile)
|
|
||||||
.catch(() => {
|
|
||||||
showErrorToast(t("try_again"));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleAcceptFriendRequest = (userId: string) => {
|
|
||||||
updateFriendRequestState(userId, "ACCEPTED")
|
|
||||||
.then(updateUserProfile)
|
|
||||||
.catch(() => {
|
|
||||||
showErrorToast(t("try_again"));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleRefuseFriendRequest = (userId: string) => {
|
|
||||||
updateFriendRequestState(userId, "REFUSED")
|
|
||||||
.then(updateUserProfile)
|
|
||||||
.catch(() => {
|
|
||||||
showErrorToast(t("try_again"));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getProfileActions = () => {
|
const getProfileActions = () => {
|
||||||
if (isMe) {
|
if (isMe) {
|
||||||
@ -203,7 +178,10 @@ export function UserContent({
|
|||||||
if (userProfile.relation == null) {
|
if (userProfile.relation == null) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Button theme="outline" onClick={handleSendFriendRequest}>
|
<Button
|
||||||
|
theme="outline"
|
||||||
|
onClick={() => handleFriendAction(userProfile.id, "SEND")}
|
||||||
|
>
|
||||||
{t("add_friend")}
|
{t("add_friend")}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
@ -215,12 +193,17 @@ export function UserContent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (userProfile.relation.status === "ACCEPTED") {
|
if (userProfile.relation.status === "ACCEPTED") {
|
||||||
|
const userId =
|
||||||
|
userProfile.relation.AId === userDetails?.id
|
||||||
|
? userProfile.relation.BId
|
||||||
|
: userProfile.relation.AId;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Button
|
<Button
|
||||||
theme="outline"
|
theme="outline"
|
||||||
className={styles.cancelRequestButton}
|
className={styles.cancelRequestButton}
|
||||||
onClick={() => handleUndoFriendship(userProfile.relation!)}
|
onClick={() => handleFriendAction(userId, "UNDO")}
|
||||||
>
|
>
|
||||||
<XCircleIcon size={28} /> {t("undo_friendship")}
|
<XCircleIcon size={28} /> {t("undo_friendship")}
|
||||||
</Button>
|
</Button>
|
||||||
@ -233,7 +216,9 @@ export function UserContent({
|
|||||||
<Button
|
<Button
|
||||||
theme="outline"
|
theme="outline"
|
||||||
className={styles.cancelRequestButton}
|
className={styles.cancelRequestButton}
|
||||||
onClick={() => handleCancelFriendRequest(userProfile.relation!.BId)}
|
onClick={() =>
|
||||||
|
handleFriendAction(userProfile.relation!.BId, "CANCEL")
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<XCircleIcon size={28} /> {t("cancel_request")}
|
<XCircleIcon size={28} /> {t("cancel_request")}
|
||||||
</Button>
|
</Button>
|
||||||
@ -245,14 +230,18 @@ export function UserContent({
|
|||||||
<Button
|
<Button
|
||||||
theme="outline"
|
theme="outline"
|
||||||
className={styles.acceptRequestButton}
|
className={styles.acceptRequestButton}
|
||||||
onClick={() => handleAcceptFriendRequest(userProfile.relation!.AId)}
|
onClick={() =>
|
||||||
|
handleFriendAction(userProfile.relation!.AId, "ACCEPTED")
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<CheckCircleIcon size={28} /> {t("accept_request")}
|
<CheckCircleIcon size={28} /> {t("accept_request")}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
theme="outline"
|
theme="outline"
|
||||||
className={styles.cancelRequestButton}
|
className={styles.cancelRequestButton}
|
||||||
onClick={() => handleRefuseFriendRequest(userProfile.relation!.AId)}
|
onClick={() =>
|
||||||
|
handleFriendAction(userProfile.relation!.AId, "REFUSED")
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<XCircleIcon size={28} /> {t("ignore_request")}
|
<XCircleIcon size={28} /> {t("ignore_request")}
|
||||||
</Button>
|
</Button>
|
||||||
@ -278,7 +267,7 @@ export function UserContent({
|
|||||||
<UserBlockModal
|
<UserBlockModal
|
||||||
visible={showUserBlockModal}
|
visible={showUserBlockModal}
|
||||||
onClose={() => setShowUserBlockModal(false)}
|
onClose={() => setShowUserBlockModal(false)}
|
||||||
onConfirm={handleBlockUser}
|
onConfirm={() => handleFriendAction(userProfile.id, "BLOCK")}
|
||||||
displayName={userProfile.displayName}
|
displayName={userProfile.displayName}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@ -479,8 +468,7 @@ export function UserContent({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{(isMe ||
|
{showFriends && (
|
||||||
(userProfile.friends && userProfile.friends.totalFriends > 0)) && (
|
|
||||||
<div className={styles.friendsSection}>
|
<div className={styles.friendsSection}>
|
||||||
<button
|
<button
|
||||||
className={styles.friendsSectionHeader}
|
className={styles.friendsSectionHeader}
|
||||||
@ -501,7 +489,7 @@ export function UserContent({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<h3 style={{ fontWeight: "400" }}>
|
<h3 style={{ fontWeight: "400" }}>
|
||||||
{userProfile.friends.totalFriends}
|
{userProfile.totalFriends}
|
||||||
</h3>
|
</h3>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
@ -512,7 +500,7 @@ export function UserContent({
|
|||||||
gap: `${SPACING_UNIT}px`,
|
gap: `${SPACING_UNIT}px`,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{userProfile.friends.friends.map((friend) => {
|
{userProfile.friends.map((friend) => {
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
key={friend.id}
|
key={friend.id}
|
||||||
|
@ -305,7 +305,8 @@ export interface UserProfile {
|
|||||||
totalPlayTimeInSeconds: number;
|
totalPlayTimeInSeconds: number;
|
||||||
libraryGames: UserGame[];
|
libraryGames: UserGame[];
|
||||||
recentGames: UserGame[];
|
recentGames: UserGame[];
|
||||||
friends: UserFriends;
|
friends: UserFriend[];
|
||||||
|
totalFriends: number;
|
||||||
relation: UserRelation | null;
|
relation: UserRelation | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user