From 2317c2968c990647251a6b39fa29133a415f1d1f Mon Sep 17 00:00:00 2001 From: thesynthax Date: Tue, 9 Apr 2024 17:32:21 +0530 Subject: [PATCH] fix pin icon show --- .../src/components/ChatHeader/ChatHeader.js | 31 ++++++++++++++----- .../react/src/components/Message/Message.js | 1 + .../src/components/Message/MessageToolbox.js | 8 +++-- packages/react/src/store/userStore.js | 2 ++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/react/src/components/ChatHeader/ChatHeader.js b/packages/react/src/components/ChatHeader/ChatHeader.js index 6ae57ef8c..64d2abc3e 100644 --- a/packages/react/src/components/ChatHeader/ChatHeader.js +++ b/packages/react/src/components/ChatHeader/ChatHeader.js @@ -155,29 +155,44 @@ const ChatHeader = ({ setShowSearch(false); }, [setShowMentions, setShowSearch]); + const authenticatedUserRoles = useUserStore((state) => state.roles); + const setCanPinMsg = useUserStore((state) => state.setCanPinMsg); + useEffect(() => { const getMessageLimit = async () => { const messageLimitObj = await RCInstance.getMessageLimit(); setMessageLimit(messageLimitObj?.value); }; - const setMessageAllowed = async () => { + const setMsgAndPinAllowed = async (ro) => { const permissionRes = await RCInstance.permissionInfo(); const channelRolesRes = await RCInstance.getChannelRoles( isChannelPrivate ); if (permissionRes.success && channelRolesRes.success) { + const pinMsgRoles = permissionRes.update[146]?.roles || []; const postMsgRoles = permissionRes.update[140]?.roles || []; - const userRoles = channelRolesRes.roles + const channelMemberRoles = channelRolesRes.roles .filter((chRole) => chRole.u?._id === authenticatedUserId) .flatMap((chRole) => chRole.roles); - const canSendMsg = - userRoles.length > 0 && - postMsgRoles.some((role) => userRoles.includes(role)); - setCanSendMsg(canSendMsg); + const concatenatedRoles = channelMemberRoles.concat( + authenticatedUserRoles + ); + + if (ro) { + const canSendMsg = + channelMemberRoles.length > 0 && + postMsgRoles.some((role) => channelMemberRoles.includes(role)); + setCanSendMsg(canSendMsg); + } + + const canPinMsg = + concatenatedRoles.length > 0 && + pinMsgRoles.some((role) => concatenatedRoles.includes(role)); + setCanPinMsg(canPinMsg); } }; @@ -186,7 +201,7 @@ const ChatHeader = ({ if (res.success) { setChannelInfo(res.room); if (res.room.t === 'p') setIsChannelPrivate(true); - if (res.room.ro) setMessageAllowed(); + setMsgAndPinAllowed(res.room.ro); } else if ( 'errorType' in res && res.errorType === 'error-room-not-found' @@ -221,7 +236,9 @@ const ChatHeader = ({ toastPosition, isChannelPrivate, setCanSendMsg, + setCanPinMsg, authenticatedUserId, + authenticatedUserRoles, setMessageLimit, ]); diff --git a/packages/react/src/components/Message/Message.js b/packages/react/src/components/Message/Message.js index 5cc9ebe3f..5af49a95a 100644 --- a/packages/react/src/components/Message/Message.js +++ b/packages/react/src/components/Message/Message.js @@ -235,6 +235,7 @@ const Message = ({ {!message.t && showToolbox ? ( state.canPinMsg); + return ( <> @@ -141,7 +145,7 @@ export const MessageToolbox = ({ }} /> - {!isThreadMessage && ( + {!isThreadMessage && canPinMsg && ( ({ })), isUserAuthenticated: false, canSendMsg: true, + canPinMsg: true, setIsUserAuthenticated: (isUserAuthenticated) => set(() => ({ isUserAuthenticated })), setCanSendMsg: (canSendMsg) => set(() => ({ canSendMsg })), + setCanPinMsg: (canPinMsg) => set(() => ({ canPinMsg })), password: null, setPassword: (password) => set(() => ({ password })), emailoruser: null,