Files
twenty_crm/packages/twenty-front/src/modules/activities/hooks/useReplaceActivityBlockEditorContent.ts
Etienne c0a0214879 delete attachment when file is removed from activity body (#11952)
Using useEffect triggered at ActivityRichTextEditor unmount, to delete
attachments only when note is closed (and not when file block is deleted
during note update to keep command + z shortcut)

closes : https://github.com/twentyhq/twenty/issues/11229
2025-05-13 19:18:38 +02:00

35 lines
1.1 KiB
TypeScript

import { BLOCK_SCHEMA } from '@/activities/blocks/constants/Schema';
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { isNonEmptyString } from '@sniptt/guards';
import { useRecoilCallback } from 'recoil';
import { isDefined } from 'twenty-shared/utils';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
export const useReplaceActivityBlockEditorContent = (
editor: typeof BLOCK_SCHEMA.BlockNoteEditor,
) => {
const replaceBlockEditorContent = useRecoilCallback(
({ snapshot }) =>
(activityId: string) => {
if (isDefined(editor)) {
const activityInStore = snapshot
.getLoadable(recordStoreFamilyState(activityId))
.getValue();
const content = isNonEmptyString(activityInStore?.bodyV2.blocknote)
? JSON.parse(activityInStore?.bodyV2.blocknote)
: [{ type: 'paragraph', content: '' }];
if (!isDeeplyEqual(editor.document, content)) {
editor.replaceBlocks(editor.document, content);
}
}
},
[editor],
);
return {
replaceBlockEditorContent,
};
};