Skip to content

Commit

Permalink
Merge pull request #2708 from daostack/CW-system-crash-react
Browse files Browse the repository at this point in the history
System crash with  #310 error in useDiscussionById.ts:376
  • Loading branch information
MeyerPV authored Jul 2, 2024
2 parents 7fc4213 + 95d8d63 commit b1dde57
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 185 deletions.
29 changes: 14 additions & 15 deletions src/shared/components/Chat/ChatMessage/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,8 @@ const ChatMessage = ({
[isMessageEditLoading, handleCheckboxChange, isMessageEditAllowed],
);

if (isSystemMessage && discussionMessage.parsedText.length === 0) {
return null;
}

const emojiButton = useCallback(
() => (
const EmojiButton = useCallback(() => {
return (
<ReactWithEmoji
emojiButtonClassName={styles.emojiButton}
discussionId={discussionMessage.discussionId}
Expand All @@ -422,13 +418,16 @@ const ChatMessage = ({
}
isNotCurrentUserMessage={isNotCurrentUserMessage}
/>
),
[
discussionMessage.discussionId,
discussionMessage.id,
isNotCurrentUserMessage,
],
);
);
}, [
discussionMessage.discussionId,
discussionMessage.id,
isNotCurrentUserMessage,
]);

if (isSystemMessage && discussionMessage.parsedText.length === 0) {
return null;
}

return (
<ChatMessageContext.Provider value={chatMessageContextValue}>
Expand All @@ -442,7 +441,7 @@ const ChatMessage = ({
[styles.systemMessageContainer]: isSystemMessage,
})}
>
{!isSystemMessage && !isNotCurrentUserMessage && emojiButton}
{!isSystemMessage && !isNotCurrentUserMessage && <EmojiButton />}
{isNotCurrentUserMessage &&
(isUserDiscussionMessage || isBotMessage) && (
<div className={styles.iconWrapper} onClick={handleUserClick}>
Expand Down Expand Up @@ -584,7 +583,7 @@ const ChatMessage = ({
</div>
</>
)}
{!isSystemMessage && isNotCurrentUserMessage && emojiButton}
{!isSystemMessage && isNotCurrentUserMessage && <EmojiButton />}
</div>
</li>
</ChatMessageContext.Provider>
Expand Down
266 changes: 137 additions & 129 deletions src/shared/hooks/useCases/useDiscussionMessagesById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,75 +238,79 @@ export const useDiscussionMessagesById = ({
setIsBatchLoading(true);
}

DiscussionMessageService.subscribeToDiscussionMessagesByDiscussionId(
discussionId,
lastVisible && lastVisible[discussionId],
async (
addedDiscussionMessages,
modifiedDiscussionMessages,
removedDiscussionMessages,
lastVisibleDocument,
) => {
const updatedDiscussionMessages = [
...addedDiscussionMessages,
...modifiedDiscussionMessages,
];
setLastVisible((prevVisible) => ({
...prevVisible,
[discussionId]: lastVisibleDocument,
}));

const hasLastVisibleDocument = !!lastVisibleDocument?.data();

const discussionsWithText = await Promise.all(
updatedDiscussionMessages.map(async (discussionMessage) => {
const isUserDiscussionMessage =
checkIsUserDiscussionMessage(discussionMessage);
const isSystemMessage =
checkIsSystemDiscussionMessage(discussionMessage);

const parsedText = await getTextFromTextEditorString({
userId,
ownerId: isUserDiscussionMessage
? discussionMessage.ownerId
: null,
textEditorString: discussionMessage.text,
users,
commonId: discussionMessage.commonId,
systemMessage: isSystemMessage ? discussionMessage : undefined,
getCommonPagePath,
getCommonPageAboutTabPath,
directParent,
onUserClick,
onFeedItemClick,
onInternalLinkClick,
});

return {
...discussionMessage,
parsedText,
};
}),
);
if (
discussionsWithText.length < MESSAGES_NUMBER_IN_BATCH &&
!hasLastVisibleDocument
) {
setIsEndOfList((prevIsEndOfList) => ({
...prevIsEndOfList,
[discussionId]: true,
try {
DiscussionMessageService.subscribeToDiscussionMessagesByDiscussionId(
discussionId,
lastVisible && lastVisible[discussionId],
async (
addedDiscussionMessages,
modifiedDiscussionMessages,
removedDiscussionMessages,
lastVisibleDocument,
) => {
const updatedDiscussionMessages = [
...addedDiscussionMessages,
...modifiedDiscussionMessages,
];
setLastVisible((prevVisible) => ({
...prevVisible,
[discussionId]: lastVisibleDocument,
}));
}
dispatch(
cacheActions.updateDiscussionMessagesStateByDiscussionId({
discussionId,
removedDiscussionMessages,
updatedDiscussionMessages: discussionsWithText,
}),
);
setIsBatchLoading(false);
},
);

const hasLastVisibleDocument = !!lastVisibleDocument?.data();

const discussionsWithText = await Promise.all(
updatedDiscussionMessages.map(async (discussionMessage) => {
const isUserDiscussionMessage =
checkIsUserDiscussionMessage(discussionMessage);
const isSystemMessage =
checkIsSystemDiscussionMessage(discussionMessage);

const parsedText = await getTextFromTextEditorString({
userId,
ownerId: isUserDiscussionMessage
? discussionMessage.ownerId
: null,
textEditorString: discussionMessage.text,
users,
commonId: discussionMessage.commonId,
systemMessage: isSystemMessage ? discussionMessage : undefined,
getCommonPagePath,
getCommonPageAboutTabPath,
directParent,
onUserClick,
onFeedItemClick,
onInternalLinkClick,
});

return {
...discussionMessage,
parsedText,
};
}),
);
if (
discussionsWithText.length < MESSAGES_NUMBER_IN_BATCH &&
!hasLastVisibleDocument
) {
setIsEndOfList((prevIsEndOfList) => ({
...prevIsEndOfList,
[discussionId]: true,
}));
}
dispatch(
cacheActions.updateDiscussionMessagesStateByDiscussionId({
discussionId,
removedDiscussionMessages,
updatedDiscussionMessages: discussionsWithText,
}),
);
setIsBatchLoading(false);
},
);
} catch(err) {
setIsBatchLoading(false);
}
};

useDeepCompareEffect(() => {
Expand All @@ -324,70 +328,74 @@ export const useDiscussionMessagesById = ({
return;
}

const discussionMessages = [...state.data];

// if (discussionMessages.length > 0 && users.length === 0) {
// return;
// }
try {
const discussionMessages = [...state.data];

const filteredMessages = discussionMessages.filter(
({ moderation }) =>
moderation?.flag !== ModerationFlags.Hidden || hasPermissionToHide,
);
const loadedDiscussionMessages = await Promise.all(
filteredMessages.map(async (d) => {
const newDiscussionMessage = { ...d };
const parentMessage = filteredMessages.find(
({ id }) => id === d.parentId,
);
if (
checkIsUserDiscussionMessage(d) &&
checkIsUserDiscussionMessage(newDiscussionMessage)
) {
const commonMemberMessageOwner = [
AI_USER,
AI_PRO_USER,
...users,
...externalCommonUsers,
].find((o) => o.uid === d.ownerId);
const messageOwner =
commonMemberMessageOwner ||
(await UserService.getUserById(d.ownerId));
newDiscussionMessage.owner = messageOwner;
if (!commonMemberMessageOwner && messageOwner) {
dispatch(
cacheActions.addUserToExternalCommonUsers({
user: messageOwner,
}),
);
}
}
newDiscussionMessage.parentMessage = parentMessage
? {
id: parentMessage.id,
text: parentMessage.text,
ownerName: parentMessage?.ownerName,
...(checkIsUserDiscussionMessage(parentMessage) && {
ownerId: parentMessage.ownerId,
}),
moderation: parentMessage?.moderation,
images: parentMessage?.images,
files: parentMessage?.files,
createdAt: parentMessage.createdAt,
const filteredMessages = discussionMessages.filter(
({ moderation }) =>
moderation?.flag !== ModerationFlags.Hidden || hasPermissionToHide,
);
const loadedDiscussionMessages = await Promise.all(
filteredMessages.map(async (d) => {
const newDiscussionMessage = { ...d };
const parentMessage = filteredMessages.find(
({ id }) => id === d.parentId,
);
if (
checkIsUserDiscussionMessage(d) &&
checkIsUserDiscussionMessage(newDiscussionMessage)
) {
const commonMemberMessageOwner = [
AI_USER,
AI_PRO_USER,
...users,
...externalCommonUsers,
].find((o) => o.uid === d.ownerId);
const messageOwner =
commonMemberMessageOwner ||
(await UserService.getUserById(d.ownerId));
newDiscussionMessage.owner = messageOwner;
if (!commonMemberMessageOwner && messageOwner) {
dispatch(
cacheActions.addUserToExternalCommonUsers({
user: messageOwner,
}),
);
}
: null;

return newDiscussionMessage;
}),
);

setDiscussionMessagesWithOwners(loadedDiscussionMessages);
setIsFirstBatchLoaded((prev) => ({
...prev,
[discussionId]: true,
}));
}
newDiscussionMessage.parentMessage = parentMessage
? {
id: parentMessage.id,
text: parentMessage.text,
ownerName: parentMessage?.ownerName,
...(checkIsUserDiscussionMessage(parentMessage) && {
ownerId: parentMessage.ownerId,
}),
moderation: parentMessage?.moderation,
images: parentMessage?.images,
files: parentMessage?.files,
createdAt: parentMessage.createdAt,
}
: null;

return newDiscussionMessage;
}),
);

setDiscussionMessagesWithOwners(loadedDiscussionMessages);
setIsFirstBatchLoaded((prev) => ({
...prev,
[discussionId]: true,
}));
} catch(err) {
setDiscussionMessagesWithOwners([]);
setIsFirstBatchLoaded((prev) => ({
...prev,
[discussionId]: true,
}));
}
})();
}, [state.data, hasPermissionToHide, users, externalCommonUsers]);
}, [state.data, hasPermissionToHide, users, externalCommonUsers, discussionId]);

return {
...state,
Expand Down
Loading

0 comments on commit b1dde57

Please sign in to comment.