Skip to content

Commit

Permalink
Merge pull request #39466 from Expensify/aldo_support-markdown-for-de…
Browse files Browse the repository at this point in the history
…scription-preview

Support markdown for description preview

(cherry picked from commit bce4724)
  • Loading branch information
marcaaron authored and OSBotify committed Apr 3, 2024
1 parent 4f5d4f8 commit fb70f34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
25 changes: 11 additions & 14 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import type {ImageContentFit} from 'expo-image';
import type {ForwardedRef, ReactNode} from 'react';
import React, {forwardRef, useContext, useEffect, useMemo, useRef, useState} from 'react';
import React, {forwardRef, useContext, useMemo} from 'react';
import type {GestureResponderEvent, StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {AnimatedStyle} from 'react-native-reanimated';
Expand Down Expand Up @@ -325,8 +325,6 @@ function MenuItem(
const StyleUtils = useStyleUtils();
const combinedStyle = [style, styles.popoverMenuItem];
const {isSmallScreenWidth} = useWindowDimensions();
const [html, setHtml] = useState('');
const titleRef = useRef('');
const {isExecuting, singleExecution, waitForNavigate} = useContext(MenuItemGroupContext) ?? {};

const isDeleted = style && Array.isArray(style) ? style.includes(styles.offlineFeedback.deleted) : false;
Expand Down Expand Up @@ -354,26 +352,25 @@ function MenuItem(
isDeleted ? styles.offlineFeedback.deleted : {},
]);

useEffect(() => {
if (!title || (titleRef.current.length && titleRef.current === title) || !shouldParseTitle) {
return;
const html = useMemo(() => {
if (!title || !shouldParseTitle) {
return '';
}
const parser = new ExpensiMark();
setHtml(parser.replace(title));
titleRef.current = title;
return parser.replace(title);
}, [title, shouldParseTitle]);

const getProcessedTitle = useMemo(() => {
let processedTitle = '';
const processedTitle = useMemo(() => {
let titleToWrap = '';
if (shouldRenderAsHTML) {
processedTitle = title ? convertToLTR(title) : '';
titleToWrap = title ? convertToLTR(title) : '';
}

if (shouldParseTitle) {
processedTitle = html;
titleToWrap = html;
}

return processedTitle ? `<comment>${processedTitle}</comment>` : '';
return titleToWrap ? `<comment>${titleToWrap}</comment>` : '';
}, [title, shouldRenderAsHTML, shouldParseTitle, html]);

const hasPressableRightComponent = iconRight || (shouldShowRightComponent && rightComponent);
Expand Down Expand Up @@ -536,7 +533,7 @@ function MenuItem(
<View style={[styles.flexRow, styles.alignItemsCenter]}>
{!!title && (shouldRenderAsHTML || (shouldParseTitle && !!html.length)) && (
<View style={styles.renderHTMLTitle}>
<RenderHTML html={getProcessedTitle} />
<RenderHTML html={processedTitle} />
</View>
)}
{!shouldRenderAsHTML && !shouldParseTitle && !!title && (
Expand Down
37 changes: 26 additions & 11 deletions src/components/ReportActionItem/ReportPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import React, {useMemo} from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
Expand All @@ -8,6 +9,7 @@ import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import RenderHTML from '@components/RenderHTML';
import SettlementButton from '@components/SettlementButton';
import {showContextMenuForReport} from '@components/ShowContextMenuContext';
import Text from '@components/Text';
Expand Down Expand Up @@ -145,15 +147,6 @@ function ReportPreview({
if (TransactionUtils.isPartialMerchant(formattedMerchant ?? '')) {
formattedMerchant = null;
}
const previewSubtitle =
// Formatted merchant can be an empty string
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(formattedMerchant ?? formattedDescription) ||
translate('iou.requestCount', {
count: numberOfRequests - numberOfScanningReceipts - numberOfPendingRequests,
scanningReceipts: numberOfScanningReceipts,
pendingReceipts: numberOfPendingRequests,
});

const shouldShowSubmitButton = isOpenExpenseReport && reimbursableSpend !== 0;
const shouldDisableSubmitButton = shouldShowSubmitButton && !ReportUtils.isAllowedToSubmitDraftExpenseReport(iouReport);
Expand Down Expand Up @@ -234,6 +227,24 @@ function ReportPreview({
numberOfRequests === 1 && (!!formattedMerchant || !!formattedDescription) && !(hasOnlyTransactionsWithPendingRoutes && !totalDisplaySpend);
const shouldShowSubtitle = !isScanning && (shouldShowSingleRequestMerchantOrDescription || numberOfRequests > 1);

const {isSupportTextHtml, supportText} = useMemo(() => {
if (formattedMerchant) {
return {isSupportTextHtml: false, supportText: formattedMerchant};
}
if (formattedDescription ?? moneyRequestComment) {
const parsedSubtitle = new ExpensiMark().replace(formattedDescription ?? moneyRequestComment);
return {isSupportTextHtml: !!parsedSubtitle, supportText: parsedSubtitle ? `<muted-text>${parsedSubtitle}</muted-text>` : ''};
}
return {
isSupportTextHtml: false,
supportText: translate('iou.requestCount', {
count: numberOfRequests - numberOfScanningReceipts - numberOfPendingRequests,
scanningReceipts: numberOfScanningReceipts,
pendingReceipts: numberOfPendingRequests,
}),
};
}, [formattedMerchant, formattedDescription, moneyRequestComment, translate, numberOfRequests, numberOfScanningReceipts, numberOfPendingRequests]);

return (
<OfflineWithFeedback
pendingAction={iouReport?.pendingFields?.preview}
Expand Down Expand Up @@ -296,10 +307,14 @@ function ReportPreview({
)}
</View>
</View>
{shouldShowSubtitle && (
{shouldShowSubtitle && supportText && (
<View style={styles.flexRow}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={[styles.textLabelSupporting, styles.textNormal, styles.lh20]}>{previewSubtitle || moneyRequestComment}</Text>
{isSupportTextHtml ? (
<RenderHTML html={supportText} />
) : (
<Text style={[styles.textLabelSupporting, styles.textNormal, styles.lh20]}>{supportText}</Text>
)}
</View>
</View>
)}
Expand Down

0 comments on commit fb70f34

Please sign in to comment.