Skip to content

Commit f99d602

Browse files
authored
fix: channel.visible not taking sort and pinned channels into account (#2925)
* fix: channel.visible not taking sort and pinned channels into account * fix: notification.message_new not respecting pinning as well * fix: broken test * chore: remove console.log * fix: duplicate channels on many notification events
1 parent f1c40af commit f99d602

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

package/src/__tests__/offline-support/offline-feature.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,8 @@ export const Generic = () => {
381381
channels.push(newChannel);
382382
useMockedApis(chatClient, [getOrCreateChannelApi(newChannel)]);
383383

384+
await act(() => dispatchNotificationMessageNewEvent(chatClient, newChannel.channel));
384385
await waitFor(() => {
385-
act(() => dispatchNotificationMessageNewEvent(chatClient, newChannel.channel));
386-
387386
const channelIdsOnUI = screen
388387
.queryAllByLabelText('list-item')
389388
.map((node) => node._fiber.pendingProps.testID);

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: 19 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,23 @@ 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+
sort,
52+
})
53+
: channels,
54+
);
4155
}
4256
}
4357
};

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
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';
@@ -10,7 +8,7 @@ import type {
108
ChannelListEventListenerOptions,
119
DefaultStreamChatGenerics,
1210
} from '../../../../types/types';
13-
import { getChannel } from '../../utils';
11+
import { getChannel, moveChannelUp } from '../../utils';
1412
import { isChannelArchived } from '../utils';
1513

1614
type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
@@ -39,7 +37,7 @@ export const useNewMessageNotification = <
3937
onNewMessageNotification(setChannels, event, options);
4038
} else {
4139
if (!options) return;
42-
const { filters } = options;
40+
const { filters, sort } = options;
4341
if (event.channel?.id && event.channel?.type) {
4442
const channel = await getChannel({
4543
client,
@@ -53,7 +51,15 @@ export const useNewMessageNotification = <
5351
return;
5452
}
5553

56-
setChannels((channels) => (channels ? uniqBy([channel, ...channels], 'cid') : channels));
54+
setChannels((channels) =>
55+
channels
56+
? moveChannelUp({
57+
channels,
58+
channelToMove: channel,
59+
sort,
60+
})
61+
: channels,
62+
);
5763
}
5864
}
5965
};

0 commit comments

Comments
 (0)