From 4f626c08c243443df41b6d4351dbd85476ea83db Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 24 Oct 2024 08:46:43 -0400 Subject: [PATCH] perf: Fix comments double rendering on mount (#7824) --- app/models/Comment.ts | 2 +- .../Document/components/CommentThreadItem.tsx | 25 +++++++------------ 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/app/models/Comment.ts b/app/models/Comment.ts index 4a67ab60c0..89461ea097 100644 --- a/app/models/Comment.ts +++ b/app/models/Comment.ts @@ -26,7 +26,7 @@ class Comment extends Model { * The Prosemirror data representing the comment content */ @Field - @observable + @observable.shallow data: ProsemirrorData; /** diff --git a/app/scenes/Document/components/CommentThreadItem.tsx b/app/scenes/Document/components/CommentThreadItem.tsx index 5c463b6803..6a9123b533 100644 --- a/app/scenes/Document/components/CommentThreadItem.tsx +++ b/app/scenes/Document/components/CommentThreadItem.tsx @@ -1,5 +1,5 @@ import { differenceInMilliseconds } from "date-fns"; -import { toJS } from "mobx"; +import { action } from "mobx"; import { observer } from "mobx-react"; import { darken } from "polished"; import * as React from "react"; @@ -96,8 +96,7 @@ function CommentThreadItem({ highlightedText, }: Props) { const { t } = useTranslation(); - const [forceRender, setForceRender] = React.useState(0); - const [data, setData] = React.useState(toJS(comment.data)); + const [data, setData] = React.useState(comment.data); const showAuthor = firstOfAuthor; const showTime = useShowTime(comment.createdAt, previousCommentCreatedAt); const showEdited = @@ -117,31 +116,24 @@ function CommentThreadItem({ ); }; - const handleSubmit = async (event: React.FormEvent) => { + const handleSubmit = action(async (event: React.FormEvent) => { event.preventDefault(); try { setReadOnly(); - await comment.save({ - data, - }); + comment.data = data; + await comment.save(); } catch (error) { setEditing(); toast.error(t("Error updating comment")); } - }; + }); const handleCancel = () => { - setData(toJS(comment.data)); + setData(comment.data); setReadOnly(); - setForceRender((i) => ++i); }; - React.useEffect(() => { - setData(toJS(comment.data)); - setForceRender((i) => ++i); - }, [comment.data]); - return ( {firstOfAuthor && ( @@ -186,8 +178,9 @@ function CommentThreadItem({ )}