Skip to content

Commit

Permalink
Fix:Fix: Restrict pin icon visibility based on user permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
SinghaAnirban005 committed Nov 30, 2024
1 parent 8ba15a5 commit cc9afe1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/react/src/hooks/useRCAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export const useRCAuth = () => {
);
const setPassword = useUserStore((state) => state.setPassword);
const setEmailorUser = useUserStore((state) => state.setEmailorUser);
const setUserPermissions = useUserStore((state) => state.setUserPermissions);
const dispatchToastMessage = useToastBarDispatch();

const handleLogin = async (userOrEmail, password, code) => {
try {
const res = await RCInstance.login(userOrEmail, password, code);
const permissions = await RCInstance.permissionInfo();
if (res.error === 'Unauthorized' || res.error === 403) {
dispatchToastMessage({
type: 'error',
Expand Down Expand Up @@ -56,6 +58,7 @@ export const useRCAuth = () => {
setIsTotpModalOpen(false);
setEmailorUser(null);
setPassword(null);
setUserPermissions(permissions);
dispatchToastMessage({
type: 'success',
message: 'Successfully logged in',
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/store/userStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const useUserStore = create((set) => ({
setEmailorUser: (emailoruser) => set(() => ({ emailoruser })),
roles: {},
setRoles: (roles) => set((state) => ({ ...state, roles })),
userPermissions: {},
setUserPermissions: (userPermissions) =>
set((state) => ({ ...state, userPermissions })),
showCurrentUserInfo: false,
setShowCurrentUserInfo: (showCurrentUserInfo) =>
set(() => ({ showCurrentUserInfo })),
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/views/Message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const Message = ({

const authenticatedUserId = useUserStore((state) => state.userId);
const authenticatedUserUsername = useUserStore((state) => state.username);
const userRoles = useUserStore((state) => state.roles);
const pinPermissions = useUserStore(
(state) => state.userPermissions.update[150].roles
);
const [setMessageToReport, toggleShowReportMessage] = useMessageStore(
(state) => [state.setMessageToReport, state.toggleShowReportMessage]
);
Expand All @@ -67,6 +71,8 @@ const Message = ({
const theme = useTheme();
const styles = getMessageStyles(theme);
const bubbleStyles = useBubbleStyles(isMe);
const pinRoles = new Set(pinPermissions);
const isAllowedToPin = userRoles.some((role) => pinRoles.has(role));

const variantStyles =
!isInSidebar && variantOverrides === 'bubble' ? bubbleStyles : {};
Expand Down Expand Up @@ -200,6 +206,7 @@ const Message = ({
message={message}
isEditing={editMessage._id === message._id}
authenticatedUserId={authenticatedUserId}
isAllowedToPin={isAllowedToPin}
handleOpenThread={handleOpenThread}
handleDeleteMessage={handleDeleteMessage}
handleStarMessage={handleStarMessage}
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/views/Message/MessageToolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const MessageToolbox = ({
style = {},
isThreadMessage = false,
authenticatedUserId,
isAllowedToPin,
handleOpenThread,
handleEmojiClick,
handlePinMessage,
Expand Down Expand Up @@ -110,7 +111,7 @@ export const MessageToolbox = ({
id: 'pin',
onClick: () => handlePinMessage(message),
iconName: message.pinned ? 'pin-filled' : 'pin',
visible: !isThreadMessage,
visible: !isThreadMessage && isAllowedToPin,
},
edit: {
label: 'Edit',
Expand Down

0 comments on commit cc9afe1

Please sign in to comment.