@@ -4,8 +4,14 @@ import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useRef } fro
44import { Channel , Event , ExtendableGenerics } from 'stream-chat' ;
55import uniqBy from 'lodash.uniqby' ;
66
7- import { isChannelPinned } from '.' ;
8- import { findLastPinnedChannelIndex , moveChannelUpwards } from '../utils' ;
7+ import {
8+ findLastPinnedChannelIndex ,
9+ isChannelArchived ,
10+ isChannelPinned ,
11+ moveChannelUpwards ,
12+ shouldConsiderArchivedChannels ,
13+ shouldConsiderPinnedChannels ,
14+ } from '../utils' ;
915import { useChatContext } from '../../../context' ;
1016import { getChannel } from '../../../utils' ;
1117import { ChannelListProps } from '../ChannelList' ;
@@ -27,16 +33,14 @@ type RepeatedParameters<SCG extends ExtendableGenerics> = {
2733type HandleMessageNewParameters < SCG extends ExtendableGenerics > = BaseParameters < SCG > &
2834 RepeatedParameters < SCG > & {
2935 allowNewMessagesFromUnfilteredChannels : boolean ;
30- considerPinnedChannels : boolean ;
3136 lockChannelOrder : boolean ;
32- } ;
37+ } & Required < Pick < ChannelListProps < SCG > , 'filters' | 'sort' > > ;
3338
3439type HandleNotificationMessageNewParameters < SCG extends ExtendableGenerics > = BaseParameters < SCG > &
3540 RepeatedParameters < SCG > & {
3641 allowNewMessagesFromUnfilteredChannels : boolean ;
37- considerPinnedChannels : boolean ;
3842 lockChannelOrder : boolean ;
39- } ;
43+ } & Required < Pick < ChannelListProps < SCG > , 'filters' | 'sort' > > ;
4044
4145type HandleNotificationRemovedFromChannelParameters < SCG extends ExtendableGenerics > =
4246 BaseParameters < SCG > & RepeatedParameters < SCG > ;
@@ -45,14 +49,12 @@ type HandleNotificationAddedToChannelParameters<SCG extends ExtendableGenerics>
4549 BaseParameters < SCG > &
4650 RepeatedParameters < SCG > & {
4751 allowNewMessagesFromUnfilteredChannels : boolean ;
48- considerPinnedChannels : boolean ;
4952 lockChannelOrder : boolean ;
50- } ;
53+ } & Required < Pick < ChannelListProps < SCG > , 'sort' > > ;
5154
5255type HandleMemberUpdatedParameters < SCG extends ExtendableGenerics > = BaseParameters < SCG > & {
53- considerPinnedChannels : boolean ;
5456 lockChannelOrder : boolean ;
55- } ;
57+ } & Required < Pick < ChannelListProps < SCG > , 'sort' > > ;
5658
5759type HandleChannelDeletedParameters < SCG extends ExtendableGenerics > = BaseParameters < SCG > &
5860 RepeatedParameters < SCG > ;
@@ -97,7 +99,8 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
9799 const handleMessageNew = useCallback (
98100 ( {
99101 allowNewMessagesFromUnfilteredChannels,
100- considerPinnedChannels,
102+ filters,
103+ sort,
101104 customHandler,
102105 event,
103106 lockChannelOrder,
@@ -110,12 +113,17 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
110113 setChannels ( ( channels ) => {
111114 const targetChannelIndex = channels . findIndex ( ( channel ) => channel . cid === event . cid ) ;
112115 const targetChannelExistsWithinList = targetChannelIndex >= 0 ;
116+ const targetChannel = channels [ targetChannelIndex ] ;
113117
114- const isTargetChannelPinned = isChannelPinned ( {
115- channel : channels [ targetChannelIndex ] ,
116- } ) ;
118+ const isTargetChannelPinned = isChannelPinned ( targetChannel ) ;
119+ const isTargetChannelArchived = isChannelArchived ( targetChannel ) ;
120+
121+ const considerArchivedChannels = shouldConsiderArchivedChannels ( filters ) ;
122+ const considerPinnedChannels = shouldConsiderPinnedChannels ( sort ) ;
117123
118124 if (
125+ // target channel is archived
126+ ( isTargetChannelArchived && considerArchivedChannels ) ||
119127 // target channel is pinned
120128 ( isTargetChannelPinned && considerPinnedChannels ) ||
121129 // list order is locked
@@ -151,6 +159,8 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
151159 const handleNotificationMessageNew = useCallback (
152160 async ( {
153161 allowNewMessagesFromUnfilteredChannels,
162+ sort,
163+ filters,
154164 customHandler,
155165 event,
156166 setChannels,
@@ -159,14 +169,31 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
159169 return customHandler ( setChannels , event ) ;
160170 }
161171
162- if ( allowNewMessagesFromUnfilteredChannels && event . channel ?. type ) {
163- const channel = await getChannel ( {
164- client,
165- id : event . channel . id ,
166- type : event . channel . type ,
167- } ) ;
168- setChannels ( ( channels ) => uniqBy ( [ channel , ...channels ] , 'cid' ) ) ;
172+ const considerArchivedChannels = shouldConsiderArchivedChannels ( filters ) ;
173+ const considerPinnedChannels = shouldConsiderPinnedChannels ( sort ) ;
174+
175+ if ( ! event . channel ?. type ) return ;
176+
177+ const channel = await getChannel ( {
178+ client,
179+ id : event . channel . id ,
180+ type : event . channel . type ,
181+ } ) ;
182+
183+ if ( isChannelArchived ( channel ) && considerArchivedChannels ) {
184+ return ;
169185 }
186+
187+ if ( ! allowNewMessagesFromUnfilteredChannels ) return ;
188+
189+ setChannels ( ( channels ) =>
190+ moveChannelUpwards ( {
191+ channels,
192+ channelToMove : channel ,
193+ channelToMoveIndexWithinChannels : - 1 ,
194+ considerPinnedChannels,
195+ } ) ,
196+ ) ;
170197 } ,
171198 [ client ] ,
172199 ) ;
@@ -217,19 +244,16 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
217244 ) ;
218245
219246 const handleMemberUpdated = useCallback (
220- ( {
221- considerPinnedChannels,
222- event,
223- lockChannelOrder,
224- setChannels,
225- } : HandleMemberUpdatedParameters < SCG > ) => {
226- if ( lockChannelOrder || ! considerPinnedChannels ) return ;
247+ ( { sort, event, lockChannelOrder, setChannels } : HandleMemberUpdatedParameters < SCG > ) => {
248+ if ( ! event . member ?. user || event . member . user . id !== client . userID || ! event . channel_type ) {
249+ return ;
250+ }
227251
228- if ( ! event . member || ! event . channel_type ) return ;
229- // const member = e.member;
230252 const channelType = event . channel_type ;
231253 const channelId = event . channel_id ;
232254
255+ const considerPinnedChannels = shouldConsiderPinnedChannels ( sort ) ;
256+
233257 setChannels ( ( currentChannels ) => {
234258 const targetChannel = client . channel ( channelType , channelId ) ;
235259 // assumes that channel instances are not changing
@@ -242,6 +266,14 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
242266 newChannels . splice ( targetChannelIndex , 1 ) ;
243267 }
244268
269+ // handle archiving
270+ if ( typeof event . member ?. archived_at === 'string' ) {
271+ return newChannels ;
272+ }
273+
274+ // handle pinning
275+ if ( ! considerPinnedChannels || lockChannelOrder ) return currentChannels ;
276+
245277 const lastPinnedChannelIndex = findLastPinnedChannelIndex ( { channels : newChannels } ) ;
246278 const newTargetChannelIndex =
247279 typeof lastPinnedChannelIndex === 'number' ? lastPinnedChannelIndex + 1 : 0 ;
@@ -385,7 +417,10 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
385417} ;
386418
387419type UseDefaultHandleChannelListShapeParameters < SCG extends ExtendableGenerics > = Required <
388- Pick < ChannelListProps < SCG > , 'allowNewMessagesFromUnfilteredChannels' | 'lockChannelOrder' >
420+ Pick <
421+ ChannelListProps < SCG > ,
422+ 'allowNewMessagesFromUnfilteredChannels' | 'lockChannelOrder' | 'filters' | 'sort'
423+ >
389424> &
390425 Pick <
391426 ChannelListProps < SCG > ,
@@ -399,7 +434,6 @@ type UseDefaultHandleChannelListShapeParameters<SCG extends ExtendableGenerics>
399434 | 'onMessageNewHandler'
400435 | 'onRemovedFromChannel'
401436 > & {
402- considerPinnedChannels : boolean ;
403437 customHandleChannelListShape ?: ( data : {
404438 defaults : ReturnType < typeof useChannelListShapeDefaults < SCG > > ;
405439 event : Event < SCG > ;
@@ -410,7 +444,6 @@ type UseDefaultHandleChannelListShapeParameters<SCG extends ExtendableGenerics>
410444
411445export const usePrepareShapeHandlers = < SCG extends ExtendableGenerics > ( {
412446 allowNewMessagesFromUnfilteredChannels,
413- considerPinnedChannels,
414447 customHandleChannelListShape,
415448 lockChannelOrder,
416449 onAddedToChannel,
@@ -423,6 +456,8 @@ export const usePrepareShapeHandlers = <SCG extends ExtendableGenerics>({
423456 onMessageNewHandler,
424457 onRemovedFromChannel,
425458 setChannels,
459+ filters,
460+ sort,
426461} : UseDefaultHandleChannelListShapeParameters < SCG > ) => {
427462 const defaults = useChannelListShapeDefaults < SCG > ( ) ;
428463
@@ -439,7 +474,8 @@ export const usePrepareShapeHandlers = <SCG extends ExtendableGenerics>({
439474 case 'message.new' :
440475 defaults . handleMessageNew ( {
441476 allowNewMessagesFromUnfilteredChannels,
442- considerPinnedChannels,
477+ sort,
478+ filters,
443479 customHandler : onMessageNewHandler ,
444480 event,
445481 lockChannelOrder,
@@ -449,7 +485,8 @@ export const usePrepareShapeHandlers = <SCG extends ExtendableGenerics>({
449485 case 'notification.message_new' :
450486 defaults . handleNotificationMessageNew ( {
451487 allowNewMessagesFromUnfilteredChannels,
452- considerPinnedChannels,
488+ sort,
489+ filters,
453490 customHandler : onMessageNew ,
454491 event,
455492 lockChannelOrder,
@@ -459,7 +496,7 @@ export const usePrepareShapeHandlers = <SCG extends ExtendableGenerics>({
459496 case 'notification.added_to_channel' :
460497 defaults . handleNotificationAddedToChannel ( {
461498 allowNewMessagesFromUnfilteredChannels,
462- considerPinnedChannels ,
499+ sort ,
463500 customHandler : onAddedToChannel ,
464501 event,
465502 lockChannelOrder,
@@ -497,7 +534,7 @@ export const usePrepareShapeHandlers = <SCG extends ExtendableGenerics>({
497534 break ;
498535 case 'member.updated' :
499536 defaults . handleMemberUpdated ( {
500- considerPinnedChannels ,
537+ sort ,
501538 event,
502539 lockChannelOrder,
503540 setChannels,
0 commit comments