Skip to content

Commit cd560e7

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

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

src/components/ChannelList/hooks/useChannelListShape.ts

Lines changed: 23 additions & 27 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);
@@ -126,34 +131,24 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
126131

127132
if (
128133
// target channel is archived, filter is defined and is set to false
129-
(isTargetChannelArchived && considerArchivedChannels && !filters.archived) ||
134+
(considerArchivedChannels && isTargetChannelArchived && !filters.archived) ||
135+
(considerArchivedChannels && !isTargetChannelArchived && filters.archived) ||
130136
// target channel is pinned, sort is defined
131137
(isTargetChannelPinned && considerPinnedChannels) ||
132138
// list order is locked
133139
lockChannelOrder ||
134140
// target channel is not within the loaded list and loading from cache is disallowed
135141
(!targetChannelExistsWithinList && !allowNewMessagesFromUnfilteredChannels)
136142
) {
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-
});
143+
return currentChannels;
154144
}
155145

156-
return channels;
146+
return moveChannelUpwards({
147+
channels: currentChannels,
148+
channelToMove: targetChannel,
149+
channelToMoveIndexWithinChannels: targetChannelIndex,
150+
sort,
151+
});
157152
});
158153
},
159154
[client],
@@ -272,7 +267,6 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
272267
return;
273268
}
274269

275-
const member = event.member;
276270
const channelType = event.channel_type;
277271
const channelId = event.channel_id;
278272

@@ -287,6 +281,9 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
287281
const targetChannelIndex = currentChannels.indexOf(targetChannel);
288282
const targetChannelExistsWithinList = targetChannelIndex >= 0;
289283

284+
const isTargetChannelArchived = isChannelArchived(targetChannel);
285+
const isTargetChannelPinned = isChannelPinned(targetChannel);
286+
290287
// handle pinning
291288
if (!considerPinnedChannels || lockChannelOrder) return currentChannels;
292289

@@ -298,9 +295,8 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
298295

299296
// handle archiving (remove channel)
300297
if (
301-
considerArchivedChannels &&
302-
((typeof member.archived_at === 'string' && !filters.archived) ||
303-
(!member.archived_at && filters.archived))
298+
(considerArchivedChannels && isTargetChannelArchived && !filters.archived) ||
299+
(considerArchivedChannels && !isTargetChannelArchived && filters.archived)
304300
) {
305301
return newChannels;
306302
}
@@ -310,7 +306,7 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
310306
// calculate last pinned channel index only if `pinned_at` sort is set to
311307
// ascending order or if it's in descending order while the pin is being removed, otherwise
312308
// we move to the top (index 0)
313-
if (pinnedAtSort === 1 || (pinnedAtSort === -1 && !member.pinned_at)) {
309+
if (pinnedAtSort === 1 || (pinnedAtSort === -1 && !isTargetChannelPinned)) {
314310
lastPinnedChannelIndex = findLastPinnedChannelIndex({ channels: newChannels });
315311
}
316312

src/components/ChannelList/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ export const shouldConsiderArchivedChannels = <SCG extends ExtendableGenerics>(
179179
export const isChannelPinned = <SCG extends ExtendableGenerics>(channel: Channel<SCG>) => {
180180
if (!channel) return false;
181181

182-
const member = channel.state.membership;
182+
const membership = channel.state.membership;
183183

184-
return !!member?.pinned_at;
184+
return typeof membership.pinned_at === 'string';
185185
};
186186

187187
export const isChannelArchived = <SCG extends ExtendableGenerics>(channel: Channel<SCG>) => {
188188
if (!channel) return false;
189189

190-
const member = channel.state.membership;
190+
const membership = channel.state.membership;
191191

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

0 commit comments

Comments
 (0)