@@ -113,10 +113,15 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
113113 return customHandler ( setChannels , event ) ;
114114 }
115115
116- setChannels ( ( channels ) => {
117- const targetChannelIndex = channels . findIndex ( ( channel ) => channel . cid === event . cid ) ;
116+ const channelType = event . channel_type ;
117+ const channelId = event . channel_id ;
118+
119+ if ( ! channelType || ! channelId ) return ;
120+
121+ setChannels ( ( currentChannels ) => {
122+ const targetChannel = client . channel ( channelType , channelId ) ;
123+ const targetChannelIndex = currentChannels . indexOf ( targetChannel ) ;
118124 const targetChannelExistsWithinList = targetChannelIndex >= 0 ;
119- const targetChannel = channels [ targetChannelIndex ] ;
120125
121126 const isTargetChannelPinned = isChannelPinned ( targetChannel ) ;
122127 const isTargetChannelArchived = isChannelArchived ( targetChannel ) ;
@@ -125,35 +130,26 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
125130 const considerPinnedChannels = shouldConsiderPinnedChannels ( sort ) ;
126131
127132 if (
128- // target channel is archived, filter is defined and is set to false
129- ( isTargetChannelArchived && considerArchivedChannels && ! filters . archived ) ||
130- // target channel is pinned, sort is defined
131- ( isTargetChannelPinned && considerPinnedChannels ) ||
133+ // filter is defined, target channel is archived and filter option is set to false
134+ ( considerArchivedChannels && isTargetChannelArchived && ! filters . archived ) ||
135+ // filter is defined, target channel isn't archived and filter option is set to true
136+ ( considerArchivedChannels && ! isTargetChannelArchived && filters . archived ) ||
137+ // sort option is defined, target channel is pinned
138+ ( considerPinnedChannels && isTargetChannelPinned ) ||
132139 // list order is locked
133140 lockChannelOrder ||
134141 // target channel is not within the loaded list and loading from cache is disallowed
135142 ( ! targetChannelExistsWithinList && ! allowNewMessagesFromUnfilteredChannels )
136143 ) {
137- return channels ;
138- }
139-
140- // we either have the channel to move or we pull it from the cache (or instantiate) if it's allowed
141- const channelToMove : Channel < SCG > | null =
142- channels [ targetChannelIndex ] ??
143- ( allowNewMessagesFromUnfilteredChannels && event . channel_type
144- ? client . channel ( event . channel_type , event . channel_id )
145- : null ) ;
146-
147- if ( channelToMove ) {
148- return moveChannelUpwards ( {
149- channels,
150- channelToMove,
151- channelToMoveIndexWithinChannels : targetChannelIndex ,
152- sort,
153- } ) ;
144+ return currentChannels ;
154145 }
155146
156- return channels ;
147+ return moveChannelUpwards ( {
148+ channels : currentChannels ,
149+ channelToMove : targetChannel ,
150+ channelToMoveIndexWithinChannels : targetChannelIndex ,
151+ sort,
152+ } ) ;
157153 } ) ;
158154 } ,
159155 [ client ] ,
@@ -272,7 +268,6 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
272268 return ;
273269 }
274270
275- const member = event . member ;
276271 const channelType = event . channel_type ;
277272 const channelId = event . channel_id ;
278273
@@ -287,6 +282,9 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
287282 const targetChannelIndex = currentChannels . indexOf ( targetChannel ) ;
288283 const targetChannelExistsWithinList = targetChannelIndex >= 0 ;
289284
285+ const isTargetChannelArchived = isChannelArchived ( targetChannel ) ;
286+ const isTargetChannelPinned = isChannelPinned ( targetChannel ) ;
287+
290288 // handle pinning
291289 if ( ! considerPinnedChannels || lockChannelOrder ) return currentChannels ;
292290
@@ -298,9 +296,8 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
298296
299297 // handle archiving (remove channel)
300298 if (
301- considerArchivedChannels &&
302- ( ( typeof member . archived_at === 'string' && ! filters . archived ) ||
303- ( ! member . archived_at && filters . archived ) )
299+ ( considerArchivedChannels && isTargetChannelArchived && ! filters . archived ) ||
300+ ( considerArchivedChannels && ! isTargetChannelArchived && filters . archived )
304301 ) {
305302 return newChannels ;
306303 }
@@ -310,7 +307,7 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
310307 // calculate last pinned channel index only if `pinned_at` sort is set to
311308 // ascending order or if it's in descending order while the pin is being removed, otherwise
312309 // we move to the top (index 0)
313- if ( pinnedAtSort === 1 || ( pinnedAtSort === - 1 && ! member . pinned_at ) ) {
310+ if ( pinnedAtSort === 1 || ( pinnedAtSort === - 1 && ! isTargetChannelPinned ) ) {
314311 lastPinnedChannelIndex = findLastPinnedChannelIndex ( { channels : newChannels } ) ;
315312 }
316313
0 commit comments