mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-01-24 05:54:55 +03:00
feat: pass userId to modal
This commit is contained in:
parent
010f07373d
commit
a196b91cb9
@ -145,7 +145,8 @@ contextBridge.exposeInMainWorld("electron", {
|
|||||||
|
|
||||||
/* User */
|
/* User */
|
||||||
getUser: (userId: string) => ipcRenderer.invoke("getUser", userId),
|
getUser: (userId: string) => ipcRenderer.invoke("getUser", userId),
|
||||||
getUserFriends: (userId: string) => ipcRenderer.invoke("getUserFriends", userId),
|
getUserFriends: (userId: string, take: number, skip: number) =>
|
||||||
|
ipcRenderer.invoke("getUserFriends", userId, take, skip),
|
||||||
|
|
||||||
/* Auth */
|
/* Auth */
|
||||||
signOut: () => ipcRenderer.invoke("signOut"),
|
signOut: () => ipcRenderer.invoke("signOut"),
|
||||||
|
@ -42,11 +42,12 @@ export function App() {
|
|||||||
const {
|
const {
|
||||||
isFriendsModalVisible,
|
isFriendsModalVisible,
|
||||||
friendRequetsModalTab,
|
friendRequetsModalTab,
|
||||||
|
friendModalUserId,
|
||||||
fetchFriendRequests,
|
fetchFriendRequests,
|
||||||
hideFriendsModal,
|
hideFriendsModal,
|
||||||
} = useUserDetails();
|
} = useUserDetails();
|
||||||
|
|
||||||
const { fetchUserDetails, updateUserDetails, clearUserDetails } =
|
const { userDetails, fetchUserDetails, updateUserDetails, clearUserDetails } =
|
||||||
useUserDetails();
|
useUserDetails();
|
||||||
|
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
@ -218,11 +219,14 @@ export function App() {
|
|||||||
onClose={handleToastClose}
|
onClose={handleToastClose}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<UserFriendModal
|
{userDetails && (
|
||||||
visible={isFriendsModalVisible}
|
<UserFriendModal
|
||||||
initialTab={friendRequetsModalTab}
|
visible={isFriendsModalVisible}
|
||||||
onClose={hideFriendsModal}
|
initialTab={friendRequetsModalTab}
|
||||||
/>
|
onClose={hideFriendsModal}
|
||||||
|
userId={friendModalUserId}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<Sidebar />
|
<Sidebar />
|
||||||
|
@ -84,7 +84,9 @@ export function SidebarProfile() {
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className={styles.friendRequestButton}
|
className={styles.friendRequestButton}
|
||||||
onClick={() => showFriendsModal(UserFriendModalTab.AddFriend)}
|
onClick={() =>
|
||||||
|
showFriendsModal(UserFriendModalTab.AddFriend, userDetails.id)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<PersonAddIcon size={24} />
|
<PersonAddIcon size={24} />
|
||||||
{friendRequests.length}
|
{friendRequests.length}
|
||||||
|
6
src/renderer/src/declaration.d.ts
vendored
6
src/renderer/src/declaration.d.ts
vendored
@ -128,7 +128,11 @@ declare global {
|
|||||||
|
|
||||||
/* User */
|
/* User */
|
||||||
getUser: (userId: string) => Promise<UserProfile | null>;
|
getUser: (userId: string) => Promise<UserProfile | null>;
|
||||||
getUserFriends: (userId: string) => Promise<UserFriends>;
|
getUserFriends: (
|
||||||
|
userId: string,
|
||||||
|
take: number,
|
||||||
|
skip: number
|
||||||
|
) => Promise<UserFriends>;
|
||||||
|
|
||||||
/* Profile */
|
/* Profile */
|
||||||
getMe: () => Promise<UserProfile | null>;
|
getMe: () => Promise<UserProfile | null>;
|
||||||
|
@ -8,6 +8,7 @@ export interface UserDetailsState {
|
|||||||
friendRequests: FriendRequest[];
|
friendRequests: FriendRequest[];
|
||||||
isFriendsModalVisible: boolean;
|
isFriendsModalVisible: boolean;
|
||||||
friendRequetsModalTab: UserFriendModalTab | null;
|
friendRequetsModalTab: UserFriendModalTab | null;
|
||||||
|
friendModalUserId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState: UserDetailsState = {
|
const initialState: UserDetailsState = {
|
||||||
@ -16,6 +17,7 @@ const initialState: UserDetailsState = {
|
|||||||
friendRequests: [],
|
friendRequests: [],
|
||||||
isFriendsModalVisible: false,
|
isFriendsModalVisible: false,
|
||||||
friendRequetsModalTab: null,
|
friendRequetsModalTab: null,
|
||||||
|
friendModalUserId: "",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const userDetailsSlice = createSlice({
|
export const userDetailsSlice = createSlice({
|
||||||
@ -33,10 +35,11 @@ export const userDetailsSlice = createSlice({
|
|||||||
},
|
},
|
||||||
setFriendsModalVisible: (
|
setFriendsModalVisible: (
|
||||||
state,
|
state,
|
||||||
action: PayloadAction<UserFriendModalTab>
|
action: PayloadAction<{ initialTab: UserFriendModalTab; userId: string }>
|
||||||
) => {
|
) => {
|
||||||
state.isFriendsModalVisible = true;
|
state.isFriendsModalVisible = true;
|
||||||
state.friendRequetsModalTab = action.payload;
|
state.friendRequetsModalTab = action.payload.initialTab;
|
||||||
|
state.friendModalUserId = action.payload.userId;
|
||||||
},
|
},
|
||||||
setFriendsModalHidden: (state) => {
|
setFriendsModalHidden: (state) => {
|
||||||
state.isFriendsModalVisible = false;
|
state.isFriendsModalVisible = false;
|
||||||
|
@ -19,6 +19,7 @@ export function useUserDetails() {
|
|||||||
profileBackground,
|
profileBackground,
|
||||||
friendRequests,
|
friendRequests,
|
||||||
isFriendsModalVisible,
|
isFriendsModalVisible,
|
||||||
|
friendModalUserId,
|
||||||
friendRequetsModalTab,
|
friendRequetsModalTab,
|
||||||
} = useAppSelector((state) => state.userDetails);
|
} = useAppSelector((state) => state.userDetails);
|
||||||
|
|
||||||
@ -90,8 +91,8 @@ export function useUserDetails() {
|
|||||||
}, [dispatch]);
|
}, [dispatch]);
|
||||||
|
|
||||||
const showFriendsModal = useCallback(
|
const showFriendsModal = useCallback(
|
||||||
(tab: UserFriendModalTab) => {
|
(initialTab: UserFriendModalTab, userId: string) => {
|
||||||
dispatch(setFriendsModalVisible(tab));
|
dispatch(setFriendsModalVisible({ initialTab, userId }));
|
||||||
fetchFriendRequests();
|
fetchFriendRequests();
|
||||||
},
|
},
|
||||||
[dispatch]
|
[dispatch]
|
||||||
@ -125,6 +126,7 @@ export function useUserDetails() {
|
|||||||
friendRequests,
|
friendRequests,
|
||||||
friendRequetsModalTab,
|
friendRequetsModalTab,
|
||||||
isFriendsModalVisible,
|
isFriendsModalVisible,
|
||||||
|
friendModalUserId,
|
||||||
showFriendsModal,
|
showFriendsModal,
|
||||||
hideFriendsModal,
|
hideFriendsModal,
|
||||||
fetchUserDetails,
|
fetchUserDetails,
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
import { UserFriend } from "@types";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
export interface UserFriendModalListProps {
|
||||||
|
userId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pageSize = 12;
|
||||||
|
|
||||||
|
export const UserFriendModalList = ({ userId }: UserFriendModalListProps) => {
|
||||||
|
const [page, setPage] = useState(0);
|
||||||
|
const [maxPage, setMaxPage] = useState(0);
|
||||||
|
const [friends, setFriends] = useState<UserFriend[]>([]);
|
||||||
|
|
||||||
|
const loadNextPage = () => {
|
||||||
|
if (page > maxPage) return;
|
||||||
|
window.electron
|
||||||
|
.getUserFriends(userId, pageSize, page * pageSize)
|
||||||
|
.then((newPage) => {
|
||||||
|
if (page === 0) {
|
||||||
|
setMaxPage(newPage.totalFriends / pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
setFriends([...friends, ...newPage.friends]);
|
||||||
|
setPage(page + 1);
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setPage(0);
|
||||||
|
setMaxPage(0);
|
||||||
|
setFriends([]);
|
||||||
|
loadNextPage();
|
||||||
|
}, [userId]);
|
||||||
|
|
||||||
|
return friends.map((friend) => {
|
||||||
|
return <p key={friend.id}>{friend.displayName}</p>;
|
||||||
|
});
|
||||||
|
};
|
@ -3,23 +3,27 @@ import { SPACING_UNIT } from "@renderer/theme.css";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { UserFriendModalAddFriend } from "./user-friend-modal-add-friend";
|
import { UserFriendModalAddFriend } from "./user-friend-modal-add-friend";
|
||||||
|
import { useUserDetails } from "@renderer/hooks";
|
||||||
|
import { UserFriendModalList } from "./user-friend-modal-list";
|
||||||
|
|
||||||
export enum UserFriendModalTab {
|
export enum UserFriendModalTab {
|
||||||
FriendsList,
|
FriendsList,
|
||||||
AddFriend,
|
AddFriend,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserAddFriendsModalProps {
|
export interface UserFriendsModalProps {
|
||||||
visible: boolean;
|
visible: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
initialTab: UserFriendModalTab | null;
|
initialTab: UserFriendModalTab | null;
|
||||||
|
userId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UserFriendModal = ({
|
export const UserFriendModal = ({
|
||||||
visible,
|
visible,
|
||||||
onClose,
|
onClose,
|
||||||
initialTab,
|
initialTab,
|
||||||
}: UserAddFriendsModalProps) => {
|
userId,
|
||||||
|
}: UserFriendsModalProps) => {
|
||||||
const { t } = useTranslation("user_profile");
|
const { t } = useTranslation("user_profile");
|
||||||
|
|
||||||
const tabs = [t("friends_list"), t("add_friends")];
|
const tabs = [t("friends_list"), t("add_friends")];
|
||||||
@ -28,6 +32,9 @@ export const UserFriendModal = ({
|
|||||||
initialTab || UserFriendModalTab.FriendsList
|
initialTab || UserFriendModalTab.FriendsList
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const { userDetails } = useUserDetails();
|
||||||
|
const isMe = userDetails?.id == userId;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (initialTab != null) {
|
if (initialTab != null) {
|
||||||
setCurrentTab(initialTab);
|
setCurrentTab(initialTab);
|
||||||
@ -36,7 +43,7 @@ export const UserFriendModal = ({
|
|||||||
|
|
||||||
const renderTab = () => {
|
const renderTab = () => {
|
||||||
if (currentTab == UserFriendModalTab.FriendsList) {
|
if (currentTab == UserFriendModalTab.FriendsList) {
|
||||||
return <></>;
|
return <UserFriendModalList userId={userId} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentTab == UserFriendModalTab.AddFriend) {
|
if (currentTab == UserFriendModalTab.AddFriend) {
|
||||||
@ -56,19 +63,21 @@ export const UserFriendModal = ({
|
|||||||
gap: `${SPACING_UNIT * 2}px`,
|
gap: `${SPACING_UNIT * 2}px`,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<section style={{ display: "flex", gap: `${SPACING_UNIT}px` }}>
|
{isMe && (
|
||||||
{tabs.map((tab, index) => {
|
<section style={{ display: "flex", gap: `${SPACING_UNIT}px` }}>
|
||||||
return (
|
{tabs.map((tab, index) => {
|
||||||
<Button
|
return (
|
||||||
key={tab}
|
<Button
|
||||||
theme={index === currentTab ? "primary" : "outline"}
|
key={tab}
|
||||||
onClick={() => setCurrentTab(index)}
|
theme={index === currentTab ? "primary" : "outline"}
|
||||||
>
|
onClick={() => setCurrentTab(index)}
|
||||||
{tab}
|
>
|
||||||
</Button>
|
{tab}
|
||||||
);
|
</Button>
|
||||||
})}
|
);
|
||||||
</section>
|
})}
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
<h2>{tabs[currentTab]}</h2>
|
<h2>{tabs[currentTab]}</h2>
|
||||||
{renderTab()}
|
{renderTab()}
|
||||||
</div>
|
</div>
|
||||||
|
@ -424,7 +424,12 @@ export function UserContent({
|
|||||||
<div className={styles.friendsSection}>
|
<div className={styles.friendsSection}>
|
||||||
<button
|
<button
|
||||||
className={styles.friendsSectionHeader}
|
className={styles.friendsSectionHeader}
|
||||||
onClick={() => showFriendsModal(UserFriendModalTab.FriendsList)}
|
onClick={() =>
|
||||||
|
showFriendsModal(
|
||||||
|
UserFriendModalTab.FriendsList,
|
||||||
|
userProfile.id
|
||||||
|
)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<h2>{t("friends")}</h2>
|
<h2>{t("friends")}</h2>
|
||||||
|
|
||||||
@ -480,7 +485,10 @@ export function UserContent({
|
|||||||
<Button
|
<Button
|
||||||
theme="outline"
|
theme="outline"
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
showFriendsModal(UserFriendModalTab.AddFriend)
|
showFriendsModal(
|
||||||
|
UserFriendModalTab.AddFriend,
|
||||||
|
userProfile.id
|
||||||
|
)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<PlusIcon /> {t("add")}
|
<PlusIcon /> {t("add")}
|
||||||
|
Loading…
Reference in New Issue
Block a user