Skip to content

Commit e568a3e

Browse files
Fixes and adjustments
1 parent d92478d commit e568a3e

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

src/components/ChannelList/ChannelList.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { ChannelListMessenger, ChannelListMessengerProps } from './ChannelListMe
55
import { useConnectionRecoveredListener } from './hooks/useConnectionRecoveredListener';
66
import { useMobileNavigation } from './hooks/useMobileNavigation';
77
import { CustomQueryChannelsFn, usePaginatedChannels } from './hooks/usePaginatedChannels';
8-
import {
9-
MAX_QUERY_CHANNELS_LIMIT,
10-
moveChannelUpwards,
11-
shouldConsiderPinnedChannels,
12-
} from './utils';
8+
import { MAX_QUERY_CHANNELS_LIMIT, moveChannelUpwards } from './utils';
139

1410
import { Avatar as DefaultAvatar } from '../Avatar';
1511
import { ChannelPreview, ChannelPreviewUIComponentProps } from '../ChannelPreview/ChannelPreview';
@@ -237,8 +233,7 @@ const UnMemoizedChannelList = <SCG extends DefaultStreamChatGenerics = DefaultSt
237233
const newChannels = moveChannelUpwards({
238234
channels,
239235
channelToMove: customActiveChannelObject,
240-
// TODO: adjust acordingly (based on sort)
241-
considerPinnedChannels: shouldConsiderPinnedChannels(sort),
236+
sort,
242237
});
243238

244239
setChannels(newChannels);

src/components/ChannelList/hooks/useChannelListShape.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
146146
channels,
147147
channelToMove,
148148
channelToMoveIndexWithinChannels: targetChannelIndex,
149-
considerPinnedChannels,
149+
sort,
150150
});
151151
}
152152

@@ -169,29 +169,31 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
169169
return customHandler(setChannels, event);
170170
}
171171

172-
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
173-
const considerPinnedChannels = shouldConsiderPinnedChannels(sort);
174-
175-
if (!event.channel?.type) return;
172+
if (!event.channel) {
173+
return;
174+
}
176175

177176
const channel = await getChannel({
178177
client,
179178
id: event.channel.id,
180179
type: event.channel.type,
181180
});
182181

182+
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
183183
if (isChannelArchived(channel) && considerArchivedChannels) {
184184
return;
185185
}
186186

187-
if (!allowNewMessagesFromUnfilteredChannels) return;
187+
if (!allowNewMessagesFromUnfilteredChannels) {
188+
return;
189+
}
188190

189191
setChannels((channels) =>
190192
moveChannelUpwards({
191193
channels,
192194
channelToMove: channel,
193195
channelToMoveIndexWithinChannels: -1,
194-
considerPinnedChannels,
196+
sort,
195197
}),
196198
);
197199
},
@@ -249,32 +251,44 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
249251
return;
250252
}
251253

254+
const member = event.member;
252255
const channelType = event.channel_type;
253256
const channelId = event.channel_id;
254257

255258
const considerPinnedChannels = shouldConsiderPinnedChannels(sort);
256259

260+
// TODO: extract this and consider single property sort object too
261+
const pinnedAtSort = Array.isArray(sort) ? (sort[0]?.pinned_at ?? null) : null;
262+
257263
setChannels((currentChannels) => {
258264
const targetChannel = client.channel(channelType, channelId);
259265
// assumes that channel instances are not changing
260266
const targetChannelIndex = currentChannels.indexOf(targetChannel);
261267
const targetChannelExistsWithinList = targetChannelIndex >= 0;
262268

269+
// handle pinning
270+
if (!considerPinnedChannels || lockChannelOrder) return currentChannels;
271+
263272
const newChannels = [...currentChannels];
264273

265274
if (targetChannelExistsWithinList) {
266275
newChannels.splice(targetChannelIndex, 1);
267276
}
268277

269-
// handle archiving
270-
if (typeof event.member?.archived_at === 'string') {
278+
// handle archiving (remove channel)
279+
if (typeof member.archived_at === 'string') {
271280
return newChannels;
272281
}
273282

274-
// handle pinning
275-
if (!considerPinnedChannels || lockChannelOrder) return currentChannels;
283+
let lastPinnedChannelIndex: number | null = null;
284+
285+
// calculate last pinned channel index only if `pinned_at` sort is set to
286+
// ascending order or if it's in descending order while the pin is being removed, otherwise
287+
// we move to the top (index 0)
288+
if (pinnedAtSort === 1 || (pinnedAtSort === -1 && !member.pinned_at)) {
289+
lastPinnedChannelIndex = findLastPinnedChannelIndex({ channels: newChannels });
290+
}
276291

277-
const lastPinnedChannelIndex = findLastPinnedChannelIndex({ channels: newChannels });
278292
const newTargetChannelIndex =
279293
typeof lastPinnedChannelIndex === 'number' ? lastPinnedChannelIndex + 1 : 0;
280294

src/components/ChannelList/utils.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import uniqBy from 'lodash.uniqby';
2-
import type { Channel, ExtendableGenerics } from 'stream-chat';
2+
import type { Channel, ChannelSort, ExtendableGenerics } from 'stream-chat';
33

44
import type { DefaultStreamChatGenerics } from '../../types/types';
55
import type { ChannelListProps } from './ChannelList';
@@ -59,27 +59,24 @@ export function findLastPinnedChannelIndex<SCG extends ExtendableGenerics>({
5959
type 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+
*/
7673
export 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

Comments
 (0)