-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fix block editor not changing content when switching to a new activity #9107
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState'; | ||
import { isNonEmptyString } from '@sniptt/guards'; | ||
import { useRecoilCallback } from 'recoil'; | ||
import { isDefined } from 'twenty-ui'; | ||
import { isDeeplyEqual } from '~/utils/isDeeplyEqual'; | ||
|
||
export const useReplaceBlockEditorContent = ( | ||
activityId: string, | ||
editor: any, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: editor type should be properly typed rather than 'any' to ensure type safety |
||
) => { | ||
const replaceBlockEditorContent = useRecoilCallback( | ||
({ snapshot }) => | ||
() => { | ||
if (isDefined(editor)) { | ||
const activityInStore = snapshot | ||
.getLoadable(recordStoreFamilyState(activityId)) | ||
.getValue(); | ||
|
||
const content = isNonEmptyString(activityInStore?.body) | ||
? JSON.parse(activityInStore?.body) | ||
: [{ type: 'paragraph', content: '' }]; | ||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: JSON.parse could throw if body contains invalid JSON. Wrap in try/catch and provide fallback |
||
|
||
if (!isDeeplyEqual(editor.document, content)) { | ||
editor.replaceBlocks(editor.document, content); | ||
} | ||
} | ||
}, | ||
[activityId, editor], | ||
); | ||
|
||
return { | ||
replaceBlockEditorContent, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: replaceBlockEditorContent() called directly in render function could cause infinite re-renders or race conditions. Should be in useEffect.