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
34 changes: 15 additions & 19 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ import {isEmailPublicDomain} from './LoginUtils';
import {getForReportAction, getMovedReportID} from './ModifiedExpenseMessage';
import getStateFromPath from './Navigation/helpers/getStateFromPath';
import {isFullScreenName} from './Navigation/helpers/isNavigatorName';
import isSearchTopmostFullScreenRoute from './Navigation/helpers/isSearchTopmostFullScreenRoute';
import {linkingConfig} from './Navigation/linkingConfig';
import Navigation, {navigationRef} from './Navigation/Navigation';
import type {MoneyRequestNavigatorParamList, ReportsSplitNavigatorParamList} from './Navigation/types';
Expand Down Expand Up @@ -6397,8 +6396,12 @@ function getDeletedTransactionMessage(action: ReportAction) {

function getMovedTransactionMessage(report: OnyxEntry<Report>) {
const reportName = getReportName(report) ?? report?.reportName ?? '';
const reportUrl = getReportURLForCurrentContext(report?.reportID);
return translateLocal('iou.movedTransaction', {reportUrl, reportName});
const reportUrl = `${environmentURL}/r/${report?.reportID}`;
const message = translateLocal('iou.movedTransaction', {

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-4

Objects passed as props should be properly memoized to prevent unnecessary re-renders. Creating a new object on every function call can cause performance issues when this function is called frequently.

Consider memoizing the object or passing primitive values directly:

// Better approach - pass primitive values
return translateLocal('iou.movedTransaction', reportUrl, reportName);

// Or if object is required, consider memoization at the call site
const params = useMemo(() => ({
    reportUrl: `${environmentURL}/r/${report?.reportID}`,
    reportName: getReportName(report) ?? report?.reportName ?? ''
}), [report?.reportID, report?.reportName]);

reportUrl,
reportName,
});
return message;
}

function getUnreportedTransactionMessage() {
Expand All @@ -6423,8 +6426,8 @@ function getMovedActionMessage(action: ReportAction, report: OnyxEntry<Report>)
const toPolicyName = getPolicyNameByID(toPolicyID);
return translateLocal('iou.movedAction', {

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-4

Objects passed as props should be properly memoized to prevent unnecessary re-renders. This object creation happens every time the function is called, which can impact performance.

Consider extracting the object creation and memoizing it:

const params = useMemo(() => ({
    shouldHideMovedReportUrl: !isDM(report),
    movedReportUrl: `${environmentURL}/r/${movedReportID}`,
    newParentReportUrl: `${environmentURL}/r/${newParentReportID}`,
    toPolicyName,
}), [report, movedReportID, newParentReportID, toPolicyName]);

return translateLocal('iou.movedAction', params);

shouldHideMovedReportUrl: !isDM(report),
movedReportUrl: getReportURLForCurrentContext(movedReportID),
newParentReportUrl: getReportURLForCurrentContext(newParentReportID),
movedReportUrl: `${environmentURL}/r/${movedReportID}`,
newParentReportUrl: `${environmentURL}/r/${newParentReportID}`,
toPolicyName,
});
}
Expand Down Expand Up @@ -6748,8 +6751,8 @@ function buildOptimisticMovedReportAction(
const movedActionMessage = [
{
html: shouldHideMovedReportUrl
? `moved this <a href='${getReportURLForCurrentContext(movedReportID)}' target='_blank' rel='noreferrer noopener'>report</a> to the <a href='${getReportURLForCurrentContext(newParentReportID)}' target='_blank' rel='noreferrer noopener'>${policyName}</a> workspace`
: `moved this report to the <a href='${getReportURLForCurrentContext(newParentReportID)}' target='_blank' rel='noreferrer noopener'>${policyName}</a> workspace`,
? `moved this <a href='${CONST.NEW_EXPENSIFY_URL}r/${movedReportID}' target='_blank' rel='noreferrer noopener'>report</a> to the <a href='${CONST.NEW_EXPENSIFY_URL}r/${newParentReportID}' target='_blank' rel='noreferrer noopener'>${policyName}</a> workspace`
: `moved this report to the <a href='${CONST.NEW_EXPENSIFY_URL}r/${newParentReportID}' target='_blank' rel='noreferrer noopener'>${policyName}</a> workspace`,
text: `moved this report to the ${policyName} workspace`,
type: CONST.REPORT.MESSAGE.TYPE.COMMENT,
},
Expand Down Expand Up @@ -6836,8 +6839,11 @@ function buildOptimisticTransactionAction(
targetReportID: string,
): ReportAction {
const reportName = allReports?.[targetReportID]?.reportName ?? '';
const url = getReportURLForCurrentContext(targetReportID);
const [actionText, messageHtml] = [`moved this expense to ${reportName}`, `moved this expense to <a href='${url}' target='_blank' rel='noreferrer noopener'>${reportName}</a>`];
const url = `${environmentURL}/r/${targetReportID}`;
const [actionText, messageHtml] =
type === CONST.REPORT.ACTIONS.TYPE.MOVED_TRANSACTION
? [`moved this expense to ${reportName}`, `moved this expense to <a href='${url}' target='_blank' rel='noreferrer noopener'>${reportName}</a>`]
: ['moved this expense to your personal space', 'moved this expense to your personal space'];

return {
actionName: type,
Expand Down Expand Up @@ -11791,15 +11797,6 @@ function getReportStatusTranslation(stateNum?: number, statusNum?: number): stri
return '';
}

function getReportURLForCurrentContext(reportID: string | undefined): string {
if (!reportID) {
return `${environmentURL}/r/`;
}
const isInSearchContext = isSearchTopmostFullScreenRoute();
const relativePath = isInSearchContext ? ROUTES.SEARCH_MONEY_REQUEST_REPORT.getRoute({reportID}) : ROUTES.REPORT_WITH_ID.getRoute(reportID);
return `${environmentURL}/${relativePath}`;
}

export {
areAllRequestsBeingSmartScanned,
buildOptimisticAddCommentReportAction,
Expand Down Expand Up @@ -12190,7 +12187,6 @@ export {
isWorkspaceThread,
isMoneyRequestReportEligibleForMerge,
getReportStatusTranslation,
getReportURLForCurrentContext,
getMovedActionMessage,
excludeParticipantsForDisplay,
getReportName,
Expand Down
44 changes: 0 additions & 44 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import useReportIsArchived from '@hooks/useReportIsArchived';
import {putOnHold} from '@libs/actions/IOU';
import type {OnboardingTaskLinks} from '@libs/actions/Welcome/OnboardingFlow';
import DateUtils from '@libs/DateUtils';
import {getEnvironmentURL} from '@libs/Environment/Environment';
import getBase62ReportID from '@libs/getBase62ReportID';
import {translateLocal} from '@libs/Localize';
import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute';
import {getOriginalMessage, isWhisperAction} from '@libs/ReportActionsUtils';
import {
buildOptimisticChatReport,
Expand Down Expand Up @@ -56,7 +54,6 @@ import {
getReportIDFromLink,
getReportName,
getReportStatusTranslation,
getReportURLForCurrentContext,
getSearchReportName,
getWorkspaceIcon,
getWorkspaceNameUpdatedMessage,
Expand Down Expand Up @@ -89,7 +86,6 @@ import {buildOptimisticTransaction} from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {
Beta,
OnyxInputOrEntry,
Expand Down Expand Up @@ -140,8 +136,6 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
// Be sure to include the mocked permissions library or else the beta tests won't work
jest.mock('@libs/Permissions');

jest.mock('@libs/Navigation/helpers/isSearchTopmostFullScreenRoute', () => jest.fn());

jest.mock('@libs/Navigation/Navigation', () => ({
setNavigationActionToMicrotaskQueue: jest.fn(),
navigationRef: {
Expand Down Expand Up @@ -6971,44 +6965,6 @@ describe('ReportUtils', () => {
});
});

describe('getReportURLForCurrentContext', () => {
const flushPromises = () =>
new Promise<void>((resolve) => {
setImmediate(resolve);
});
const mockIsSearchTopmostFullScreenRoute = jest.mocked(isSearchTopmostFullScreenRoute);
let environmentURL: string;

beforeAll(async () => {
environmentURL = await getEnvironmentURL();
await flushPromises();
});

afterAll(() => {
mockIsSearchTopmostFullScreenRoute.mockRestore();
});

beforeEach(() => {
mockIsSearchTopmostFullScreenRoute.mockReset();
mockIsSearchTopmostFullScreenRoute.mockReturnValue(false);
});

it('returns report route when not in search context', () => {
const reportID = '123';
expect(getReportURLForCurrentContext(reportID)).toBe(`${environmentURL}/${ROUTES.REPORT_WITH_ID.getRoute(reportID)}`);
});

it('returns search route when in search context', () => {
const reportID = '456';
mockIsSearchTopmostFullScreenRoute.mockReturnValue(true);
expect(getReportURLForCurrentContext(reportID)).toBe(`${environmentURL}/${ROUTES.SEARCH_MONEY_REQUEST_REPORT.getRoute({reportID})}`);
});

it('falls back to the base report path when reportID is missing', () => {
expect(getReportURLForCurrentContext(undefined)).toBe(`${environmentURL}/r/`);
});
});

describe('requiresManualSubmission', () => {
it('should return true when manual submit is enabled', () => {
const report: Report = {
Expand Down