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

fix block editor not changing content when switching to a new activity #9107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useDebouncedCallback } from 'use-debounce';
import { v4 } from 'uuid';

import { useUpsertActivity } from '@/activities/hooks/useUpsertActivity';
import { activityBodyFamilyState } from '@/activities/states/activityBodyFamilyState';
import { activityTitleHasBeenSetFamilyState } from '@/activities/states/activityTitleHasBeenSetFamilyState';
import { canCreateActivityState } from '@/activities/states/canCreateActivityState';
import { ActivityEditorHotkeyScope } from '@/activities/types/ActivityEditorHotkeyScope';
Expand All @@ -27,6 +26,7 @@ import { useUploadAttachmentFile } from '@/activities/files/hooks/useUploadAttac
import { Note } from '@/activities/types/Note';
import { Task } from '@/activities/types/Task';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useReplaceBlockEditorContent } from '@/ui/input/hooks/useReplaceBlockEditorContent';
import '@blocknote/core/fonts/inter.css';
import '@blocknote/mantine/style.css';
import '@blocknote/react/style.css';
Expand Down Expand Up @@ -55,12 +55,6 @@ export const RichTextEditor = ({
}),
);

const [activityBody, setActivityBody] = useRecoilState(
activityBodyFamilyState({
activityId: activityId,
}),
);

const { objectMetadataItem: objectMetadataItemActivity } =
useObjectMetadataItem({
objectNameSingular: activityObjectNameSingular,
Expand Down Expand Up @@ -241,24 +235,14 @@ export const RichTextEditor = ({

const handleBodyChangeDebounced = useDebouncedCallback(handleBodyChange, 500);

// See https://github.com/twentyhq/twenty/issues/6724 for explanation
const setActivityBodyDebouncedToAvoidDragBug = useDebouncedCallback(
setActivityBody,
100,
);

const handleEditorChange = () => {
const newStringifiedBody = JSON.stringify(editor.document) ?? '';

setActivityBodyDebouncedToAvoidDragBug(newStringifiedBody);

handleBodyChangeDebounced(newStringifiedBody);
};

const initialBody = useMemo(() => {
if (isNonEmptyString(activityBody) && activityBody !== '{}') {
return JSON.parse(activityBody);
} else if (
if (
isDefined(activity) &&
isNonEmptyString(activity.body) &&
activity?.body !== '{}'
Expand All @@ -267,7 +251,7 @@ export const RichTextEditor = ({
} else {
return undefined;
}
}, [activity, activityBody]);
}, [activity]);

const handleEditorBuiltInUploadFile = async (file: File) => {
const { attachementAbsoluteURL } = await handleUploadAttachment(file);
Expand All @@ -282,6 +266,13 @@ export const RichTextEditor = ({
uploadFile: handleEditorBuiltInUploadFile,
});

const { replaceBlockEditorContent } = useReplaceBlockEditorContent(
activityId,
editor,
);

replaceBlockEditorContent();
Copy link
Contributor

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.


useScopedHotkeys(
Key.Escape,
() => {
Expand Down
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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
};
};
Loading