Skip to content

Commit 95bd650

Browse files
Merge pull request #663 from GetStream/vishal/allowNewMessagesFromUnfilteredChannels
Adding a prop allowNewMessagesFromUnfilteredChannels on ChannelList
2 parents 800ad79 + 6ce0233 commit 95bd650

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/components/ChannelList/ChannelList.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ const ChannelList = (props) => {
121121
useMobileNavigation(channelListRef, navOpen, closeMobileNav);
122122

123123
// All the event listeners
124-
useMessageNewListener(setChannels, props.lockChannelOrder);
124+
useMessageNewListener(
125+
setChannels,
126+
props.lockChannelOrder,
127+
props.allowNewMessagesFromUnfilteredChannels,
128+
);
125129
useNotificationMessageNewListener(setChannels, props.onMessageNew);
126130
useNotificationAddedToChannelListener(setChannels, props.onAddedToChannel);
127131
useNotificationRemovedFromChannelListener(
@@ -381,6 +385,18 @@ ChannelList.propTypes = {
381385
* If true, channels won't be dynamically sorted by most recent message.
382386
*/
383387
lockChannelOrder: PropTypes.bool,
388+
/**
389+
* When client receives an event `message.new`, we push that channel to top of the list.
390+
*
391+
* But If the channel doesn't exist in the list, then we get the channel from client
392+
* (client maintains list of watched channels as `client.activeChannels`) and push
393+
* that channel to top of the list by default. You can disallow this behavior by setting following
394+
* prop to false. This is quite usefull where you have multiple tab structure and you don't want
395+
* ChannelList in Tab1 to react to new message on some channel in Tab2.
396+
*
397+
* Default value is true.
398+
*/
399+
allowNewMessagesFromUnfilteredChannels: PropTypes.bool,
384400
};
385401

386402
export default React.memo(ChannelList);

src/components/ChannelList/hooks/useMessageNewListener.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { moveChannelUp } from '../utils';
1414
export const useMessageNewListener = (
1515
setChannels,
1616
lockChannelOrder = false,
17+
allowNewMessagesFromUnfilteredChannels = true,
1718
) => {
1819
const { client } = useContext(ChatContext);
1920
useEffect(() => {
@@ -22,7 +23,7 @@ export const useMessageNewListener = (
2223
setChannels((channels) => {
2324
const channelInList =
2425
channels.filter((c) => c.cid === e.cid).length > 0;
25-
if (!channelInList) {
26+
if (!channelInList && allowNewMessagesFromUnfilteredChannels) {
2627
// @ts-ignore
2728
const channel = client.channel(e.channel_type, e.channel_id);
2829
return uniqBy([channel, ...channels], 'cid');

types/index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ export interface ChannelListProps {
275275
List?: React.ElementType<ChannelListUIComponentProps>;
276276
Paginator?: React.ElementType<PaginatorProps>;
277277
lockChannelOrder?: boolean;
278+
/**
279+
* When client receives an event `message.new`, we push that channel to top of the list.
280+
*
281+
* But If the channel doesn't exist in the list, then we get the channel from client
282+
* (client maintains list of watched channels as `client.activeChannels`) and push
283+
* that channel to top of the list by default. You can disallow this behavior by setting following
284+
* prop to false. This is quite usefull where you have multiple tab structure and you don't want
285+
* ChannelList in Tab1 to react to new message on some channel in Tab2.
286+
*
287+
* Default value is true.
288+
*/
289+
allowNewMessagesFromUnfilteredChannels?: PropTypes.bool;
278290
onMessageNew?(
279291
thisArg: React.Dispatch<React.SetStateAction<Client.Channel[]>>,
280292
e: Client.Event,

0 commit comments

Comments
 (0)