Skip to content

Commit 0f2a0f9

Browse files
committed
fix: channel.visible not taking sort and pinned channels into account
1 parent 68f8b88 commit 0f2a0f9

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

package/src/components/ChannelList/ChannelList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ export const ChannelList = <
349349

350350
useChannelVisible({
351351
onChannelVisible,
352+
options: { sort },
352353
setChannels,
353354
});
354355

package/src/components/ChannelList/hooks/listeners/useChannelVisible.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
import { useEffect } from 'react';
22

3-
import uniqBy from 'lodash/uniqBy';
4-
53
import type { Channel, Event } from 'stream-chat';
64

75
import { useChatContext } from '../../../../contexts/chatContext/ChatContext';
86

9-
import type { DefaultStreamChatGenerics } from '../../../../types/types';
10-
import { getChannel } from '../../utils';
7+
import type {
8+
ChannelListEventListenerOptions,
9+
DefaultStreamChatGenerics,
10+
} from '../../../../types/types';
11+
import { getChannel, moveChannelUp } from '../../utils';
1112

1213
type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
1314
{
1415
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>;
1516
onChannelVisible?: (
1617
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
1718
event: Event<StreamChatGenerics>,
19+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
1820
) => void;
21+
options?: ChannelListEventListenerOptions<StreamChatGenerics>;
1922
};
2023

2124
export const useChannelVisible = <
2225
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2326
>({
2427
onChannelVisible,
28+
options,
2529
setChannels,
2630
}: Parameters<StreamChatGenerics>) => {
2731
const { client } = useChatContext<StreamChatGenerics>();
@@ -31,13 +35,24 @@ export const useChannelVisible = <
3135
if (typeof onChannelVisible === 'function') {
3236
onChannelVisible(setChannels, event);
3337
} else {
38+
if (!options) return;
39+
const { sort } = options;
3440
if (event.channel_id && event.channel_type) {
3541
const channel = await getChannel<StreamChatGenerics>({
3642
client,
3743
id: event.channel_id,
3844
type: event.channel_type,
3945
});
40-
setChannels((channels) => (channels ? uniqBy([channel, ...channels], 'cid') : channels));
46+
setChannels((channels) =>
47+
channels
48+
? moveChannelUp({
49+
channels,
50+
channelToMove: channel,
51+
channelToMoveIndexWithinChannels: -1,
52+
sort,
53+
})
54+
: channels,
55+
);
4156
}
4257
}
4358
};

0 commit comments

Comments
 (0)