Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System crash with #310 error in useDiscussionById.ts:376 #2708

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -316,70 +320,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
Loading