Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6697,7 +6697,7 @@ function replaceLocalAttachmentReferences(draftMarkdown: string, currentCommentH
return draftMarkdown.replace(localAttachmentReferenceRegex, '').trim();
}

const attachmentTags = currentCommentHtml.match(/<(?:img|video)\b[^>]*>|<a\b[^>]*data-expensify-source="[^"]*"[^>]*>[\s\S]*?<\/a>/gi) ?? [];
const attachmentTags = currentCommentHtml.match(/<img\b[^>]*>|<video\b[^>]*>[\s\S]*?<\/video>|<a\b[^>]*data-expensify-source="[^"]*"[^>]*>[\s\S]*?<\/a>/gi) ?? [];
const syncedAttachmentTag = attachmentTags.find((tag) => {
const source = tag.match(CONST.REGEX.ATTACHMENT.ATTACHMENT_SOURCE)?.at(2) ?? '';
return source.match(CONST.REGEX.ATTACHMENT.ATTACHMENT_SOURCE_ID)?.at(1) === reportActionID;
Expand All @@ -6718,13 +6718,23 @@ function replaceLocalAttachmentReferences(draftMarkdown: string, currentCommentH
});
}

/** Empty parts are dropped so deleting all the text does not leave a separator above the attachment. */
function buildEditedCommentWithAttachment(commentHtml: string, uploadingAttachmentHtml: string | undefined): string {
if (!uploadingAttachmentHtml) {
return commentHtml;
}
return [commentHtml, uploadingAttachmentHtml].filter(Boolean).join('<br /><br />');
}

/** The still-uploading attachment tag, re-appended to an edit's optimistic message so it stays on screen. */
function getUploadingAttachmentHtmlFromComment(currentCommentHtml: string | undefined): string | undefined {
if (!currentCommentHtml?.includes(CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE)) {
return undefined;
}
const attachmentTagRegex = new RegExp(
`<(?:img|video)\\b[^>]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>|<a\\b[^>]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>[\\s\\S]*?</a>`,
`<img\\b[^>]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>` +
`|<video\\b[^>]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>[\\s\\S]*?</video>` +
`|<a\\b[^>]*${CONST.ATTACHMENT_OPTIMISTIC_SOURCE_ATTRIBUTE}="[^"]*"[^>]*>[\\s\\S]*?</a>`,
'i',
);
return currentCommentHtml.match(attachmentTagRegex)?.at(0);
Expand Down Expand Up @@ -14474,6 +14484,7 @@ export {
replaceLocalAttachmentReferences,
isUploadingAttachmentRemovedFromDraft,
getUploadingAttachmentHtmlFromComment,
buildEditedCommentWithAttachment,
};

export type {
Expand Down
3 changes: 2 additions & 1 deletion src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import {updateTitleFieldToMatchPolicy} from '@libs/ReportTitleUtils';
import type {Ancestor, OptimisticAddCommentReportAction, OptimisticChatReport, SelfDMParameters} from '@libs/ReportUtils';
import {
buildEditedCommentWithAttachment,
buildOptimisticAddCommentReportAction,
buildOptimisticChangeFieldAction,
buildOptimisticChangePolicyReportAction,
Expand Down Expand Up @@ -3565,7 +3566,7 @@ function editReportComment(

// Optimistic message only: the sent copy is stripped, so without this the attachment vanishes until upload lands.
const uploadingAttachmentHtml = shouldRemoveQueuedAttachment ? undefined : getUploadingAttachmentHtmlFromComment(originalCommentHTML);
const optimisticHtml = uploadingAttachmentHtml ? `${htmlForNewComment}<br /><br />${uploadingAttachmentHtml}` : htmlForNewComment;
const optimisticHtml = buildEditedCommentWithAttachment(htmlForNewComment, uploadingAttachmentHtml);
const optimisticText = uploadingAttachmentHtml ? Parser.htmlToText(optimisticHtml) : reportComment;

const optimisticReportActions: PartialDeep<ReportActions> = {
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/crea
import getReportURLForCurrentContext from '@libs/Navigation/helpers/getReportURLForCurrentContext';
import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute';
import Navigation from '@libs/Navigation/Navigation';
import Parser from '@libs/Parser';
import * as PolicyUtils from '@libs/PolicyUtils';
import {getOriginalMessage, getReportAction, isActionOfType, isWhisperAction} from '@libs/ReportActionsUtils';
// Testing only so it's okay to import computeReportName
Expand All @@ -30,6 +31,7 @@ import {buildReportNameFromParticipantNames, computeReportName as computeReportN
import type {OptionData} from '@libs/ReportUtils';
import {
areAllRequestsBeingSmartScanned,
buildEditedCommentWithAttachment,
buildOptimisticAnnounceChat,
buildOptimisticApprovedReportAction,
buildOptimisticCancelPaymentReportAction,
Expand Down Expand Up @@ -6470,6 +6472,42 @@ describe('ReportUtils', () => {
expect(getUploadingAttachmentHtmlFromComment(syncedImageHtml)).toBeUndefined();
});

describe('buildEditedCommentWithAttachment', () => {
const attachmentTag = '<video src="blob:local" data-optimistic-src="blob:local">clip.mp4</video>';

it('separates the attachment from the text that was kept', () => {
expect(buildEditedCommentWithAttachment('Hello edited', attachmentTag)).toBe(`Hello edited<br /><br />${attachmentTag}`);
});

it('leaves no separator when the edit removed all of the text', () => {
expect(buildEditedCommentWithAttachment('', attachmentTag)).toBe(attachmentTag);
});

it('returns the comment untouched when nothing is uploading', () => {
expect(buildEditedCommentWithAttachment('Hello edited', undefined)).toBe('Hello edited');
});
});

describe('video attachments', () => {
const videoSource = 'blob:https://dev.new.expensify.com:8082/uuid-video';
const uploadingVideoHtml = `Hello<br /><br /><video src="${videoSource}" data-optimistic-src="${videoSource}" data-expensify-source="${videoSource}" data-name="clip.mp4">clip.mp4</video>`;

it('re-appends the whole video element, not just its opening tag', () => {
const tag = getUploadingAttachmentHtmlFromComment(uploadingVideoHtml);

expect(tag).toContain('</video>');
expect(tag).toContain('>clip.mp4<');
});

it('survives a second edit, because the stored element still parses back to a reference', () => {
const storedAfterFirstEdit = `Hello edited<br /><br />${getUploadingAttachmentHtmlFromComment(uploadingVideoHtml)}`;
const secondEditDraft = Parser.htmlToMarkdown(storedAfterFirstEdit).trim();

expect(secondEditDraft).toContain(videoSource);
expect(isUploadingAttachmentRemovedFromDraft(secondEditDraft, storedAfterFirstEdit)).toBe(false);
});
});

it('does not swap in an attachment owned by a different report action', () => {
const otherActionHtml =
'Hello<br /><br /><img src="https://www.expensify.com/chat-attachments/999/w_other.jpg" data-expensify-source="https://www.expensify.com/chat-attachments/999/other.jpg" />';
Expand Down
Loading