Skip to content

Commit 7e17101

Browse files
Merge pull request #576 from GetStream/vishal/message-new-fix
Fix for message.new handler
2 parents ab0c761 + d3843a4 commit 7e17101

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/components/ChannelList/hooks/useMessageNewListener.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @ts-check
22

33
import { useEffect, useContext } from 'react';
4+
import uniqBy from 'lodash.uniqby';
5+
46
import { ChatContext } from '../../../context';
57
import { moveChannelUp } from '../utils';
68

@@ -18,13 +20,18 @@ export const useMessageNewListener = (
1820
/** @param {import('stream-chat').Event} e */
1921
const handleEvent = (e) => {
2022
setChannels((channels) => {
23+
const channelInList =
24+
channels.filter((c) => c.cid === e.cid).length > 0;
25+
if (!channelInList) {
26+
// @ts-ignore
27+
const channel = client.channel(e.channel_type, e.channel_id);
28+
return uniqBy([channel, ...channels], 'cid');
29+
}
2130
if (!lockChannelOrder) return moveChannelUp(e.cid, channels);
2231
return channels;
2332
});
2433
};
25-
2634
client.on('message.new', handleEvent);
27-
2835
return () => {
2936
client.off('message.new', handleEvent);
3037
};

0 commit comments

Comments
 (0)