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
2 changes: 1 addition & 1 deletion src/components/MoneyReportHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ function MoneyReportHeader({
betas,
});

const showNextStepBar = shouldShowNextStep && !!optimisticNextStep?.message?.length;
const showNextStepBar = shouldShowNextStep && !!(optimisticNextStep?.message?.length ?? (optimisticNextStep && 'messageKey' in optimisticNextStep));
const showNextStepSkeleton = shouldShowNextStep && !optimisticNextStep && !!isLoadingInitialReportActions && !isOffline;
const shouldShowMoreContent = showNextStepBar || showNextStepSkeleton || !!statusBarProps || isReportInSearch;

Expand Down
46 changes: 39 additions & 7 deletions src/components/MoneyReportHeaderStatusBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@ import {View} from 'react-native';
import type {ValueOf} from 'type-fest';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {parseMessage} from '@libs/NextStepUtils';
import {buildNextStepMessage, parseMessage} from '@libs/NextStepUtils';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type {ReportNextStep} from '@src/types/onyx/Report';
import type ReportNextStepDeprecated from '@src/types/onyx/ReportNextStepDeprecated';
import type IconAsset from '@src/types/utils/IconAsset';
import Icon from './Icon';
import RenderHTML from './RenderHTML';

/** Combined type that accepts either the new format or the deprecated format */
type NextStepData = ReportNextStep | ReportNextStepDeprecated;

type MoneyReportHeaderStatusBarProps = {
/** The next step for the report (deprecated old format) */
nextStep: ReportNextStepDeprecated | undefined;
/** The next step for the report (supports both new and deprecated formats) */
nextStep: NextStepData | undefined;
};

type IconName = ValueOf<typeof CONST.NEXT_STEP.ICONS>;
type IconMap = Record<IconName, IconAsset>;

/**
* Type guard to check if the next step is in the deprecated format (has message array).
* We prioritize the old format first for backwards compatibility during migration.
*/
function isDeprecatedFormatNextStep(step: NextStepData): step is ReportNextStepDeprecated {
return 'message' in step && Array.isArray(step.message) && step.message.length > 0;
}

function MoneyReportHeaderStatusBar({nextStep}: MoneyReportHeaderStatusBarProps) {
const styles = useThemeStyles();
const theme = useTheme();
const {translate} = useLocalize();
const icons = useMemoizedLazyExpensifyIcons(['Hourglass', 'Checkmark', 'Stopwatch', 'DotIndicator']);
const iconMap: IconMap = useMemo(
() => ({
Expand All @@ -35,11 +49,29 @@ function MoneyReportHeaderStatusBar({nextStep}: MoneyReportHeaderStatusBarProps)
[icons],
);
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const currentUserAccountID = currentUserPersonalDetails.accountID;
const currentUserEmail = currentUserPersonalDetails.login ?? '';

const messageContent = useMemo(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ PERF-6 (docs)

The messageContent is computed synchronously based on props/hook values and does not require useMemo for correctness. The computation is relatively lightweight (type checking and a single function call), and the memoization overhead may exceed the benefit.

Suggested fix: Remove useMemo and compute the value directly in the component body:

let messageContent = "";
if (nextStep) {
    if (isDeprecatedFormatNextStep(nextStep)) {
        messageContent = parseMessage(nextStep.message, currentUserEmail);
    } else if ("messageKey" in nextStep && nextStep.messageKey) {
        messageContent = buildNextStepMessage(nextStep, translate, currentUserAccountID);
    }
}

This ensures the value is always in sync with props without the extra render cycle or memoization complexity.


Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

const messageArray = nextStep?.message;
return parseMessage(messageArray, currentUserEmail);
}, [nextStep?.message, currentUserEmail]);
if (!nextStep) {
return '';
}

// Handle old/deprecated format first (with message array) for backwards compatibility
if (isDeprecatedFormatNextStep(nextStep)) {
return parseMessage(nextStep.message, currentUserEmail);
}

// Fall back to new format (with messageKey)
if ('messageKey' in nextStep && nextStep.messageKey) {
return buildNextStepMessage(nextStep, translate, currentUserAccountID);
}

return '';
}, [nextStep, translate, currentUserAccountID, currentUserEmail]);

// iconFill can be set by frontend optimistic updates (deprecated format) but backend never sends it in new format
const iconFill = (nextStep && 'iconFill' in nextStep ? (nextStep as {iconFill?: string}).iconFill : undefined) ?? theme.icon;

return (
<View style={[styles.dFlex, styles.flexRow, styles.alignItemsCenter, styles.overflowHidden, styles.w100, styles.headerStatusBarContainer]}>
Expand All @@ -48,7 +80,7 @@ function MoneyReportHeaderStatusBar({nextStep}: MoneyReportHeaderStatusBarProps)
src={(nextStep?.icon && iconMap?.[nextStep.icon]) ?? icons.Hourglass}
height={variables.iconSizeSmall}
width={variables.iconSizeSmall}
fill={nextStep?.iconFill ?? theme.icon}
fill={iconFill}
/>
</View>
<View style={[styles.dFlex, styles.flexRow, styles.flexShrink1]}>
Expand Down
1 change: 1 addition & 0 deletions src/libs/DebugUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ function validateReportDraftProperty(key: keyof Report | keyof ReportNameValuePa
icon: 'string',
actorAccountID: 'number',
eta: 'object',
iconFill: 'string',
});
case 'tripData':
return validateObject<ObjectElement<Report, 'tripData'>>(value, {
Expand Down
3 changes: 2 additions & 1 deletion src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type BuildNextStepNewParams = {
};

function buildNextStepMessage(nextStep: ReportNextStep, translate: LocaleContextProps['translate'], currentUserAccountID: number): string {
const actor = getDisplayNameForParticipant({accountID: nextStep.actorAccountID, formatPhoneNumber: formatPhoneNumberPhoneUtils});
// Escape actor name to prevent HTML injection since this will be rendered as HTML
const actor = Str.safeEscape(getDisplayNameForParticipant({accountID: nextStep.actorAccountID, formatPhoneNumber: formatPhoneNumberPhoneUtils}) ?? '');
let actorType: ValueOf<typeof CONST.NEXT_STEP.ACTOR_TYPE>;
if (nextStep.actorAccountID === currentUserAccountID) {
actorType = CONST.NEXT_STEP.ACTOR_TYPE.CURRENT_USER;
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type ReportNextStep = {
/** The ETA date time */
dateTime?: string;
};

/** The fill color of the icon */
iconFill?: string;
};

/** Model of report data */
Expand Down
Loading