From 7e34886d15e44e83b55052f81a1817e92387de68 Mon Sep 17 00:00:00 2001 From: PhilReact Date: Thu, 19 Jun 2025 07:27:11 +0300 Subject: [PATCH] fix getting list of groups where user is admin --- src/App.tsx | 5 ++ src/components/Apps/AppsPrivate.tsx | 8 ++- src/components/Group/Group.tsx | 59 +++++++++++++--- src/components/Group/GroupJoinRequests.tsx | 78 ++++++++-------------- 4 files changed, 88 insertions(+), 62 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index aab17d7..a9206b8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -129,6 +129,7 @@ import { isUsingImportExportSettingsAtom, lastEnteredGroupIdAtom, mailsAtom, + myGroupsWhereIAmAdminAtom, oldPinnedAppsAtom, qMailLastEnteredTimestampAtom, settingsLocalLastUpdatedAtom, @@ -563,6 +564,9 @@ function App() { const resetAtomQMailLastEnteredTimestampAtom = useResetRecoilState(qMailLastEnteredTimestampAtom) const resetAtomMailsAtom = useResetRecoilState(mailsAtom) const resetLastEnteredGroupIdAtom = useResetRecoilState(lastEnteredGroupIdAtom) + const resetMyGroupsWhereIAmAdminAtom = useResetRecoilState( + myGroupsWhereIAmAdminAtom + ); const resetAllRecoil = () => { resetAtomSortablePinnedAppsAtom(); resetAtomCanSaveSettingToQdnAtom(); @@ -574,6 +578,7 @@ function App() { resetAtomMailsAtom() resetGroupPropertiesAtom() resetLastEnteredGroupIdAtom() + resetMyGroupsWhereIAmAdminAtom() }; useEffect(() => { if (!isMobile) return; diff --git a/src/components/Apps/AppsPrivate.tsx b/src/components/Apps/AppsPrivate.tsx index f2dc302..d62c8d9 100644 --- a/src/components/Apps/AppsPrivate.tsx +++ b/src/components/Apps/AppsPrivate.tsx @@ -214,9 +214,11 @@ export const AppsPrivate = ({myName, myAddress}) => { console.error(error); } }, [myAddress]); - useEffect(() => { - getNames(); - }, [getNames]); + useEffect(() => { + if (isOpenPrivateModal) { + getNames(); + } + }, [getNames, isOpenPrivateModal]); const handleChange = (event: React.SyntheticEvent, newValue: number) => { setValueTabPrivateApp(newValue); diff --git a/src/components/Group/Group.tsx b/src/components/Group/Group.tsx index 8aa603d..e923727 100644 --- a/src/components/Group/Group.tsx +++ b/src/components/Group/Group.tsx @@ -67,7 +67,7 @@ import HomeIcon from "@mui/icons-material/Home"; import CloseIcon from "@mui/icons-material/Close"; import { ThingsToDoInitial } from "./ThingsToDoInitial"; -import { GroupJoinRequests } from "./GroupJoinRequests"; +import { GroupJoinRequests, requestQueueGroupJoinRequests } from "./GroupJoinRequests"; import { GroupForum } from "../Chat/GroupForum"; import { GroupInvites } from "./GroupInvites"; import { @@ -100,7 +100,7 @@ import { formatEmailDate } from "./QMailMessages"; import { useHandleMobileNativeBack } from "../../hooks/useHandleMobileNativeBack"; import { AdminSpace } from "../Chat/AdminSpace"; import { useRecoilState, useSetRecoilState } from "recoil"; -import { addressInfoControllerAtom, groupsPropertiesAtom, isOpenBlockedModalAtom, lastEnteredGroupIdAtom, selectedGroupIdAtom } from "../../atoms/global"; +import { addressInfoControllerAtom, groupsPropertiesAtom, isOpenBlockedModalAtom, lastEnteredGroupIdAtom, myGroupsWhereIAmAdminAtom, selectedGroupIdAtom } from "../../atoms/global"; import { sortArrayByTimestampAndGroupName } from "../../utils/time"; import { BlockedUsersModal } from "./BlockedUsersModal"; import { GlobalTouchMenu } from "../GlobalTouchMenu"; @@ -472,6 +472,9 @@ export const Group = ({ const { setMemberGroups, memberGroups, rootHeight, isRunningPublicNode } = useContext(MyContext); const lastGroupNotification = useRef(null); const [timestampEnterData, setTimestampEnterData] = useState({}); + const groupsPropertiesRef = useRef({}); + const setMyGroupsWhereIAmAdmin = useSetRecoilState(myGroupsWhereIAmAdminAtom); + const [chatMode, setChatMode] = useState("groups"); const [newChat, setNewChat] = useState(false); const [openSnack, setOpenSnack] = React.useState(false); @@ -536,6 +539,9 @@ export const Group = ({ useEffect(()=> { timestampEnterDataRef.current = timestampEnterData }, [timestampEnterData]) + useEffect(() => { + groupsPropertiesRef.current = groupsProperties; + }, [groupsProperties]); useEffect(() => { isFocusedRef.current = isFocused; @@ -988,15 +994,50 @@ export const Group = ({ } }, []) + const getGroupsWhereIAmAMember = useCallback(async (groups) => { + try { + let groupsAsAdmin = []; + const getAllGroupsAsAdmin = groups + .filter((item) => item.groupId !== '0') + .map(async (group) => { + const isAdminResponse = await requestQueueGroupJoinRequests.enqueue( + () => { + return fetch( + `${getBaseApiReact()}/groups/members/${group.groupId}?limit=0&onlyAdmins=true` + ); + } + ); + const isAdminData = await isAdminResponse.json(); - useEffect(()=> { - if(!myAddress) return - if(areKeysEqual(groups?.map((grp)=> grp?.groupId), Object.keys(groupsProperties))){ - } else { - getGroupsProperties(myAddress) + const findMyself = isAdminData?.members?.find( + (member) => member.member === myAddress + ); + + if (findMyself) { + groupsAsAdmin.push(group); + } + return true; + }); + + await Promise.all(getAllGroupsAsAdmin); + setMyGroupsWhereIAmAdmin(groupsAsAdmin); + } catch (error) { + console.error(); } - }, [groups, myAddress]) - + }, []); + + useEffect(() => { + if (!myAddress) return; + if ( + !areKeysEqual( + groups?.map((grp) => grp?.groupId), + Object.keys(groupsPropertiesRef.current) + ) + ) { + getGroupsProperties(myAddress); + getGroupsWhereIAmAMember(groups); + } + }, [groups, myAddress]); useEffect(() => { // Handler function for incoming messages diff --git a/src/components/Group/GroupJoinRequests.tsx b/src/components/Group/GroupJoinRequests.tsx index 02a26ec..3bbd1f8 100644 --- a/src/components/Group/GroupJoinRequests.tsx +++ b/src/components/Group/GroupJoinRequests.tsx @@ -17,7 +17,7 @@ import { CustomLoader } from "../../common/CustomLoader"; import { getBaseApi } from "../../background"; import { MyContext, getBaseApiReact, isMobile } from "../../App"; import { myGroupsWhereIAmAdminAtom } from "../../atoms/global"; -import { useSetRecoilState } from "recoil"; +import { useRecoilState, useSetRecoilState } from "recoil"; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import ExpandLessIcon from '@mui/icons-material/ExpandLess'; export const requestQueueGroupJoinRequests = new RequestQueueWithPromise(2) @@ -28,66 +28,44 @@ export const GroupJoinRequests = ({ myAddress, groups, setOpenManageMembers, get const [groupsWithJoinRequests, setGroupsWithJoinRequests] = React.useState([]) const [loading, setLoading] = React.useState(true) const {txList, setTxList} = React.useContext(MyContext) - const setMyGroupsWhereIAmAdmin = useSetRecoilState( - myGroupsWhereIAmAdminAtom - ); + const [myGroupsWhereIAmAdmin] = useRecoilState(myGroupsWhereIAmAdminAtom); - const getJoinRequests = async ()=> { + + const getJoinRequests = async () => { try { - setLoading(true) - - let groupsAsAdmin = [] - const getAllGroupsAsAdmin = groups.filter((item)=> item.groupId !== '0').map(async (group)=> { - - const isAdminResponse = await requestQueueGroupJoinRequests.enqueue(()=> { - return fetch( - `${getBaseApiReact()}/groups/members/${group.groupId}?limit=0&onlyAdmins=true` - ); - }) - const isAdminData = await isAdminResponse.json() - + setLoading(true); + const res = await Promise.all( + myGroupsWhereIAmAdmin.map(async (group) => { + const joinRequestResponse = + await requestQueueGroupJoinRequests.enqueue(() => { + return fetch( + `${getBaseApiReact()}/groups/joinrequests/${group.groupId}` + ); + }); - const findMyself = isAdminData?.members?.find((member)=> member.member === myAddress) - - if(findMyself){ - groupsAsAdmin.push(group) - } - return true - }) - - - await Promise.all(getAllGroupsAsAdmin) - setMyGroupsWhereIAmAdmin(groupsAsAdmin) - const res = await Promise.all(groupsAsAdmin.map(async (group)=> { - - const joinRequestResponse = await requestQueueGroupJoinRequests.enqueue(()=> { - return fetch( - `${getBaseApiReact()}/groups/joinrequests/${group.groupId}` - ); - }) - - const joinRequestData = await joinRequestResponse.json() - return { - group, - data: joinRequestData - } - })) - setGroupsWithJoinRequests(res) + const joinRequestData = await joinRequestResponse.json(); + return { + group, + data: joinRequestData, + }; + }) + ); + setGroupsWithJoinRequests(res); } catch (error) { - + console.log(error); } finally { - setLoading(false) + setLoading(false); } - } + }; React.useEffect(() => { - if (myAddress && groups.length > 0) { - getJoinRequests() + if (myAddress && myGroupsWhereIAmAdmin.length > 0) { + getJoinRequests(); } else { - setLoading(false) + setLoading(false); } - }, [myAddress, groups]); + }, [myAddress, myGroupsWhereIAmAdmin]); const filteredJoinRequests = React.useMemo(()=> { return groupsWithJoinRequests.map((group)=> {