-
Notifications
You must be signed in to change notification settings - Fork 77
/
ChatThread.tsx
336 lines (313 loc) · 12.5 KB
/
ChatThread.tsx
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { Stack, TooltipHost, PrimaryButton, Icon } from '@fluentui/react';
import {
Chat,
MessageSeenIcon,
Flex,
Ref,
Button,
PresenceAvailableIcon,
PresenceStrokeIcon,
RedbangIcon
} from '@fluentui/react-northstar';
import React, { useEffect, useState, createRef, useRef } from 'react';
import { LiveAnnouncer, LiveMessage } from 'react-aria-live';
import { URL_REGEX, NUMBER_OF_MESSAGES_TO_LOAD } from '../constants';
import {
chatContainerStyle,
messageAvatarContainerStyle,
noReadReceiptStyle,
chatStyle,
chatMessageStyle,
newMessageButtonStyle,
loadMoreMessageButtonStyle,
readReceiptIconStyle,
DownIconStyle
} from './styles/ChatThread.styles';
import { User } from '../core/reducers/ContosoClientReducers';
import { ClientChatMessage } from '../core/reducers/MessagesReducer';
import { isUserMatchingIdentity } from '../utils/utils';
interface ChatThreadProps {
isYourLatestMessage(clientMessageId: string, messages: any[]): boolean;
isYourLatestSeenMessage(clientMessageId: string, MessagesWithSeen: any[]): boolean;
isLargeParticipantsGroup(): boolean;
isMessageSeen(clientMessageId: string, messages: any[]): boolean;
sendReadReceipt(messages: any[], userId: string): void;
messages: ClientChatMessage[];
user: User;
users: any;
failedMessages: string[];
}
// Reference: https://stackoverflow.com/questions/33235890/react-replace-links-in-a-text
const renderHyperlink = (text: string) => {
return text.split(' ').map((part) =>
URL_REGEX.test(part) ? (
<a href={part} target="_blank" rel="noopener noreferrer">
{part}{' '}
</a>
) : (
part + ' '
)
);
};
let createdRef: any = createRef();
let chatThreadRef: any = createRef();
// A Chatthread will be fed many messages so it will try to map out the messages out of the props and feed them into a
// Chat item
// We need to be smarter and figure out for the last N messages are they all of the same person or not?
export default (props: ChatThreadProps): JSX.Element => {
const [messagesWithAttached, setMessagesWithAttached] = useState<any[]>([]);
const [indexOfTheFirstMessage, setIndexOfTheFirstMessage] = useState(props.messages.length);
const [isAtBottomOfScroll, setIsAtBottomOfScroll] = useState(true);
const [isAtTopOfScroll, setIsAtTopOfScroll] = useState(false);
const [existsNewMessage, setExistsNewMessage] = useState(false);
const [shouldUpdateMessageWithAttached, setShouldUpdateMessageWithAttached] = useState(false);
const [numberOfMessagesToRender, setNumberOfMessagesToRender] = useState(0);
const existsNewMessageText = 'New Messages';
const loadMoreText = 'click to load more messages...';
const unableToLoadMoreText = 'You have reached the beginning of the thread';
const messagesWithAttachedRef = useRef(messagesWithAttached);
const setMessagesWithAttachedRef = (messagesWithAttachedValue: any[]) => {
messagesWithAttachedRef.current = messagesWithAttachedValue;
setMessagesWithAttached(messagesWithAttachedValue);
};
const isAtBottomOfScrollRef = useRef(isAtBottomOfScroll);
const setIsAtBottomOfScrollRef = (isAtBottomOfScrollValue: boolean) => {
isAtBottomOfScrollRef.current = isAtBottomOfScrollValue;
setIsAtBottomOfScroll(isAtBottomOfScrollValue);
};
const numberOfMessagesToRenderRef = useRef(numberOfMessagesToRender);
const setNumberOfMessagesToRenderRef = (numberOfMessagesToRenderValue: number) => {
numberOfMessagesToRenderRef.current = numberOfMessagesToRenderValue;
setNumberOfMessagesToRender(numberOfMessagesToRenderValue);
};
useEffect(() => {
setNumberOfMessagesToRenderRef(Math.ceil(chatThreadRef.current.clientHeight / 34)); //34 px is the minimum height of the chat bubble
}, [chatThreadRef.current?.clientHeight]);
useEffect(() => {
updateIndexOfTheFirstMessage();
}, [numberOfMessagesToRender]);
useEffect(() => {
// get the sender of the most recent message
const user =
props.messages && props.messages[props.messages.length - 1] && props.messages[props.messages.length - 1].sender;
// if you are the person that sent the most recent message
if (props.messages.length > 0 && user && isUserMatchingIdentity(user, props.user.identity)) {
// after sending a message, scroll to bottom
scrollToBottom();
updateIndexOfTheFirstMessage();
} else if (isAtBottomOfScroll) {
updateIndexOfTheFirstMessage();
} else {
setExistsNewMessage(true);
}
window.addEventListener('focus', sendReadReceipt);
createdRef.current.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('focus', sendReadReceipt);
createdRef.current.removeEventListener('scroll', handleScroll);
};
}, [props.messages]);
useEffect(() => {
if (shouldUpdateMessageWithAttached) {
updateMessageWithAttached();
}
setShouldUpdateMessageWithAttached(false);
}, [shouldUpdateMessageWithAttached]);
// auto scroll to the latest message when we are at bottom of the scroll
useEffect(() => {
if (isAtBottomOfScroll) {
scrollToBottom();
}
}, [messagesWithAttached]);
const loadMoreMessages = () => {
updateIndexOfTheFirstMessageToLoadMore();
};
const loadNewMessages = () => {
scrollToBottom();
updateIndexOfTheFirstMessage();
};
const scrollToBottom = () => {
createdRef.current.scrollTop = createdRef.current.scrollHeight - createdRef.current.clientHeight;
setExistsNewMessage(false);
};
const sendReadReceipt = () => {
props.sendReadReceipt(messagesWithAttachedRef.current, props.user.identity);
};
const readReceiptIcon = (message: any) => {
// message is pending send or is failed to be sent
if (message.failed) {
let messageFailed: boolean =
props.failedMessages.find((failedMessage: string) => failedMessage === message.clientMessageId) !== undefined;
return messageFailed ? (
<TooltipHost content="failed to send">
<RedbangIcon size="medium" styles={{ color: 'red' }} />{' '}
</TooltipHost>
) : (
<TooltipHost content="sending">
<PresenceStrokeIcon size="medium" />{' '}
</TooltipHost>
);
} else {
// show read receipt if it's not a large participant group
if (!props.isLargeParticipantsGroup()) {
let MessagesWithSeen = messagesWithAttached.map((messageWithAttached) => {
let isSeen = props.isMessageSeen(messageWithAttached.clientMessageId, messagesWithAttached);
return { ...messageWithAttached, isSeen };
});
if (props.isYourLatestSeenMessage(message.clientMessageId, MessagesWithSeen)) {
return (
<TooltipHost content="seen">
<MessageSeenIcon size="medium" />
</TooltipHost>
);
}
}
if (props.isYourLatestMessage(message.id, messagesWithAttached)) {
return (
<TooltipHost content="sent">
<PresenceAvailableIcon size="medium" />
</TooltipHost>
);
} else {
return <div className={noReadReceiptStyle}></div>;
}
}
};
const handleScroll = () => {
let atBottom = createdRef.current.scrollTop >= createdRef.current.scrollHeight - createdRef.current.clientHeight;
let atTop = createdRef.current.scrollTop === 0;
if (atBottom && !isAtBottomOfScrollRef.current) {
loadNewMessages();
}
setIsAtBottomOfScrollRef(atBottom);
setIsAtTopOfScroll(atTop);
};
const updateIndexOfTheFirstMessageToLoadMore = () => {
setIndexOfTheFirstMessage(
indexOfTheFirstMessage > NUMBER_OF_MESSAGES_TO_LOAD ? indexOfTheFirstMessage - NUMBER_OF_MESSAGES_TO_LOAD : 0
);
setShouldUpdateMessageWithAttached(true);
};
const updateIndexOfTheFirstMessage = () => {
setIndexOfTheFirstMessage(
props.messages.length > numberOfMessagesToRenderRef.current
? props.messages.length - numberOfMessagesToRenderRef.current
: 0
);
setShouldUpdateMessageWithAttached(true);
};
const updateMessageWithAttached = () => {
let newMessagesWithAttached: any[] = [];
let messagesToRender = props.messages.slice(indexOfTheFirstMessage, props.messages.length);
messagesToRender.map((message: any, index: number, messagesList: any) => {
let mine = message.sender.communicationUserId === props.user.identity;
let attached: string | boolean = false;
if (index === 0) {
if (index !== messagesList.length - 1) {
//the next message has the same sender
if (messagesList[index].sender.communicationUserId === messagesList[index + 1].sender.communicationUserId) {
attached = 'top';
}
}
} else {
if (messagesList[index].sender.communicationUserId === messagesList[index - 1].sender.communicationUserId) {
//the previous message has the same sender
if (index !== messagesList.length - 1) {
if (messagesList[index].sender.communicationUserId === messagesList[index + 1].sender.communicationUserId) {
//the next message has the same sender
attached = true;
} else {
//the next message has a different sender
attached = 'bottom';
}
} else {
// this is the last message of the whole messages list
attached = 'bottom';
}
} else {
//the previous message has a different sender
if (index !== messagesList.length - 1) {
if (messagesList[index].sender.communicationUserId === messagesList[index + 1].sender.communicationUserId) {
//the next message has the same sender
attached = 'top';
}
}
}
}
let messageWithAttached = { ...message, attached, mine };
newMessagesWithAttached.push(messageWithAttached);
});
setMessagesWithAttachedRef(newMessagesWithAttached);
};
return (
<Ref innerRef={chatThreadRef}>
<Stack className={chatContainerStyle}>
{isAtTopOfScroll && (
<Button
text
fluid
className={loadMoreMessageButtonStyle}
content={indexOfTheFirstMessage === 0 ? unableToLoadMoreText : loadMoreText}
disabled={indexOfTheFirstMessage === 0}
onClick={loadMoreMessages}
/>
)}
<Ref innerRef={createdRef}>
<LiveAnnouncer>
<Chat
styles={chatStyle}
items={messagesWithAttached.map((message: any, index: number) => {
const liveAuthor = `${message.senderDisplayName} says `;
const messageContentItem = (
<div>
<LiveMessage message={`${message.mine ? '' : liveAuthor} ${message.content}`} aria-live="polite" />
{renderHyperlink(message.content.message)}
</div>
);
return {
gutter: message.mine ? (
''
) : (
<div
className={messageAvatarContainerStyle(
props.users[message.sender.communicationUserId] === undefined
? ''
: props.users[message.sender.communicationUserId].emoji
)}
>
{props.users[message.sender.communicationUserId] === undefined
? ''
: props.users[message.sender.communicationUserId].emoji}
</div>
),
key: index,
contentPosition: message.mine ? 'end' : 'start',
message: (
<Flex vAlign="end">
<Chat.Message
styles={chatMessageStyle(message.mine)}
content={messageContentItem}
author={message.senderDisplayName}
mine={message.mine}
/>
<div className={readReceiptIconStyle(message.mine)}>{readReceiptIcon(message)}</div>
</Flex>
),
attached: message.attached
};
})}
/>
</LiveAnnouncer>
</Ref>
{existsNewMessage && (
<div>
<PrimaryButton className={newMessageButtonStyle} onClick={loadNewMessages}>
<Icon iconName="Down" className={DownIconStyle} />
{existsNewMessageText}
</PrimaryButton>
</div>
)}
</Stack>
</Ref>
);
};