Skip to content

Commit 1c7639a

Browse files
Fix channel.visible handler
1 parent 6f2de4e commit 1c7639a

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/components/ChannelList/hooks/useChannelListShape.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useRef } from 'react';
44
import { Channel, Event, ExtendableGenerics } from 'stream-chat';
5-
import uniqBy from 'lodash.uniqby';
65

76
import {
87
extractSortValue,
@@ -68,7 +67,9 @@ type HandleChannelHiddenParameters<SCG extends ExtendableGenerics> = BaseParamet
6867
RepeatedParameters<SCG>;
6968

7069
type HandleChannelVisibleParameters<SCG extends ExtendableGenerics> =
71-
BaseParameters<SCG> & RepeatedParameters<SCG>;
70+
BaseParameters<SCG> &
71+
RepeatedParameters<SCG> &
72+
Required<Pick<ChannelListProps<SCG>, 'sort' | 'filters'>>;
7273

7374
type HandleChannelTruncatedParameters<SCG extends ExtendableGenerics> =
7475
BaseParameters<SCG> & RepeatedParameters<SCG>;
@@ -194,7 +195,6 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
194195
moveChannelUpwards({
195196
channels,
196197
channelToMove: channel,
197-
channelToMoveIndexWithinChannels: -1,
198198
sort,
199199
}),
200200
);
@@ -353,20 +353,36 @@ export const useChannelListShapeDefaults = <SCG extends ExtendableGenerics>() =>
353353
async ({
354354
customHandler,
355355
event,
356+
filters,
356357
setChannels,
358+
sort,
357359
}: HandleChannelVisibleParameters<SCG>) => {
358360
if (typeof customHandler === 'function') {
359361
return customHandler(setChannels, event);
360362
}
361363

362-
if (event.type && event.channel_type && event.channel_id) {
363-
const channel = await getChannel({
364-
client,
365-
id: event.channel_id,
366-
type: event.channel_type,
367-
});
368-
setChannels((channels) => uniqBy([channel, ...channels], 'cid'));
364+
if (!event.channel) {
365+
return;
369366
}
367+
368+
const channel = await getChannel({
369+
client,
370+
id: event.channel.id,
371+
type: event.channel.type,
372+
});
373+
374+
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
375+
if (isChannelArchived(channel) && considerArchivedChannels && !filters.archived) {
376+
return;
377+
}
378+
379+
setChannels((channels) =>
380+
moveChannelUpwards({
381+
channels,
382+
channelToMove: channel,
383+
sort,
384+
}),
385+
);
370386
},
371387
[client],
372388
);
@@ -586,7 +602,9 @@ export const usePrepareShapeHandlers = <SCG extends ExtendableGenerics>({
586602
defaults.handleChannelVisible({
587603
customHandler: onChannelVisible,
588604
event,
605+
filters,
589606
setChannels,
607+
sort,
590608
});
591609
break;
592610
case 'channel.truncated':

0 commit comments

Comments
 (0)