11import uniqBy from 'lodash.uniqby' ;
2- import type { Channel , ExtendableGenerics } from 'stream-chat' ;
2+ import type { Channel , ChannelSort , ExtendableGenerics } from 'stream-chat' ;
33
44import type { DefaultStreamChatGenerics } from '../../types/types' ;
55import type { ChannelListProps } from './ChannelList' ;
@@ -59,27 +59,24 @@ export function findLastPinnedChannelIndex<SCG extends ExtendableGenerics>({
5959type MoveChannelUpwardsParams < SCG extends DefaultStreamChatGenerics = DefaultStreamChatGenerics > = {
6060 channels : Array < Channel < SCG > > ;
6161 channelToMove : Channel < SCG > ;
62+ sort : ChannelSort < SCG > ;
6263 /**
6364 * If the index of the channel within `channels` list which is being moved upwards
6465 * (`channelToMove`) is known, you can supply it to skip extra calculation.
6566 */
6667 channelToMoveIndexWithinChannels ?: number ;
67- /**
68- * Pinned channels should not move within the list based on recent activity, channels which
69- * receive messages and are not pinned should move upwards but only under the last pinned channel
70- * in the list. Property defaults to `false` and should be calculated based on existence of
71- * the `pinned_at` sort option.
72- */
73- considerPinnedChannels ?: boolean ;
7468} ;
7569
70+ /**
71+ * This function should not be used to move pinned already channels.
72+ */
7673export const moveChannelUpwards = <
7774 SCG extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
7875> ( {
7976 channels,
8077 channelToMove,
8178 channelToMoveIndexWithinChannels,
82- considerPinnedChannels = false ,
79+ sort ,
8380} : MoveChannelUpwardsParams < SCG > ) => {
8481 // get index of channel to move up
8582 const targetChannelIndex =
@@ -89,14 +86,12 @@ export const moveChannelUpwards = <
8986 const targetChannelExistsWithinList = targetChannelIndex >= 0 ;
9087 const targetChannelAlreadyAtTheTop = targetChannelIndex === 0 ;
9188
92- if ( targetChannelAlreadyAtTheTop ) return channels ;
89+ // pinned channels should not move within the list based on recent activity, channels which
90+ // receive messages and are not pinned should move upwards but only under the last pinned channel
91+ // in the list
92+ const considerPinnedChannels = shouldConsiderPinnedChannels ( sort ) ;
9393
94- // as position of pinned channels has to stay unchanged, we need to
95- // find last pinned channel in the list to move the target channel after
96- let lastPinnedChannelIndex : number | null = null ;
97- if ( considerPinnedChannels ) {
98- lastPinnedChannelIndex = findLastPinnedChannelIndex ( { channels } ) ;
99- }
94+ if ( targetChannelAlreadyAtTheTop ) return channels ;
10095
10196 const newChannels = [ ...channels ] ;
10297
@@ -105,6 +100,13 @@ export const moveChannelUpwards = <
105100 newChannels . splice ( targetChannelIndex , 1 ) ;
106101 }
107102
103+ // as position of pinned channels has to stay unchanged, we need to
104+ // find last pinned channel in the list to move the target channel after
105+ let lastPinnedChannelIndex : number | null = null ;
106+ if ( considerPinnedChannels ) {
107+ lastPinnedChannelIndex = findLastPinnedChannelIndex ( { channels : newChannels } ) ;
108+ }
109+
108110 // re-insert it at the new place (to specific index if pinned channels are considered)
109111 newChannels . splice (
110112 typeof lastPinnedChannelIndex === 'number' ? lastPinnedChannelIndex + 1 : 0 ,
0 commit comments