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

Added img attribute caching #49774

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 3 additions & 9 deletions src/libs/Parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// eslint-disable-next-line no-restricted-imports
import {ExpensiMark} from 'expensify-common';
import Onyx from 'react-native-onyx';

Check failure on line 3 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Insert `{Extras}·from·'expensify-common/dist/ExpensiMark';⏎import·`

Check failure on line 3 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Insert `{Extras}·from·'expensify-common/dist/ExpensiMark';⏎import·`
import ONYXKEYS from '@src/ONYXKEYS';
import Log from './Log';
import * as ReportConnection from './ReportConnection';

Check failure on line 6 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Delete `';⏎import·{·Extras·}·from·'expensify-common/dist/ExpensiMark`

Check failure on line 6 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Delete `';⏎import·{·Extras·}·from·'expensify-common/dist/ExpensiMark`
import { Extras } from 'expensify-common/dist/ExpensiMark';

Check failure on line 7 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

All imports in the declaration are only used as types. Use `import type`

Check failure on line 7 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

`expensify-common/dist/ExpensiMark` import should occur before import of `./Log`

Check failure on line 7 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

All imports in the declaration are only used as types. Use `import type`

Check failure on line 7 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

`expensify-common/dist/ExpensiMark` import should occur before import of `./Log`

Check failure on line 7 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / typecheck

Module '"expensify-common/dist/ExpensiMark"' has no exported member 'Extras'. Did you mean to use 'import Extras from "expensify-common/dist/ExpensiMark"' instead?

const accountIDToNameMap: Record<string, string> = {};

Expand All @@ -20,27 +21,20 @@
},
});

type Extras = {
reportIDToName?: Record<string, string>;
accountIDToName?: Record<string, string>;
cacheVideoAttributes?: (vidSource: string, attrs: string) => void;
videoAttributeCache?: Record<string, string>;
};

class ExpensiMarkWithContext extends ExpensiMark {
htmlToMarkdown(htmlString: string, extras?: Extras): string {
return super.htmlToMarkdown(htmlString, {
reportIDToName: extras?.reportIDToName ?? ReportConnection.getAllReportsNameMap(),

Check failure on line 27 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unsafe assignment of an `any` value

Check failure on line 27 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unsafe member access .reportIDToName on an `any` value

Check failure on line 27 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe assignment of an `any` value

Check failure on line 27 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe member access .reportIDToName on an `any` value
accountIDToName: extras?.accountIDToName ?? accountIDToNameMap,

Check failure on line 28 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unsafe assignment of an `any` value

Check failure on line 28 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unsafe member access .accountIDToName on an `any` value

Check failure on line 28 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe assignment of an `any` value

Check failure on line 28 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe member access .accountIDToName on an `any` value
cacheVideoAttributes: extras?.cacheVideoAttributes,
mediaAttributeCachingFn: extras?.mediaAttributeCachingFn,

Check failure on line 29 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unsafe assignment of an `any` value

Check failure on line 29 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unsafe member access .mediaAttributeCachingFn on an `any` value

Check failure on line 29 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe assignment of an `any` value

Check failure on line 29 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe member access .mediaAttributeCachingFn on an `any` value

Check failure on line 29 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / typecheck

Object literal may only specify known properties, and 'mediaAttributeCachingFn' does not exist in type 'Extras'.
});
}

htmlToText(htmlString: string, extras?: Extras): string {
return super.htmlToText(htmlString, {
reportIDToName: extras?.reportIDToName ?? ReportConnection.getAllReportsNameMap(),
accountIDToName: extras?.accountIDToName ?? accountIDToNameMap,
cacheVideoAttributes: extras?.cacheVideoAttributes,
mediaAttributeCachingFn: extras?.mediaAttributeCachingFn,

Check failure on line 37 in src/libs/Parser.ts

View workflow job for this annotation

GitHub Actions / typecheck

Object literal may only specify known properties, and 'mediaAttributeCachingFn' does not exist in type 'Extras'.
});
}

Expand Down
10 changes: 5 additions & 5 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1566,24 +1566,24 @@
*
* @param newCommentText text of the comment after editing.
* @param originalCommentMarkdown original markdown of the comment before editing.
* @param videoAttributeCache cache of video attributes ([videoSource]: videoAttributes)
* @param mediaAttributeCache cache of media attributes - img and video ([mediaSource]: mediaAttributes)
*/
function handleUserDeletedLinksInHtml(newCommentText: string, originalCommentMarkdown: string, videoAttributeCache?: Record<string, string>): string {
function handleUserDeletedLinksInHtml(newCommentText: string, originalCommentMarkdown: string, mediaAttributeCache?: Record<string, string>): string {
if (newCommentText.length > CONST.MAX_MARKUP_LENGTH) {
return newCommentText;
}

const textWithMention = ReportUtils.completeShortMention(newCommentText);

const htmlForNewComment = Parser.replace(textWithMention, {
extras: {videoAttributeCache},
extras: {mediaAttributeCache},

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

View workflow job for this annotation

GitHub Actions / typecheck

Object literal may only specify known properties, and 'mediaAttributeCache' does not exist in type 'Extras'.
});
const removedLinks = Parser.getRemovedMarkdownLinks(originalCommentMarkdown, textWithMention);
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, videoAttributeCache?: Record<string, string>) {
function editReportComment(reportID: string, originalReportAction: OnyxEntry<ReportAction>, textForNewComment: string, mediaAttributeCache?: Record<string, string>) {
const originalReportID = ReportUtils.getOriginalReportID(reportID, originalReportAction);

if (!originalReportID || !originalReportAction) {
Expand All @@ -1600,7 +1600,7 @@
if (originalCommentMarkdown === textForNewComment) {
return;
}
const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown, videoAttributeCache);
const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown, mediaAttributeCache);

const reportComment = Parser.htmlToText(htmlForNewComment);

Expand Down
15 changes: 10 additions & 5 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@

const shouldUseForcedSelectionRange = shouldUseEmojiPickerSelection();

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

function ReportActionItemMessageEdit(
{action, draftMessage, reportID, policyID, index, isGroupPolicyReport, shouldDisableEmojiPicker = false}: ReportActionItemMessageEditProps,
Expand Down Expand Up @@ -126,11 +126,15 @@
const isCommentPendingSaved = useRef(false);

useEffect(() => {
draftMessageVideoAttributeCache.clear();
draftMessageMediaAttributeCache.clear();

const originalMessage = Parser.htmlToMarkdown(ReportActionsUtils.getReportActionHtml(action), {
cacheVideoAttributes: (videoSource, attrs) => draftMessageVideoAttributeCache.set(videoSource, attrs),
mediaAttributeCachingFn: (mediaSource, attrs) => draftMessageMediaAttributeCache.set(mediaSource, attrs),

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

View workflow job for this annotation

GitHub Actions / typecheck

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

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

View workflow job for this annotation

GitHub Actions / typecheck

Parameter 'attrs' implicitly has an 'any' type.
});

console.log(ReportActionsUtils.getReportActionHtml(action), 'orgAction');
console.log(draftMessageMediaAttributeCache, 'cachedAttributes');

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

Report.editReportComment(reportID, action, trimmedNewDraft, Object.fromEntries(draftMessageMediaAttributeCache));
deleteDraft();
}, [action, deleteDraft, draft, reportID]);

Expand Down
Loading