Skip to content

Commit 7b9f153

Browse files
committed
CW-system-crash-react Fixed ReactWithEmoji usage. Added try/catch for apis
1 parent 326b870 commit 7b9f153

File tree

3 files changed

+214
-193
lines changed

3 files changed

+214
-193
lines changed

src/shared/components/Chat/ChatMessage/ChatMessage.tsx

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -409,27 +409,6 @@ const ChatMessage = ({
409409
return null;
410410
}
411411

412-
const emojiButton = useCallback(
413-
() => (
414-
<ReactWithEmoji
415-
emojiButtonClassName={styles.emojiButton}
416-
discussionId={discussionMessage.discussionId}
417-
discussionMessageId={discussionMessage.id}
418-
className={
419-
isNotCurrentUserMessage
420-
? styles.reactWithEmoji
421-
: styles.reactWithEmojiSelf
422-
}
423-
isNotCurrentUserMessage={isNotCurrentUserMessage}
424-
/>
425-
),
426-
[
427-
discussionMessage.discussionId,
428-
discussionMessage.id,
429-
isNotCurrentUserMessage,
430-
],
431-
);
432-
433412
return (
434413
<ChatMessageContext.Provider value={chatMessageContextValue}>
435414
<li
@@ -442,7 +421,19 @@ const ChatMessage = ({
442421
[styles.systemMessageContainer]: isSystemMessage,
443422
})}
444423
>
445-
{!isSystemMessage && !isNotCurrentUserMessage && emojiButton}
424+
{!isSystemMessage && !isNotCurrentUserMessage && (
425+
<ReactWithEmoji
426+
emojiButtonClassName={styles.emojiButton}
427+
discussionId={discussionMessage.discussionId}
428+
discussionMessageId={discussionMessage.id}
429+
className={
430+
isNotCurrentUserMessage
431+
? styles.reactWithEmoji
432+
: styles.reactWithEmojiSelf
433+
}
434+
isNotCurrentUserMessage={isNotCurrentUserMessage}
435+
/>
436+
)}
446437
{isNotCurrentUserMessage &&
447438
(isUserDiscussionMessage || isBotMessage) && (
448439
<div className={styles.iconWrapper} onClick={handleUserClick}>
@@ -584,7 +575,19 @@ const ChatMessage = ({
584575
</div>
585576
</>
586577
)}
587-
{!isSystemMessage && isNotCurrentUserMessage && emojiButton}
578+
{!isSystemMessage && isNotCurrentUserMessage && (
579+
<ReactWithEmoji
580+
emojiButtonClassName={styles.emojiButton}
581+
discussionId={discussionMessage.discussionId}
582+
discussionMessageId={discussionMessage.id}
583+
className={
584+
isNotCurrentUserMessage
585+
? styles.reactWithEmoji
586+
: styles.reactWithEmojiSelf
587+
}
588+
isNotCurrentUserMessage={isNotCurrentUserMessage}
589+
/>
590+
)}
588591
</div>
589592
</li>
590593
</ChatMessageContext.Provider>

src/shared/hooks/useCases/useDiscussionMessagesById.ts

Lines changed: 137 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -238,75 +238,79 @@ export const useDiscussionMessagesById = ({
238238
setIsBatchLoading(true);
239239
}
240240

241-
DiscussionMessageService.subscribeToDiscussionMessagesByDiscussionId(
242-
discussionId,
243-
lastVisible && lastVisible[discussionId],
244-
async (
245-
addedDiscussionMessages,
246-
modifiedDiscussionMessages,
247-
removedDiscussionMessages,
248-
lastVisibleDocument,
249-
) => {
250-
const updatedDiscussionMessages = [
251-
...addedDiscussionMessages,
252-
...modifiedDiscussionMessages,
253-
];
254-
setLastVisible((prevVisible) => ({
255-
...prevVisible,
256-
[discussionId]: lastVisibleDocument,
257-
}));
258-
259-
const hasLastVisibleDocument = !!lastVisibleDocument?.data();
260-
261-
const discussionsWithText = await Promise.all(
262-
updatedDiscussionMessages.map(async (discussionMessage) => {
263-
const isUserDiscussionMessage =
264-
checkIsUserDiscussionMessage(discussionMessage);
265-
const isSystemMessage =
266-
checkIsSystemDiscussionMessage(discussionMessage);
267-
268-
const parsedText = await getTextFromTextEditorString({
269-
userId,
270-
ownerId: isUserDiscussionMessage
271-
? discussionMessage.ownerId
272-
: null,
273-
textEditorString: discussionMessage.text,
274-
users,
275-
commonId: discussionMessage.commonId,
276-
systemMessage: isSystemMessage ? discussionMessage : undefined,
277-
getCommonPagePath,
278-
getCommonPageAboutTabPath,
279-
directParent,
280-
onUserClick,
281-
onFeedItemClick,
282-
onInternalLinkClick,
283-
});
284-
285-
return {
286-
...discussionMessage,
287-
parsedText,
288-
};
289-
}),
290-
);
291-
if (
292-
discussionsWithText.length < MESSAGES_NUMBER_IN_BATCH &&
293-
!hasLastVisibleDocument
294-
) {
295-
setIsEndOfList((prevIsEndOfList) => ({
296-
...prevIsEndOfList,
297-
[discussionId]: true,
241+
try {
242+
DiscussionMessageService.subscribeToDiscussionMessagesByDiscussionId(
243+
discussionId,
244+
lastVisible && lastVisible[discussionId],
245+
async (
246+
addedDiscussionMessages,
247+
modifiedDiscussionMessages,
248+
removedDiscussionMessages,
249+
lastVisibleDocument,
250+
) => {
251+
const updatedDiscussionMessages = [
252+
...addedDiscussionMessages,
253+
...modifiedDiscussionMessages,
254+
];
255+
setLastVisible((prevVisible) => ({
256+
...prevVisible,
257+
[discussionId]: lastVisibleDocument,
298258
}));
299-
}
300-
dispatch(
301-
cacheActions.updateDiscussionMessagesStateByDiscussionId({
302-
discussionId,
303-
removedDiscussionMessages,
304-
updatedDiscussionMessages: discussionsWithText,
305-
}),
306-
);
307-
setIsBatchLoading(false);
308-
},
309-
);
259+
260+
const hasLastVisibleDocument = !!lastVisibleDocument?.data();
261+
262+
const discussionsWithText = await Promise.all(
263+
updatedDiscussionMessages.map(async (discussionMessage) => {
264+
const isUserDiscussionMessage =
265+
checkIsUserDiscussionMessage(discussionMessage);
266+
const isSystemMessage =
267+
checkIsSystemDiscussionMessage(discussionMessage);
268+
269+
const parsedText = await getTextFromTextEditorString({
270+
userId,
271+
ownerId: isUserDiscussionMessage
272+
? discussionMessage.ownerId
273+
: null,
274+
textEditorString: discussionMessage.text,
275+
users,
276+
commonId: discussionMessage.commonId,
277+
systemMessage: isSystemMessage ? discussionMessage : undefined,
278+
getCommonPagePath,
279+
getCommonPageAboutTabPath,
280+
directParent,
281+
onUserClick,
282+
onFeedItemClick,
283+
onInternalLinkClick,
284+
});
285+
286+
return {
287+
...discussionMessage,
288+
parsedText,
289+
};
290+
}),
291+
);
292+
if (
293+
discussionsWithText.length < MESSAGES_NUMBER_IN_BATCH &&
294+
!hasLastVisibleDocument
295+
) {
296+
setIsEndOfList((prevIsEndOfList) => ({
297+
...prevIsEndOfList,
298+
[discussionId]: true,
299+
}));
300+
}
301+
dispatch(
302+
cacheActions.updateDiscussionMessagesStateByDiscussionId({
303+
discussionId,
304+
removedDiscussionMessages,
305+
updatedDiscussionMessages: discussionsWithText,
306+
}),
307+
);
308+
setIsBatchLoading(false);
309+
},
310+
);
311+
} catch(err) {
312+
setIsBatchLoading(false);
313+
}
310314
};
311315

312316
useDeepCompareEffect(() => {
@@ -316,70 +320,74 @@ export const useDiscussionMessagesById = ({
316320
return;
317321
}
318322

319-
const discussionMessages = [...state.data];
320-
321-
// if (discussionMessages.length > 0 && users.length === 0) {
322-
// return;
323-
// }
323+
try {
324+
const discussionMessages = [...state.data];
324325

325-
const filteredMessages = discussionMessages.filter(
326-
({ moderation }) =>
327-
moderation?.flag !== ModerationFlags.Hidden || hasPermissionToHide,
328-
);
329-
const loadedDiscussionMessages = await Promise.all(
330-
filteredMessages.map(async (d) => {
331-
const newDiscussionMessage = { ...d };
332-
const parentMessage = filteredMessages.find(
333-
({ id }) => id === d.parentId,
334-
);
335-
if (
336-
checkIsUserDiscussionMessage(d) &&
337-
checkIsUserDiscussionMessage(newDiscussionMessage)
338-
) {
339-
const commonMemberMessageOwner = [
340-
AI_USER,
341-
AI_PRO_USER,
342-
...users,
343-
...externalCommonUsers,
344-
].find((o) => o.uid === d.ownerId);
345-
const messageOwner =
346-
commonMemberMessageOwner ||
347-
(await UserService.getUserById(d.ownerId));
348-
newDiscussionMessage.owner = messageOwner;
349-
if (!commonMemberMessageOwner && messageOwner) {
350-
dispatch(
351-
cacheActions.addUserToExternalCommonUsers({
352-
user: messageOwner,
353-
}),
354-
);
355-
}
356-
}
357-
newDiscussionMessage.parentMessage = parentMessage
358-
? {
359-
id: parentMessage.id,
360-
text: parentMessage.text,
361-
ownerName: parentMessage?.ownerName,
362-
...(checkIsUserDiscussionMessage(parentMessage) && {
363-
ownerId: parentMessage.ownerId,
364-
}),
365-
moderation: parentMessage?.moderation,
366-
images: parentMessage?.images,
367-
files: parentMessage?.files,
368-
createdAt: parentMessage.createdAt,
326+
const filteredMessages = discussionMessages.filter(
327+
({ moderation }) =>
328+
moderation?.flag !== ModerationFlags.Hidden || hasPermissionToHide,
329+
);
330+
const loadedDiscussionMessages = await Promise.all(
331+
filteredMessages.map(async (d) => {
332+
const newDiscussionMessage = { ...d };
333+
const parentMessage = filteredMessages.find(
334+
({ id }) => id === d.parentId,
335+
);
336+
if (
337+
checkIsUserDiscussionMessage(d) &&
338+
checkIsUserDiscussionMessage(newDiscussionMessage)
339+
) {
340+
const commonMemberMessageOwner = [
341+
AI_USER,
342+
AI_PRO_USER,
343+
...users,
344+
...externalCommonUsers,
345+
].find((o) => o.uid === d.ownerId);
346+
const messageOwner =
347+
commonMemberMessageOwner ||
348+
(await UserService.getUserById(d.ownerId));
349+
newDiscussionMessage.owner = messageOwner;
350+
if (!commonMemberMessageOwner && messageOwner) {
351+
dispatch(
352+
cacheActions.addUserToExternalCommonUsers({
353+
user: messageOwner,
354+
}),
355+
);
369356
}
370-
: null;
371-
372-
return newDiscussionMessage;
373-
}),
374-
);
375-
376-
setDiscussionMessagesWithOwners(loadedDiscussionMessages);
377-
setIsFirstBatchLoaded((prev) => ({
378-
...prev,
379-
[discussionId]: true,
380-
}));
357+
}
358+
newDiscussionMessage.parentMessage = parentMessage
359+
? {
360+
id: parentMessage.id,
361+
text: parentMessage.text,
362+
ownerName: parentMessage?.ownerName,
363+
...(checkIsUserDiscussionMessage(parentMessage) && {
364+
ownerId: parentMessage.ownerId,
365+
}),
366+
moderation: parentMessage?.moderation,
367+
images: parentMessage?.images,
368+
files: parentMessage?.files,
369+
createdAt: parentMessage.createdAt,
370+
}
371+
: null;
372+
373+
return newDiscussionMessage;
374+
}),
375+
);
376+
377+
setDiscussionMessagesWithOwners(loadedDiscussionMessages);
378+
setIsFirstBatchLoaded((prev) => ({
379+
...prev,
380+
[discussionId]: true,
381+
}));
382+
} catch(err) {
383+
setDiscussionMessagesWithOwners([]);
384+
setIsFirstBatchLoaded((prev) => ({
385+
...prev,
386+
[discussionId]: true,
387+
}));
388+
}
381389
})();
382-
}, [state.data, hasPermissionToHide, users, externalCommonUsers]);
390+
}, [state.data, hasPermissionToHide, users, externalCommonUsers, discussionId]);
383391

384392
return {
385393
...state,

0 commit comments

Comments
 (0)