Skip to content

Commit 0788344

Browse files
Reset behavior of the old handler
1 parent cc4ce90 commit 0788344

File tree

1 file changed

+16
-52
lines changed

1 file changed

+16
-52
lines changed

src/components/ChannelList/hooks/useMessageNewListener.ts

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,41 @@
11
import { useEffect } from 'react';
2-
import type { Dispatch, SetStateAction } from 'react';
3-
import type { Channel, Event, ExtendableGenerics } from 'stream-chat';
2+
import uniqBy from 'lodash.uniqby';
43

5-
import { moveChannelUpwards } from '../utils';
6-
import { useChatContext } from '../../../context/ChatContext';
7-
import type { DefaultStreamChatGenerics } from '../../../types/types';
4+
import { moveChannelUp } from '../utils';
85

9-
export const isChannelPinned = <SCG extends ExtendableGenerics>({
10-
channel,
11-
}: {
12-
channel?: Channel<SCG>;
13-
}) => {
14-
if (!channel) return false;
6+
import { useChatContext } from '../../../context/ChatContext';
157

16-
const member = channel.state.membership;
8+
import type { Channel, Event } from 'stream-chat';
179

18-
return !!member?.pinned_at;
19-
};
10+
import type { DefaultStreamChatGenerics } from '../../../types/types';
2011

2112
export const useMessageNewListener = <
22-
SCG extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
13+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
2314
>(
24-
setChannels: Dispatch<SetStateAction<Array<Channel<SCG>>>>,
15+
setChannels: React.Dispatch<React.SetStateAction<Array<Channel<StreamChatGenerics>>>>,
2516
customHandler?: (
26-
setChannels: Dispatch<SetStateAction<Array<Channel<SCG>>>>,
27-
event: Event<SCG>,
17+
setChannels: React.Dispatch<React.SetStateAction<Array<Channel<StreamChatGenerics>>>>,
18+
event: Event<StreamChatGenerics>,
2819
) => void,
2920
lockChannelOrder = false,
3021
allowNewMessagesFromUnfilteredChannels = true,
31-
considerPinnedChannels = false, // automatically set to true by checking sorting options (must include {pinned_at: -1/1})
3222
) => {
33-
const { client } = useChatContext<SCG>('useMessageNewListener');
23+
const { client } = useChatContext<StreamChatGenerics>('useMessageNewListener');
3424

3525
useEffect(() => {
36-
const handleEvent = (event: Event<SCG>) => {
26+
const handleEvent = (event: Event<StreamChatGenerics>) => {
3727
if (customHandler && typeof customHandler === 'function') {
3828
customHandler(setChannels, event);
3929
} else {
4030
setChannels((channels) => {
41-
const targetChannelIndex = channels.findIndex((channel) => channel.cid === event.cid);
42-
const targetChannelExistsWithinList = targetChannelIndex >= 0;
43-
44-
const isTargetChannelPinned = isChannelPinned({
45-
channel: channels[targetChannelIndex],
46-
});
31+
const channelInList = channels.filter((channel) => channel.cid === event.cid).length > 0;
4732

48-
if (
49-
// target channel is pinned
50-
(isTargetChannelPinned && considerPinnedChannels) ||
51-
// list order is locked
52-
lockChannelOrder ||
53-
// target channel is not within the loaded list and loading from cache is disallowed
54-
(!targetChannelExistsWithinList && !allowNewMessagesFromUnfilteredChannels)
55-
) {
56-
return channels;
33+
if (!channelInList && allowNewMessagesFromUnfilteredChannels && event.channel_type) {
34+
const channel = client.channel(event.channel_type, event.channel_id);
35+
return uniqBy([channel, ...channels], 'cid');
5736
}
5837

59-
// we either have the channel to move or we pull it from the cache (or instantiate) if it's allowed
60-
const channelToMove: Channel<SCG> | null =
61-
channels[targetChannelIndex] ??
62-
(allowNewMessagesFromUnfilteredChannels && event.channel_type
63-
? client.channel(event.channel_type, event.channel_id)
64-
: null);
65-
66-
if (channelToMove) {
67-
return moveChannelUpwards({
68-
channels,
69-
channelToMove,
70-
channelToMoveIndexWithinChannels: targetChannelIndex,
71-
considerPinnedChannels,
72-
});
73-
}
38+
if (!lockChannelOrder) return moveChannelUp({ channels, cid: event.cid || '' });
7439

7540
return channels;
7641
});
@@ -85,7 +50,6 @@ export const useMessageNewListener = <
8550
}, [
8651
allowNewMessagesFromUnfilteredChannels,
8752
client,
88-
considerPinnedChannels,
8953
customHandler,
9054
lockChannelOrder,
9155
setChannels,

0 commit comments

Comments
 (0)