Skip to content

Commit 20e403a

Browse files
committed
feat: handle on added to channel event
1 parent 17a5def commit 20e403a

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

package/src/components/ChannelList/ChannelList.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,16 @@ export type ChannelListProps<
9090
*
9191
* @param setChannels Setter for internal state property - `channels`. It's created from useState() hook.
9292
* @param event An [Event Object](https://getstream.io/chat/docs/event_object) corresponding to `notification.added_to_channel` event
93+
* @param filters Channel filters
94+
* @param sort Channel sort options
9395
*
9496
* @overrideType Function
9597
* */
9698
onAddedToChannel?: (
9799
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
98100
event: Event<StreamChatGenerics>,
101+
filters?: ChannelFilters<StreamChatGenerics>,
102+
sort?: ChannelSort<StreamChatGenerics>,
99103
) => void;
100104
/**
101105
* Function that overrides default behavior when a channel gets deleted. In absence of this prop, the channel will be removed from the list.
@@ -314,6 +318,8 @@ export const ChannelList = <
314318
useAddedToChannelNotification({
315319
onAddedToChannel,
316320
setChannels,
321+
filters,
322+
sort,
317323
});
318324

319325
useChannelDeleted({

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,70 @@ import { useEffect } from 'react';
22

33
import uniqBy from 'lodash/uniqBy';
44

5-
import type { Channel, Event } from 'stream-chat';
5+
import type { Channel, ChannelFilters, ChannelSort, Event } from 'stream-chat';
66

77
import { useChatContext } from '../../../../contexts/chatContext/ChatContext';
88

99
import type { DefaultStreamChatGenerics } from '../../../../types/types';
1010
import { getChannel } from '../../utils';
11+
import { findLastPinnedChannelIndex, findPinnedAtSortOrder } from '../utils';
1112

1213
type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
1314
{
1415
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>;
1516
onAddedToChannel?: (
1617
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
1718
event: Event<StreamChatGenerics>,
19+
filters?: ChannelFilters<StreamChatGenerics>,
20+
sort?: ChannelSort<StreamChatGenerics>,
1821
) => void;
22+
filters?: ChannelFilters<StreamChatGenerics>;
23+
sort?: ChannelSort<StreamChatGenerics>;
1924
};
2025

2126
export const useAddedToChannelNotification = <
2227
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2328
>({
2429
onAddedToChannel,
2530
setChannels,
31+
filters,
32+
sort,
2633
}: Parameters<StreamChatGenerics>) => {
2734
const { client } = useChatContext<StreamChatGenerics>();
2835

2936
useEffect(() => {
3037
const handleEvent = async (event: Event<StreamChatGenerics>) => {
3138
if (typeof onAddedToChannel === 'function') {
32-
onAddedToChannel(setChannels, event);
39+
onAddedToChannel(setChannels, event, filters, sort);
3340
} else {
3441
if (event.channel?.id && event.channel?.type) {
3542
const channel = await getChannel<StreamChatGenerics>({
3643
client,
3744
id: event.channel.id,
3845
type: event.channel.type,
3946
});
40-
setChannels((channels) => (channels ? uniqBy([channel, ...channels], 'cid') : channels));
47+
48+
const pinnedAtSort = findPinnedAtSortOrder({ sort });
49+
50+
setChannels((channels) => {
51+
if (!channels) return channels;
52+
53+
// handle pinning
54+
let lastPinnedChannelIndex: number | null = null;
55+
56+
const newChannels = [...channels];
57+
58+
if (pinnedAtSort === 1 || pinnedAtSort === -1) {
59+
lastPinnedChannelIndex = findLastPinnedChannelIndex({ channels: newChannels });
60+
const newTargetChannelIndex =
61+
typeof lastPinnedChannelIndex === 'number' ? lastPinnedChannelIndex + 1 : 0;
62+
63+
newChannels.splice(newTargetChannelIndex, 0, channel);
64+
return newChannels;
65+
}
66+
67+
return uniqBy([channel, ...channels], 'cid');
68+
});
4169
}
4270
}
4371
};

0 commit comments

Comments
 (0)