Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/auth/src/RocketChatAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class RocketChatAuth {
async onAuthChange(callback: (user: object | null) => void) {
this.authListeners.push(callback);
const user = await this.getCurrentUser();
callback(user);
if (this.authListeners.includes(callback)) {
callback(user);
}
}

async removeAuthListener(callback: (user: object | null) => void) {
Expand Down
26 changes: 19 additions & 7 deletions packages/react/src/views/ChatBody/ChatBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,18 @@ const ChatBody = ({
);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
const onAuthChange = (user) => {
if (user) {
RCInstance.addMessageListener(addMessage);
RCInstance.addMessageDeleteListener(removeMessage);
RCInstance.addActionTriggeredListener(onActionTriggerResponse);
RCInstance.addUiInteractionListener(onActionTriggerResponse);
}
});
};
RCInstance.auth.onAuthChange(onAuthChange);

return () => {
RCInstance.auth.removeAuthListener(onAuthChange);
RCInstance.removeMessageListener(addMessage);
RCInstance.removeMessageDeleteListener(removeMessage);
RCInstance.removeActionTriggeredListener(onActionTriggerResponse);
Expand All @@ -164,25 +166,35 @@ const ChatBody = ({
}, [RCInstance, addMessage, removeMessage, onActionTriggerResponse]);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
const onAuthChange = (user) => {
if (user) {
getMessagesAndRoles();
setHasMoreMessages(true);
} else {
getMessagesAndRoles(anonymousMode);
}
});
};
RCInstance.auth.onAuthChange(onAuthChange);

return () => {
RCInstance.auth.removeAuthListener(onAuthChange);
};
}, [RCInstance, anonymousMode, getMessagesAndRoles]);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
const onAuthChange = (user) => {
if (user) {
fetchAndSetPermissions();
} else {
permissionsRef.current = null;
}
});
}, []);
};
RCInstance.auth.onAuthChange(onAuthChange);

return () => {
RCInstance.auth.removeAuthListener(onAuthChange);
};
}, [RCInstance, fetchAndSetPermissions, permissionsRef]);

// Expose clearUnreadDivider function via ref for ChatInput to call
useEffect(() => {
Expand Down
17 changes: 11 additions & 6 deletions packages/react/src/views/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
const onAuthChange = (user) => {
if (user) {
RCInstance.getCommandsList()
.then((data) => setCommands(data.commands || []))
Expand All @@ -156,7 +156,12 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
)
.catch(console.error);
}
});
};
RCInstance.auth.onAuthChange(onAuthChange);

return () => {
RCInstance.auth.removeAuthListener(onAuthChange);
};
}, [RCInstance, isChannelPrivate, setMembersHandler]);

useEffect(() => {
Expand Down Expand Up @@ -545,8 +550,8 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
editMessage.msg || editMessage.attachments
? 'Editing Message'
: isChannelReadOnly
? 'This room is read only'
: undefined
? 'This room is read only'
: undefined
}
iconName={
editMessage.msg || editMessage.attachments ? 'edit' : undefined
Expand Down Expand Up @@ -611,8 +616,8 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
? isChannelArchived
? 'Room archived'
: canSendMsg
? `Message #${channelInfo.name}`
: 'This room is read only'
? `Message #${channelInfo.name}`
: 'This room is read only'
: 'Sign in to chat'
}
css={css`
Expand Down
11 changes: 8 additions & 3 deletions packages/react/src/views/EmbeddedChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const EmbeddedChat = (props) => {

const {
isClosable = false,
setClosableState = () => {},
setClosableState = () => { },
width = '100%',
height = '95vh',
host = 'http://localhost:3000',
Expand Down Expand Up @@ -134,7 +134,7 @@ const EmbeddedChat = (props) => {
}, [RCInstance, auth, setIsLoginIn]);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
const onAuthChange = (user) => {
if (user) {
RCInstance.connect()
.then(() => {
Expand All @@ -151,7 +151,12 @@ const EmbeddedChat = (props) => {
} else {
setIsUserAuthenticated(false);
}
});
};
RCInstance.auth.onAuthChange(onAuthChange);

return () => {
RCInstance.auth.removeAuthListener(onAuthChange);
};
}, [
RCInstance,
setAuthenticatedName,
Expand Down