Skip to content

Commit

Permalink
Merge pull request #39007 from Expensify/tgolen-improve-attachmentmes…
Browse files Browse the repository at this point in the history
…sage

Modify optimistic data to support new text+attachment messages
  • Loading branch information
johnmlee101 authored Apr 11, 2024
2 parents ae11ccf + 70d3a94 commit 2e1dd1d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 18 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3098,13 +3098,27 @@ function getPolicyDescriptionText(policy: OnyxEntry<Policy>): 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: {
Expand All @@ -3123,7 +3137,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,
Expand Down
18 changes: 14 additions & 4 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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<OptimisticAddCommentReportAction> = {};
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) {
Expand Down

0 comments on commit 2e1dd1d

Please sign in to comment.