Skip to content

Commit

Permalink
CW-instant-chat-message-edit
Browse files Browse the repository at this point in the history
Added optimistic sent
  • Loading branch information
MeyerPV committed Oct 7, 2024
1 parent d0483cf commit 9b6a00c
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/shared/components/Chat/ChatMessage/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const ChatMessage = ({
}>();
const [isEditMode, setEditMode] = useState(false);
const [isMenuOpen, setIsMenuOpen] = useState(false);
const [parsedMessage, setParsedMessage] = useState(discussionMessage.parsedText);
const [isMessageEditLoading, setIsMessageEditLoading] = useState(false);
const isTabletView = useIsTabletView();
const isUserDiscussionMessage =
Expand Down Expand Up @@ -177,9 +178,9 @@ const ChatMessage = ({
]);

const createdAtDate = new Date(discussionMessage.createdAt.seconds * 1000);
const editedAtDate = new Date(
const [editedAtDate, setEditedAtDate] = useState(new Date(
(discussionMessage.editedAt?.seconds ?? 0) * 1000,
);
));

const handleUserClick = () => {
if (onUserClick && discussionMessageUserId && !isBotMessage) {
Expand Down Expand Up @@ -258,6 +259,7 @@ const ChatMessage = ({
handleEditModeClose();
} else {
notify("Something went wrong");
setParsedMessage(discussionMessage.parsedText);
}
},
}),
Expand All @@ -276,17 +278,37 @@ const ChatMessage = ({
handleEditModeClose();
} catch (err) {
notify("Something went wrong");
setParsedMessage(discussionMessage.parsedText);
} finally {
setIsMessageEditLoading(false);
}
};

const updateMessage = (message: TextEditorValue) => {
const updateMessage = async (message: TextEditorValue) => {
try {
if (chatType === ChatType.ChatMessages) {
updateChatMessage(message);
} else {
updateDiscussionMessage(message);
}
const parsedText = await getTextFromTextEditorString({
userId,
ownerId: discussionMessageUserId,
textEditorString: JSON.stringify(message),
users,
commonId: discussionMessage.commonId,
directParent,
onUserClick,
onFeedItemClick,
onInternalLinkClick,
});

setParsedMessage(parsedText);
setEditedAtDate(new Date());
handleEditModeClose();
} catch(err) {
setIsMessageEditLoading(false);
}
};
updateMessageRef.current = {
updateMessage,
Expand Down Expand Up @@ -520,7 +542,7 @@ const ChatMessage = ({
<ChatMessageLinkify
onInternalLinkClick={handleInternalLinkClick}
>
{discussionMessage.parsedText.map((text) => text)}
{(parsedMessage ?? []).map((text) => text)}
</ChatMessageLinkify>
{!isSystemMessage && (
<Time
Expand Down

0 comments on commit 9b6a00c

Please sign in to comment.