diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index fc19ba60693c..7c814608dc08 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', @@ -268,6 +269,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/ReportUtils.ts b/src/libs/ReportUtils.ts index 85f5c414dbe4..f07d6844deb3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -3082,13 +3082,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: { @@ -3107,7 +3121,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 d2f85362baf8..096125215a32 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -373,9 +373,9 @@ 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) { + if (text && !file) { const reportComment = ReportUtils.buildOptimisticAddCommentReportAction(text); reportCommentAction = reportComment.reportAction; reportCommentText = reportComment.commentText; @@ -385,10 +385,18 @@ 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; } + 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 ?? ''); + + // 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; + } + // Always prefer the file as the last action over text const lastAction = attachmentAction ?? reportCommentAction; const currentTime = DateUtils.getDBTimeWithSkew(); @@ -412,7 +420,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) {