Skip to content

The open alternative to Salesforce, designed for AI.

fix(email): keep line breaks when opening a synced draft in the composer (#26556)

Maried13ae35849f2
diff --git a/packages/twenty-front/src/modules/activities/emails/utils/__tests__/getEmailDraftPrefillFromMessage.test.ts b/packages/twenty-front/src/modules/activities/emails/utils/__tests__/getEmailDraftPrefillFromMessage.test.ts
new file mode 100644
index 00000000000..6fb878a2daa
--- /dev/null
+++ b/packages/twenty-front/src/modules/activities/emails/utils/__tests__/getEmailDraftPrefillFromMessage.test.ts
@@ -0,0 +1,54 @@
+import { type EmailThreadMessageWithSender } from '@/activities/emails/types/EmailThreadMessageWithSender';
+import { getEmailDraftPrefillFromMessage } from '@/activities/emails/utils/getEmailDraftPrefillFromMessage';
+import { MessageParticipantRole } from 'twenty-shared/types';
+
+const buildDraftMessage = (text: string): EmailThreadMessageWithSender =>
+ ({
+ id: 'draft-message-id',
+ subject: 'Your payment did not go through',
+ text,
+ messageParticipants: [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'Marie',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: 'Pedro',
+ },
+ ],
+ }) as unknown as EmailThreadMessageWithSender;
+
+describe('getEmailDraftPrefillFromMessage', () => {
+ it('keeps the line breaks of the synced draft text in the composer body', () => {
+ const prefill = getEmailDraftPrefillFromMessage(
+ buildDraftMessage('Hi,\n\nThe payment failed.\n\nBest,\nMarie'),
+ );
+
+ const document = JSON.parse(prefill.body);
+
+ expect(document.type).toBe('doc');
+ expect(document.content[0].content).toEqual([
+ { type: 'text', text: 'Hi,' },
+ { type: 'hardBreak' },
+ { type: 'hardBreak' },
+ { type: 'text', text: 'The payment failed.' },
+ { type: 'hardBreak' },
+ { type: 'hardBreak' },
+ { type: 'text', text: 'Best,' },
+ { type: 'hardBreak' },
+ { type: 'text', text: 'Marie' },
+ ]);
+ });
+
+ it('prefills recipients and subject from the draft', () => {
+ const prefill = getEmailDraftPrefillFromMessage(buildDraftMessage('Hi'));
+
+ expect(prefill.messageId).toBe('draft-message-id');
+ expect(prefill.subject).toBe('Your payment did not go through');
+ expect(prefill.to).toContain('[email protected]');
+ expect(prefill.cc).toBe('');
+ });
+});
diff --git a/packages/twenty-front/src/modules/activities/emails/utils/getEmailDraftPrefillFromMessage.ts b/packages/twenty-front/src/modules/activities/emails/utils/getEmailDraftPrefillFromMessage.ts
index cfb72cdef70..07501350153 100644
--- a/packages/twenty-front/src/modules/activities/emails/utils/getEmailDraftPrefillFromMessage.ts
+++ b/packages/twenty-front/src/modules/activities/emails/utils/getEmailDraftPrefillFromMessage.ts
@@ -3,6 +3,7 @@ import { isNonEmptyString } from '@sniptt/guards';
import { formatEmailRecipient } from '@/activities/emails/recipients/utils/formatEmailRecipient';
import { type EmailDraftPrefill } from '@/activities/emails/types/EmailDraftPrefill';
import { type EmailThreadMessageWithSender } from '@/activities/emails/types/EmailThreadMessageWithSender';
+import { serializePlainTextAsAdvancedTextEditorDocument } from '@/advanced-text-editor/utils/serializePlainTextAsAdvancedTextEditorDocument';
import { MessageParticipantRole } from 'twenty-shared/types';
export const getEmailDraftPrefillFromMessage = (
@@ -26,6 +27,6 @@ export const getEmailDraftPrefillFromMessage = (
cc: joinRecipientsByRole(MessageParticipantRole.CC),
bcc: joinRecipientsByRole(MessageParticipantRole.BCC),
subject: message.subject,
- body: message.text,
+ body: serializePlainTextAsAdvancedTextEditorDocument(message.text),
};
};