-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathChannelListItem.tsx
More file actions
211 lines (189 loc) Β· 7.29 KB
/
ChannelListItem.tsx
File metadata and controls
211 lines (189 loc) Β· 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import throttle from 'lodash.throttle';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import type { ReactNode } from 'react';
import type { Channel, Event, LocalMessage } from 'stream-chat';
import { ChannelListItemUI as DefaultChannelListItemUI } from './ChannelListItemUI';
import { useIsChannelMuted } from './hooks/useIsChannelMuted';
import { useChannelPreviewInfo } from './hooks/useChannelPreviewInfo';
import { getLatestMessagePreview as defaultGetLatestMessagePreview } from './utils';
import { useTranslationContext } from '../../context/TranslationContext';
import { useMessageDeliveryStatus } from './hooks/useMessageDeliveryStatus';
import type { MessageDeliveryStatus } from './hooks/useMessageDeliveryStatus';
import type { GroupChannelDisplayInfo } from './utils';
import {
type ChatContextValue,
type TranslationContextValue,
useChatContext,
useComponentContext,
} from '../../context';
export type ChannelListItemUIProps = ChannelListItemProps & {
/** Image of Channel to display */
displayImage?: string;
/** Title of Channel to display */
displayTitle?: string;
/** Title of Channel to display */
groupChannelDisplayInfo?: GroupChannelDisplayInfo;
/** The last message received in a channel */
lastMessage?: LocalMessage;
/** Latest message preview to display, will be a string or JSX element supporting markdown. */
latestMessagePreview?: ReactNode;
/** Status describing whether own message has been delivered or read by another. If the last message is not an own message, then the status is undefined. */
messageDeliveryStatus?: MessageDeliveryStatus;
/** Whether the channel is muted by the current user */
muted?: boolean;
/** Number of unread Messages */
unread?: number;
};
export type ChannelListItemProps = {
/** Comes from either the `channelRenderFilterFn` or `usePaginatedChannels` call from [ChannelList](https://github.com/GetStream/stream-chat-react/blob/master/src/components/ChannelList/ChannelList.tsx) */
channel: Channel;
/** If the component's channel is the active (selected) Channel */
active?: boolean;
/** Current selected channel object */
activeChannel?: Channel;
/** Forces the update of preview component on channel update */
channelUpdateCount?: number;
/** Custom class for the channel preview root */
className?: string;
/** Custom function that generates the message preview in ChannelListItem component */
getLatestMessagePreview?: (
channel: Channel,
t: TranslationContextValue['t'],
userLanguage: TranslationContextValue['userLanguage'],
isMessageAIGenerated: ChatContextValue['isMessageAIGenerated'],
) => ReactNode;
key?: string;
/** Custom ChannelListItem click handler function */
onSelect?: (event: React.MouseEvent) => void;
/** Setter for selected Channel */
setActiveChannel?: ChatContextValue['setActiveChannel'];
/** Object containing watcher parameters */
watchers?: { limit?: number; offset?: number };
};
const ChannelListItemContext = React.createContext<{ channel: Channel }>({
channel: null as unknown as Channel,
});
export const useChannelListItemContext = () => useContext(ChannelListItemContext);
export const ChannelListItem = (props: ChannelListItemProps) => {
const {
active,
channel,
channelUpdateCount,
getLatestMessagePreview = defaultGetLatestMessagePreview,
} = props;
const { ChannelListItemUI = DefaultChannelListItemUI } = useComponentContext();
const {
channel: activeChannel,
client,
isMessageAIGenerated,
setActiveChannel,
} = useChatContext('ChannelPreview');
const { t, userLanguage } = useTranslationContext('ChannelPreview');
const { displayImage, displayTitle, groupChannelDisplayInfo } = useChannelPreviewInfo({
channel,
});
const [lastMessage, setLastMessage] = useState<LocalMessage>(
channel.state.messages[channel.state.messages.length - 1],
);
const [latestMessagePreview, setLatestMessagePreview] = useState<ReactNode>(() =>
getLatestMessagePreview(channel, t, userLanguage, isMessageAIGenerated),
);
const [unread, setUnread] = useState(0);
const { messageDeliveryStatus } = useMessageDeliveryStatus({
channel,
lastMessage,
});
const isActive =
typeof active === 'undefined' ? activeChannel?.cid === channel.cid : active;
const { muted } = useIsChannelMuted(channel);
useEffect(() => {
const handleEvent = (event: Event) => {
if (!event.cid) return setUnread(0);
if (channel.cid === event.cid) setUnread(0);
};
client.on('notification.mark_read', handleEvent);
return () => client.off('notification.mark_read', handleEvent);
}, [channel, client]);
useEffect(() => {
const handleEvent = (event: Event) => {
if (channel.cid !== event.cid) return;
if (event.user?.id !== client.user?.id) return;
setUnread(channel.countUnread());
};
channel.on('notification.mark_unread', handleEvent);
return () => {
channel.off('notification.mark_unread', handleEvent);
};
}, [channel, client]);
const refreshUnreadCount = useMemo(
() =>
throttle(() => {
if (muted) {
setUnread(0);
} else {
setUnread(channel.countUnread());
}
}, 400),
[channel, muted],
);
useEffect(() => {
refreshUnreadCount();
setLatestMessagePreview(
getLatestMessagePreview(channel, t, userLanguage, isMessageAIGenerated),
);
const handleEvent = (event: Event) => {
const deletedMessagesInAnotherChannel =
event.type === 'user.messages.deleted' && event.cid && event.cid !== channel.cid;
if (deletedMessagesInAnotherChannel) return;
setLastMessage(
channel.state.latestMessages[channel.state.latestMessages.length - 1],
);
setLatestMessagePreview(
getLatestMessagePreview(channel, t, userLanguage, isMessageAIGenerated),
);
refreshUnreadCount();
};
channel.on('message.new', handleEvent);
channel.on('message.updated', handleEvent);
channel.on('message.deleted', handleEvent);
client.on('user.messages.deleted', handleEvent);
channel.on('message.undeleted', handleEvent);
channel.on('channel.truncated', handleEvent);
return () => {
channel.off('message.new', handleEvent);
channel.off('message.updated', handleEvent);
channel.off('message.deleted', handleEvent);
client.off('user.messages.deleted', handleEvent);
channel.off('message.undeleted', handleEvent);
channel.off('channel.truncated', handleEvent);
};
}, [
channel,
client,
refreshUnreadCount,
channelUpdateCount,
getLatestMessagePreview,
t,
userLanguage,
isMessageAIGenerated,
]);
const channelPreviewContextValue = useMemo(() => ({ channel }), [channel]);
if (!ChannelListItemUI) return null;
return (
<ChannelListItemContext.Provider value={channelPreviewContextValue}>
<ChannelListItemUI
{...props}
active={isActive}
displayImage={displayImage}
displayTitle={displayTitle}
groupChannelDisplayInfo={groupChannelDisplayInfo}
lastMessage={lastMessage}
latestMessagePreview={latestMessagePreview}
messageDeliveryStatus={messageDeliveryStatus}
muted={muted}
setActiveChannel={setActiveChannel}
unread={unread}
/>
</ChannelListItemContext.Provider>
);
};