Skip to content

Commit ab0f5b2

Browse files
Simplify conditions, fix message.new handler
1 parent 8124b85 commit ab0f5b2

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

src/components/ChannelList/hooks/useChannelListShape.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/components/ChannelList/utils.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ type MoveChannelUpwardsParams<SCG extends DefaultStreamChatGenerics = DefaultStr
6767
channelToMoveIndexWithinChannels?: number;
6868
};
6969

70-
/**
71-
* This function should not be used to move pinned already channels.
72-
*/
7370
export const moveChannelUpwards = <
7471
SCG extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
7572
>({
@@ -90,8 +87,11 @@ export const moveChannelUpwards = <
9087
// receive messages and are not pinned should move upwards but only under the last pinned channel
9188
// in the list
9289
const considerPinnedChannels = shouldConsiderPinnedChannels(sort);
90+
const isTargetChannelPinned = isChannelPinned(channelToMove);
9391

94-
if (targetChannelAlreadyAtTheTop) return channels;
92+
if (targetChannelAlreadyAtTheTop || (considerPinnedChannels && isTargetChannelPinned)) {
93+
return channels;
94+
}
9595

9696
const newChannels = [...channels];
9797

@@ -118,7 +118,9 @@ export const moveChannelUpwards = <
118118
};
119119

120120
/**
121-
* Returns `true` only if `{ pinned_at: -1 }` or `{ pinned_at: 1 }` option is first within the `sort` array or if `pinned_at` key of the `sort` object gets picked first when using `for...in`.
121+
* Returns `true` only if object with `pinned_at` property is first within the `sort` array
122+
* or if `pinned_at` key of the `sort` object gets picked first when using `for...in` looping mechanism
123+
* and value of the `pinned_at` is either `1` or `-1`.
122124
*/
123125
export const shouldConsiderPinnedChannels = <SCG extends ExtendableGenerics>(
124126
sort: ChannelListProps<SCG>['sort'],
@@ -166,7 +168,7 @@ export const extractSortValue = <SCG extends ExtendableGenerics>({
166168
};
167169

168170
/**
169-
* Returns `true` only if `archived` property is set to `false` within `filters`.
171+
* Returns `true` only if `archived` property is of type `boolean` within `filters` object.
170172
*/
171173
export const shouldConsiderArchivedChannels = <SCG extends ExtendableGenerics>(
172174
filters: ChannelListProps<SCG>['filters'],
@@ -176,18 +178,24 @@ export const shouldConsiderArchivedChannels = <SCG extends ExtendableGenerics>(
176178
return typeof filters.archived === 'boolean';
177179
};
178180

181+
/**
182+
* Returns `true` only if `pinned_at` property is of type `string` within `membership` object.
183+
*/
179184
export const isChannelPinned = <SCG extends ExtendableGenerics>(channel: Channel<SCG>) => {
180185
if (!channel) return false;
181186

182-
const member = channel.state.membership;
187+
const membership = channel.state.membership;
183188

184-
return !!member?.pinned_at;
189+
return typeof membership.pinned_at === 'string';
185190
};
186191

192+
/**
193+
* Returns `true` only if `archived_at` property is of type `string` within `membership` object.
194+
*/
187195
export const isChannelArchived = <SCG extends ExtendableGenerics>(channel: Channel<SCG>) => {
188196
if (!channel) return false;
189197

190-
const member = channel.state.membership;
198+
const membership = channel.state.membership;
191199

192-
return !!member?.archived_at;
200+
return typeof membership.archived_at === 'string';
193201
};

0 commit comments

Comments
 (0)