Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache extra attributes for video markdown conversion #42463

Merged
merged 7 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 7 additions & 5 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1451,18 +1451,21 @@
* @param newCommentText text of the comment after editing.
* @param originalCommentMarkdown original markdown of the comment before editing.
*/
function handleUserDeletedLinksInHtml(newCommentText: string, originalCommentMarkdown: string): string {
function handleUserDeletedLinksInHtml(newCommentText: string, originalCommentMarkdown: string, videoAttributeCache?: Record<string, string>): string {
const parser = new ExpensiMark();
if (newCommentText.length > CONST.MAX_MARKUP_LENGTH) {
return newCommentText;
}
const htmlForNewComment = parser.replace(newCommentText);

const htmlForNewComment = parser.replace(newCommentText, {

Check failure on line 1460 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / typecheck

Object literal may only specify known properties, and 'extras' does not exist in type '{ filterRules?: Name[] | undefined; disabledRules?: Name[] | undefined; shouldEscapeText?: boolean | undefined; shouldKeepRawInput?: boolean | undefined; }'.
extras: {videoAttributeCache},
});
const removedLinks = parser.getRemovedMarkdownLinks(originalCommentMarkdown, newCommentText);
return removeLinksFromHtml(htmlForNewComment, removedLinks);
}

/** Saves a new message for a comment. Marks the comment as edited, which will be reflected in the UI. */
function editReportComment(reportID: string, originalReportAction: OnyxEntry<ReportAction>, textForNewComment: string) {
function editReportComment(reportID: string, originalReportAction: OnyxEntry<ReportAction>, textForNewComment: string, videoAttributeCache?: Record<string, string>) {
const parser = new ExpensiMark();
const originalReportID = ReportUtils.getOriginalReportID(reportID, originalReportAction);

Expand All @@ -1480,8 +1483,7 @@
if (originalCommentMarkdown === textForNewComment) {
return;
}

const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown);
const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown, videoAttributeCache);
const reportComment = parser.htmlToText(htmlForNewComment);

// For comments shorter than or equal to 10k chars, convert the comment from MD into HTML because that's how it is stored in the database
Expand Down
13 changes: 11 additions & 2 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@

const shouldUseForcedSelectionRange = shouldUseEmojiPickerSelection();

// video source -> video attributes
const draftMessageVideoAttributeCache = new Map<string, string>();

function ReportActionItemMessageEdit(
{action, draftMessage, reportID, index, shouldDisableEmojiPicker = false}: ReportActionItemMessageEditProps,
forwardedRef: ForwardedRef<TextInput | HTMLTextAreaElement | undefined>,
Expand Down Expand Up @@ -106,7 +109,13 @@

useEffect(() => {
const parser = new ExpensiMark();
const originalMessage = parser.htmlToMarkdown(action.message?.[0]?.html ?? '');
draftMessageVideoAttributeCache.clear();
const originalMessage = parser.htmlToMarkdown(action.message?.[0]?.html ?? '', {
cacheVideoAttributes: (videoSource, attrs) => {

Check failure on line 114 in src/pages/home/report/ReportActionItemMessageEdit.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Object literal may only specify known properties, and 'cacheVideoAttributes' does not exist in type 'ExtrasObject'.

Check failure on line 114 in src/pages/home/report/ReportActionItemMessageEdit.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Parameter 'videoSource' implicitly has an 'any' type.

Check failure on line 114 in src/pages/home/report/ReportActionItemMessageEdit.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Parameter 'attrs' implicitly has an 'any' type.
draftMessageVideoAttributeCache.set(videoSource, attrs);

Check failure on line 115 in src/pages/home/report/ReportActionItemMessageEdit.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 115 in src/pages/home/report/ReportActionItemMessageEdit.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`
},
});

if (ReportActionsUtils.isDeletedAction(action) || !!(action.message && draftMessage === originalMessage) || !!(prevDraftMessage === draftMessage || isCommentPendingSaved.current)) {
return;
}
Expand Down Expand Up @@ -297,7 +306,7 @@
ReportActionContextMenu.showDeleteModal(reportID, action, true, deleteDraft, () => focusEditAfterCancelDelete(textInputRef.current));
return;
}
Report.editReportComment(reportID, action, trimmedNewDraft);
Report.editReportComment(reportID, action, trimmedNewDraft, Object.fromEntries(draftMessageVideoAttributeCache));
deleteDraft();
}, [action, deleteDraft, draft, reportID]);

Expand Down
Loading