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: draft message edit issue #38906

Merged
merged 6 commits into from
Mar 25, 2024
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
3 changes: 2 additions & 1 deletion src/pages/home/report/ContextMenu/ContextMenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ const ContextMenuActions: ContextMenuAction[] = [
}
const editAction = () => {
if (!draftMessage) {
Report.saveReportActionDraft(reportID, reportAction, getActionHtml(reportAction));
const parser = new ExpensiMark();
Report.saveReportActionDraft(reportID, reportAction, parser.htmlToMarkdown(getActionHtml(reportAction)));
} else {
Report.deleteReportActionDraft(reportID, reportAction);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {useIsFocused, useNavigation} from '@react-navigation/native';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import lodashDebounce from 'lodash/debounce';
import type {ForwardedRef, MutableRefObject, RefAttributes, RefObject} from 'react';
import React, {forwardRef, memo, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
Expand Down Expand Up @@ -531,7 +532,8 @@ function ComposerWithSuggestions(
) {
event.preventDefault();
if (lastReportAction) {
Report.saveReportActionDraft(reportID, lastReportAction, lastReportAction.message?.at(-1)?.html ?? '');
const parser = new ExpensiMark();
Report.saveReportActionDraft(reportID, lastReportAction, parser.htmlToMarkdown(lastReportAction.message?.at(-1)?.html ?? ''));
}
}
},
Expand Down
26 changes: 8 additions & 18 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import Str from 'expensify-common/lib/str';
import lodashDebounce from 'lodash/debounce';
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
Expand Down Expand Up @@ -82,31 +81,20 @@ function ReportActionItemMessageEdit(
const {isSmallScreenWidth} = useWindowDimensions();
const prevDraftMessage = usePrevious(draftMessage);

const getInitialDraft = () => {
if (draftMessage === action?.message?.[0].html) {
// We only convert the report action message to markdown if the draft message is unchanged.
const parser = new ExpensiMark();
return parser.htmlToMarkdown(draftMessage).trim();
}
// We need to decode saved draft message because it's escaped before saving.
return Str.htmlDecode(draftMessage);
};

const getInitialSelection = () => {
if (isMobileSafari) {
return {start: 0, end: 0};
}

const length = getInitialDraft().length;
const length = draftMessage.length;
return {start: length, end: length};
};
const emojisPresentBefore = useRef<Emoji[]>([]);
const [draft, setDraft] = useState(() => {
const initialDraft = getInitialDraft();
if (initialDraft) {
emojisPresentBefore.current = EmojiUtils.extractEmojis(initialDraft);
if (draftMessage) {
emojisPresentBefore.current = EmojiUtils.extractEmojis(draftMessage);
}
return initialDraft;
return draftMessage;
});
const [selection, setSelection] = useState<{
start: number;
Expand All @@ -126,10 +114,12 @@ function ReportActionItemMessageEdit(
const draftRef = useRef(draft);

useEffect(() => {
if (ReportActionsUtils.isDeletedAction(action) || Boolean(action.message && draftMessage === action.message[0].html) || Boolean(prevDraftMessage === draftMessage)) {
const parser = new ExpensiMark();
const originalMessage = parser.htmlToMarkdown(action.message?.[0].html ?? '');
if (ReportActionsUtils.isDeletedAction(action) || Boolean(action.message && draftMessage === originalMessage) || Boolean(prevDraftMessage === draftMessage)) {
return;
}
setDraft(Str.htmlDecode(draftMessage));
setDraft(draftMessage);
}, [draftMessage, action, prevDraftMessage]);

useEffect(() => {
Expand Down
Loading