mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
perf: Fix comments double rendering on mount (#7824)
This commit is contained in:
1 parent
57e9abd77f
commit
4f626c08c2
2 files changed
+10
-17
No files matched your search
@@ -26,7 +26,7 @@ class Comment extends Model {
|
||||
* The Prosemirror data representing the comment content
|
||||
*/
|
||||
@Field
|
||||
@observable
|
||||
@observable.shallow
|
||||
data: ProsemirrorData;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 (
|
||||
<Flex gap={8} align="flex-start" reverse={dir === "rtl"}>
|
||||
{firstOfAuthor && (
|
||||
@@ -186,8 +178,9 @@ function CommentThreadItem({
|
||||
)}
|
||||
<Body ref={formRef} onSubmit={handleSubmit}>
|
||||
<StyledCommentEditor
|
||||
key={`${forceRender}`}
|
||||
key={String(isEditing)}
|
||||
readOnly={!isEditing}
|
||||
value={comment.data}
|
||||
defaultValue={data}
|
||||
onChange={handleChange}
|
||||
onSave={handleSave}
|
||||
|
||||
Reference in new issue
Block a user