From c06a5cb3add2383a36ad7fe62798921fe5d5c2c7 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 26 Mar 2024 11:34:49 -0600 Subject: [PATCH 1/3] Modify optimistic data to support new text+attachment messages --- src/libs/ReportUtils.ts | 22 ++++++++++++++++++---- src/libs/actions/Report.ts | 13 ++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 0bb3b1101b7c..e903ec97de1e 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -2964,13 +2964,27 @@ function getPolicyDescriptionText(policy: OnyxEntry): string { function buildOptimisticAddCommentReportAction(text?: string, file?: FileObject, actorAccountID?: number): OptimisticReportAction { const parser = new ExpensiMark(); const commentText = getParsedComment(text ?? ''); + const isAttachmentOnly = file && !text; + const isTextOnly = text && !file; + + let htmlForNewComment; + let textForNewComment; + if (isAttachmentOnly) { + htmlForNewComment = CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML; + textForNewComment = CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML; + } else if (isTextOnly) { + htmlForNewComment = commentText; + textForNewComment = parser.htmlToText(htmlForNewComment); + } else { + htmlForNewComment = `${commentText}\n${CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML}`; + textForNewComment = `${commentText}\n${CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML}`; + } + const isAttachment = !text && file !== undefined; - const attachmentInfo = isAttachment ? file : {}; - const htmlForNewComment = isAttachment ? CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML : commentText; + const attachmentInfo = file ?? {}; const accountID = actorAccountID ?? currentUserAccountID; // Remove HTML from text when applying optimistic offline comment - const textForNewComment = isAttachment ? CONST.ATTACHMENT_MESSAGE_TEXT : parser.htmlToText(htmlForNewComment); return { commentText, reportAction: { @@ -2989,7 +3003,7 @@ function buildOptimisticAddCommentReportAction(text?: string, file?: FileObject, created: DateUtils.getDBTimeWithSkew(), message: [ { - translationKey: isAttachment ? CONST.TRANSLATION_KEYS.ATTACHMENT : '', + translationKey: isAttachmentOnly ? CONST.TRANSLATION_KEYS.ATTACHMENT : '', type: CONST.REPORT.MESSAGE.TYPE.COMMENT, html: htmlForNewComment, text: textForNewComment, diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 93154bfff16b..e949fc35bf02 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -363,7 +363,7 @@ function addActions(reportID: string, text = '', file?: FileObject) { let attachmentAction: OptimisticAddCommentReportAction | undefined; let commandName: typeof WRITE_COMMANDS.ADD_COMMENT | typeof WRITE_COMMANDS.ADD_ATTACHMENT = WRITE_COMMANDS.ADD_COMMENT; - if (text) { + if (text && !file) { const reportComment = ReportUtils.buildOptimisticAddCommentReportAction(text); reportCommentAction = reportComment.reportAction; reportCommentText = reportComment.commentText; @@ -373,10 +373,15 @@ function addActions(reportID: string, text = '', file?: FileObject) { // When we are adding an attachment we will call AddAttachment. // It supports sending an attachment with an optional comment and AddComment supports adding a single text comment only. commandName = WRITE_COMMANDS.ADD_ATTACHMENT; - const attachment = ReportUtils.buildOptimisticAddCommentReportAction('', file); + const attachment = ReportUtils.buildOptimisticAddCommentReportAction(text, file); attachmentAction = attachment.reportAction; } + // When there is both text and a file, the text for the report comment needs to be parsed + if (text && file) { + reportCommentText = ReportUtils.getParsedComment(text ?? ''); + } + // Always prefer the file as the last action over text const lastAction = attachmentAction ?? reportCommentAction; const currentTime = DateUtils.getDBTimeWithSkew(); @@ -400,7 +405,9 @@ function addActions(reportID: string, text = '', file?: FileObject) { // Optimistically add the new actions to the store before waiting to save them to the server const optimisticReportActions: OnyxCollection = {}; - if (text && reportCommentAction?.reportActionID) { + + // Only add the reportCommentAction when there is no file attachment. If there is both a file attachment and text, that will all be contained in the attachmentAction. + if (text && reportCommentAction?.reportActionID && !file) { optimisticReportActions[reportCommentAction.reportActionID] = reportCommentAction; } if (file && attachmentAction?.reportActionID) { From 8aadc83e469359d1ec788e6d2201af2f5843db6e Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 28 Mar 2024 14:40:40 -0600 Subject: [PATCH 2/3] Use separate API for text and attachment --- src/libs/API/types.ts | 1 + src/libs/actions/Report.ts | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index fd84e65c028e..a9ced5754dd8 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -81,6 +81,7 @@ const WRITE_COMMANDS = { TWO_FACTOR_AUTH_VALIDATE: 'TwoFactorAuth_Validate', ADD_COMMENT: 'AddComment', ADD_ATTACHMENT: 'AddAttachment', + ADD_TEXT_AND_ATTACHMENT: 'AddTextAndAttachment', CONNECT_BANK_ACCOUNT_WITH_PLAID: 'ConnectBankAccountWithPlaid', ADD_PERSONAL_BANK_ACCOUNT: 'AddPersonalBankAccount', RESTART_BANK_ACCOUNT_SETUP: 'RestartBankAccountSetup', diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index e949fc35bf02..8cdd9a750bd7 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -361,7 +361,7 @@ function addActions(reportID: string, text = '', file?: FileObject) { let reportCommentText = ''; let reportCommentAction: OptimisticAddCommentReportAction | undefined; let attachmentAction: OptimisticAddCommentReportAction | undefined; - let commandName: typeof WRITE_COMMANDS.ADD_COMMENT | typeof WRITE_COMMANDS.ADD_ATTACHMENT = WRITE_COMMANDS.ADD_COMMENT; + let commandName: typeof WRITE_COMMANDS.ADD_COMMENT | typeof WRITE_COMMANDS.ADD_ATTACHMENT | typeof WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT = WRITE_COMMANDS.ADD_COMMENT; if (text && !file) { const reportComment = ReportUtils.buildOptimisticAddCommentReportAction(text); @@ -380,6 +380,9 @@ function addActions(reportID: string, text = '', file?: FileObject) { // When there is both text and a file, the text for the report comment needs to be parsed if (text && file) { reportCommentText = ReportUtils.getParsedComment(text ?? ''); + + // When there is both text and a file, call the API specifically built to handle both so that the comment and file are both combined in a single comment + commandName = WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT; } // Always prefer the file as the last action over text From 0ff4dd75d4cd16529569032ad8eb7e293876d1f1 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 29 Mar 2024 12:46:21 -0600 Subject: [PATCH 3/3] Fix TS error and improve comments --- src/libs/API/types.ts | 1 + src/libs/actions/Report.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index a9ced5754dd8..d581a9ba21b5 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -266,6 +266,7 @@ type WriteCommandParameters = { [WRITE_COMMANDS.TWO_FACTOR_AUTH_VALIDATE]: Parameters.ValidateTwoFactorAuthParams; [WRITE_COMMANDS.ADD_COMMENT]: Parameters.AddCommentOrAttachementParams; [WRITE_COMMANDS.ADD_ATTACHMENT]: Parameters.AddCommentOrAttachementParams; + [WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT]: Parameters.AddCommentOrAttachementParams; [WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID]: Parameters.ConnectBankAccountParams; [WRITE_COMMANDS.ADD_PERSONAL_BANK_ACCOUNT]: Parameters.AddPersonalBankAccountParams; [WRITE_COMMANDS.RESTART_BANK_ACCOUNT_SETUP]: Parameters.RestartBankAccountSetupParams; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 8cdd9a750bd7..f88c4c3b5473 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -377,11 +377,11 @@ function addActions(reportID: string, text = '', file?: FileObject) { attachmentAction = attachment.reportAction; } - // When there is both text and a file, the text for the report comment needs to be parsed if (text && file) { + // When there is both text and a file, the text for the report comment needs to be parsed) reportCommentText = ReportUtils.getParsedComment(text ?? ''); - // When there is both text and a file, call the API specifically built to handle both so that the comment and file are both combined in a single comment + // And the API command needs to go to the new API which supports combining both text and attachments in a single report action commandName = WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT; }