Skip to content

Commit e7e226b

Browse files
committed
fix: add options for channel list event listeners
1 parent 12e4734 commit e7e226b

File tree

6 files changed

+67
-49
lines changed

6 files changed

+67
-49
lines changed

package/src/components/ChannelList/ChannelList.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from '../../contexts/channelsContext/ChannelsContext';
3131
import { useChatContext } from '../../contexts/chatContext/ChatContext';
3232
import { upsertCidsForQuery } from '../../store/apis/upsertCidsForQuery';
33-
import type { DefaultStreamChatGenerics } from '../../types/types';
33+
import type { ChannelListEventListenerOptions, DefaultStreamChatGenerics } from '../../types/types';
3434
import { ChannelPreviewMessenger } from '../ChannelPreview/ChannelPreviewMessenger';
3535
import { EmptyStateIndicator as EmptyStateIndicatorDefault } from '../Indicators/EmptyStateIndicator';
3636
import { LoadingErrorIndicator as LoadingErrorIndicatorDefault } from '../Indicators/LoadingErrorIndicator';
@@ -98,8 +98,7 @@ export type ChannelListProps<
9898
onAddedToChannel?: (
9999
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
100100
event: Event<StreamChatGenerics>,
101-
filters?: ChannelFilters<StreamChatGenerics>,
102-
sort?: ChannelSort<StreamChatGenerics>,
101+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
103102
) => void;
104103
/**
105104
* Function that overrides default behavior when a channel gets deleted. In absence of this prop, the channel will be removed from the list.
@@ -138,8 +137,7 @@ export type ChannelListProps<
138137
lockChannelOrder: boolean,
139138
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
140139
event: Event<StreamChatGenerics>,
141-
filters?: ChannelFilters<StreamChatGenerics>,
142-
sort?: ChannelSort<StreamChatGenerics>,
140+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
143141
) => void;
144142
/**
145143
* Function to customize behavior when a channel gets truncated
@@ -193,8 +191,7 @@ export type ChannelListProps<
193191
lockChannelOrder: boolean,
194192
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
195193
event: Event<StreamChatGenerics>,
196-
filters?: ChannelFilters<StreamChatGenerics>,
197-
sort?: ChannelSort<StreamChatGenerics>,
194+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
198195
) => void;
199196
/**
200197
* Override the default listener/handler for event `notification.message_new`
@@ -208,7 +205,7 @@ export type ChannelListProps<
208205
onNewMessageNotification?: (
209206
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
210207
event: Event<StreamChatGenerics>,
211-
filters?: ChannelFilters<StreamChatGenerics>,
208+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
212209
) => void;
213210

214211
/**
@@ -316,10 +313,9 @@ export const ChannelList = <
316313

317314
// Setup event listeners
318315
useAddedToChannelNotification({
319-
filters,
320316
onAddedToChannel,
317+
options: { filters, sort },
321318
setChannels,
322-
sort,
323319
});
324320

325321
useChannelDeleted({
@@ -333,11 +329,10 @@ export const ChannelList = <
333329
});
334330

335331
useChannelMemberUpdated({
336-
filters,
337332
lockChannelOrder,
338333
onChannelMemberUpdated,
334+
options: { filters, sort },
339335
setChannels,
340-
sort,
341336
});
342337

343338
useChannelTruncated({
@@ -358,16 +353,15 @@ export const ChannelList = <
358353
});
359354

360355
useNewMessage({
361-
filters,
362356
lockChannelOrder,
363357
onNewMessage,
358+
options: { filters, sort },
364359
setChannels,
365-
sort,
366360
});
367361

368362
useNewMessageNotification({
369-
filters,
370363
onNewMessageNotification,
364+
options: { filters, sort },
371365
setChannels,
372366
});
373367

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

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

33
import uniqBy from 'lodash/uniqBy';
44

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

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

9-
import type { DefaultStreamChatGenerics } from '../../../../types/types';
9+
import type {
10+
ChannelListEventListenerOptions,
11+
DefaultStreamChatGenerics,
12+
} from '../../../../types/types';
1013
import { getChannel } from '../../utils';
1114
import { findLastPinnedChannelIndex, findPinnedAtSortOrder } from '../utils';
1215

1316
type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
1417
{
1518
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>;
16-
filters?: ChannelFilters<StreamChatGenerics>;
1719
onAddedToChannel?: (
1820
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
1921
event: Event<StreamChatGenerics>,
20-
filters?: ChannelFilters<StreamChatGenerics>,
21-
sort?: ChannelSort<StreamChatGenerics>,
22+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
2223
) => void;
23-
sort?: ChannelSort<StreamChatGenerics>;
24+
options?: ChannelListEventListenerOptions<StreamChatGenerics>;
2425
};
2526

2627
export const useAddedToChannelNotification = <
2728
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2829
>({
29-
filters,
3030
onAddedToChannel,
31+
options,
3132
setChannels,
32-
sort,
3333
}: Parameters<StreamChatGenerics>) => {
3434
const { client } = useChatContext<StreamChatGenerics>();
3535

3636
useEffect(() => {
3737
const handleEvent = async (event: Event<StreamChatGenerics>) => {
3838
if (typeof onAddedToChannel === 'function') {
39-
onAddedToChannel(setChannels, event, filters, sort);
39+
onAddedToChannel(setChannels, event, options);
4040
} else {
41+
if (!options) return;
42+
const { sort } = options;
4143
if (event.channel?.id && event.channel?.type) {
4244
const channel = await getChannel<StreamChatGenerics>({
4345
client,

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { useEffect } from 'react';
22

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

55
import { useChatContext } from '../../../../contexts/chatContext/ChatContext';
66

7-
import type { DefaultStreamChatGenerics } from '../../../../types/types';
7+
import type {
8+
ChannelListEventListenerOptions,
9+
DefaultStreamChatGenerics,
10+
} from '../../../../types/types';
811
import {
912
findLastPinnedChannelIndex,
1013
findPinnedAtSortOrder,
@@ -18,33 +21,32 @@ type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultSt
1821
{
1922
lockChannelOrder: boolean;
2023
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>;
21-
filters?: ChannelFilters<StreamChatGenerics>;
2224
onChannelMemberUpdated?: (
2325
lockChannelOrder: boolean,
2426
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
2527
event: Event<StreamChatGenerics>,
26-
filters?: ChannelFilters<StreamChatGenerics>,
27-
sort?: ChannelSort<StreamChatGenerics>,
28+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
2829
) => void;
29-
sort?: ChannelSort<StreamChatGenerics>;
30+
options?: ChannelListEventListenerOptions<StreamChatGenerics>;
3031
};
3132

3233
export const useChannelMemberUpdated = <
3334
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
3435
>({
35-
filters,
3636
lockChannelOrder,
3737
onChannelMemberUpdated,
38+
options,
3839
setChannels,
39-
sort,
4040
}: Parameters<StreamChatGenerics>) => {
4141
const { client } = useChatContext<StreamChatGenerics>();
4242

4343
useEffect(() => {
4444
const handleEvent = (event: Event<StreamChatGenerics>) => {
4545
if (typeof onChannelMemberUpdated === 'function') {
46-
onChannelMemberUpdated(lockChannelOrder, setChannels, event, filters, sort);
46+
onChannelMemberUpdated(lockChannelOrder, setChannels, event, options);
4747
} else {
48+
if (!options) return;
49+
const { filters, sort } = options;
4850
if (!event.member?.user || event.member.user.id !== client.userID || !event.channel_type) {
4951
return;
5052
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { useEffect } from 'react';
22

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

55
import { useChatContext } from '../../../../contexts/chatContext/ChatContext';
66

7-
import type { DefaultStreamChatGenerics } from '../../../../types/types';
7+
import type {
8+
ChannelListEventListenerOptions,
9+
DefaultStreamChatGenerics,
10+
} from '../../../../types/types';
811
import { moveChannelUp } from '../../utils';
912
import {
1013
isChannelArchived,
@@ -17,33 +20,32 @@ type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultSt
1720
{
1821
lockChannelOrder: boolean;
1922
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>;
20-
filters?: ChannelFilters<StreamChatGenerics>;
2123
onNewMessage?: (
2224
lockChannelOrder: boolean,
2325
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
2426
event: Event<StreamChatGenerics>,
25-
filters?: ChannelFilters<StreamChatGenerics>,
26-
sort?: ChannelSort<StreamChatGenerics>,
27+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
2728
) => void;
28-
sort?: ChannelSort<StreamChatGenerics>;
29+
options?: ChannelListEventListenerOptions<StreamChatGenerics>;
2930
};
3031

3132
export const useNewMessage = <
3233
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
3334
>({
34-
filters,
3535
lockChannelOrder,
3636
onNewMessage,
37+
options,
3738
setChannels,
38-
sort,
3939
}: Parameters<StreamChatGenerics>) => {
4040
const { client } = useChatContext<StreamChatGenerics>();
4141

4242
useEffect(() => {
4343
const handleEvent = (event: Event<StreamChatGenerics>) => {
4444
if (typeof onNewMessage === 'function') {
45-
onNewMessage(lockChannelOrder, setChannels, event, filters, sort);
45+
onNewMessage(lockChannelOrder, setChannels, event, options);
4646
} else {
47+
if (!options) return;
48+
const { filters, sort } = options;
4749
const considerPinnedChannels = shouldConsiderPinnedChannels(sort);
4850
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
4951

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,44 @@ import { useEffect } from 'react';
22

33
import uniqBy from 'lodash/uniqBy';
44

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

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

9-
import type { DefaultStreamChatGenerics } from '../../../../types/types';
9+
import type {
10+
ChannelListEventListenerOptions,
11+
DefaultStreamChatGenerics,
12+
} from '../../../../types/types';
1013
import { getChannel } from '../../utils';
1114
import { isChannelArchived } from '../utils';
1215

1316
type Parameters<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
1417
{
1518
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>;
16-
filters?: ChannelFilters<StreamChatGenerics>;
1719
onNewMessageNotification?: (
1820
setChannels: React.Dispatch<React.SetStateAction<Channel<StreamChatGenerics>[] | null>>,
1921
event: Event<StreamChatGenerics>,
20-
filters?: ChannelFilters<StreamChatGenerics>,
22+
options?: ChannelListEventListenerOptions<StreamChatGenerics>,
2123
) => void;
24+
options?: ChannelListEventListenerOptions<StreamChatGenerics>;
2225
};
2326

2427
export const useNewMessageNotification = <
2528
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2629
>({
27-
filters,
2830
onNewMessageNotification,
31+
options,
2932
setChannels,
3033
}: Parameters<StreamChatGenerics>) => {
3134
const { client } = useChatContext<StreamChatGenerics>();
3235

3336
useEffect(() => {
3437
const handleEvent = async (event: Event<StreamChatGenerics>) => {
3538
if (typeof onNewMessageNotification === 'function') {
36-
onNewMessageNotification(setChannels, event, filters);
39+
onNewMessageNotification(setChannels, event, options);
3740
} else {
41+
if (!options) return;
42+
const { filters } = options;
3843
if (event.channel?.id && event.channel?.type) {
3944
const channel = await getChannel({
4045
client,

package/src/types/types.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { ChannelState, ExtendableGenerics, LiteralStringForUnion } from 'stream-chat';
1+
import type {
2+
ChannelFilters,
3+
ChannelSort,
4+
ChannelState,
5+
ExtendableGenerics,
6+
LiteralStringForUnion,
7+
} from 'stream-chat';
28

39
import type { FileStateValue } from '../utils/utils';
410

@@ -97,6 +103,13 @@ export interface DefaultStreamChatGenerics extends ExtendableGenerics {
97103
userType: DefaultUserType;
98104
}
99105

106+
export type ChannelListEventListenerOptions<
107+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
108+
> = {
109+
filters?: ChannelFilters<StreamChatGenerics>;
110+
sort?: ChannelSort<StreamChatGenerics>;
111+
};
112+
100113
export type UnknownType = Record<string, unknown>;
101114

102115
export type ValueOf<T> = T[keyof T];

0 commit comments

Comments
 (0)