11import { useEffect } from 'react' ;
22
3- import type { Channel , ChannelFilters , Event } from 'stream-chat' ;
3+ import type { Channel , ChannelFilters , ChannelSort , Event } from 'stream-chat' ;
44
55import { useChatContext } from '../../../../contexts/chatContext/ChatContext' ;
66
77import type { DefaultStreamChatGenerics } from '../../../../types/types' ;
88import { moveChannelUp } from '../../utils' ;
9- import { isChannelArchived } from '../utils' ;
9+ import { isChannelArchived , isChannelPinned , shouldConsiderPinnedChannels } from '../utils' ;
1010
1111type Parameters < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics > =
1212 {
@@ -17,8 +17,10 @@ type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultSt
1717 setChannels : React . Dispatch < React . SetStateAction < Channel < StreamChatGenerics > [ ] | null > > ,
1818 event : Event < StreamChatGenerics > ,
1919 filters ?: ChannelFilters < StreamChatGenerics > ,
20+ sort ?: ChannelSort < StreamChatGenerics > ,
2021 ) => void ;
2122 filters ?: ChannelFilters < StreamChatGenerics > ;
23+ sort ?: ChannelSort < StreamChatGenerics > ;
2224 } ;
2325
2426export const useNewMessage = <
@@ -28,13 +30,14 @@ export const useNewMessage = <
2830 onNewMessage,
2931 setChannels,
3032 filters,
33+ sort,
3134} : Parameters < StreamChatGenerics > ) => {
3235 const { client } = useChatContext < StreamChatGenerics > ( ) ;
3336
3437 useEffect ( ( ) => {
3538 const handleEvent = ( event : Event < StreamChatGenerics > ) => {
3639 if ( typeof onNewMessage === 'function' ) {
37- onNewMessage ( lockChannelOrder , setChannels , event , filters ) ;
40+ onNewMessage ( lockChannelOrder , setChannels , event , filters , sort ) ;
3841 } else {
3942 setChannels ( ( channels ) => {
4043 if ( ! channels ) return channels ;
@@ -45,11 +48,19 @@ export const useNewMessage = <
4548 const targetChannel = channels [ targetChannelIndex ] ;
4649
4750 const isTargetChannelArchived = isChannelArchived ( targetChannel ) ;
51+ const isTargetChannelPinned = isChannelPinned ( targetChannel ) ;
52+ const isArchivedFilterTrue = filters && filters . archived === true ;
53+ const isArchivedFilterFalse = filters && filters . archived === false ;
4854
49- const considerArchivedChannels = filters && filters . archived === false ;
55+ const considerPinnedChannels = shouldConsiderPinnedChannels ( sort ) ;
5056
51- // If channel is archived and we don't want to consider archived channels, return existing list
52- if ( isTargetChannelArchived && considerArchivedChannels ) {
57+ if (
58+ // If the channel is archived and we are not considering archived channels
59+ ( isTargetChannelArchived && isArchivedFilterFalse ) ||
60+ // If the channel is pinned and we are not considering pinned channels
61+ ( isTargetChannelPinned && considerPinnedChannels ) ||
62+ lockChannelOrder
63+ ) {
5364 return channels ;
5465 }
5566
@@ -58,6 +69,14 @@ export const useNewMessage = <
5869 // It may happen that channel was hidden using channel.hide(). In that case
5970 // We remove it from `channels` state, but its still being watched and exists in client.activeChannels.
6071 const channel = client . channel ( event . channel_type , event . channel_id ) ;
72+ // While adding new channels, we need to consider whether they are archived or not.
73+ if (
74+ // When archived filter false, and channel is archived
75+ ( isChannelArchived ( channel ) && isArchivedFilterFalse ) ||
76+ // When archived filter true, and channel is not archived
77+ ( isArchivedFilterTrue && ! isChannelArchived ( channel ) )
78+ )
79+ return channels ;
6180 return [ channel , ...channels ] ;
6281 }
6382
0 commit comments