Skip to content
Merged
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
32 changes: 15 additions & 17 deletions src/pages/iou/request/IOURequestRedirectToStartPage.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import ScreenWrapper from '@components/ScreenWrapper';

import {startDistanceRequest, startMoneyRequest} from '@libs/actions/IOU/MoneyRequest';
import Navigation from '@libs/Navigation/Navigation';
import {generateReportID} from '@libs/ReportUtils';
import {isDistanceExpenseType} from '@libs/TransactionUtils';

import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';

import React, {useEffect} from 'react';
import React, {useEffect, useRef} from 'react';

import type {WithWritableReportOrNotFoundProps} from './step/withWritableReportOrNotFound';

type IOURequestRedirectToStartPageProps = WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.START>;

function IOURequestRedirectToStartPage({route}: IOURequestRedirectToStartPageProps) {
const didRedirectRef = useRef(false);
const {iouType, iouRequestType} = route.params ?? {};
const isIouTypeValid = Object.values(CONST.IOU.TYPE).includes(iouType);
const isIouRequestTypeValid = Object.values(CONST.IOU.REQUEST_TYPE).includes(iouRequestType);
const isSplitDistanceSubtype = iouType === CONST.IOU.TYPE.SPLIT && iouRequestType !== CONST.IOU.REQUEST_TYPE.DISTANCE && isDistanceExpenseType(iouRequestType);
const shouldShowNotFound = !isIouTypeValid || !isIouRequestTypeValid || isSplitDistanceSubtype;

useEffect(() => {
if (!isIouTypeValid || !isIouRequestTypeValid) {
if (shouldShowNotFound || didRedirectRef.current) {
return;
}
didRedirectRef.current = true;

// Dismiss this modal because the redirects below will open a new modal and there shouldn't be two modals stacked on top of each other.
Navigation.dismissModal();

// Redirect the person to the right start page using a random reportID
const optimisticReportID = generateReportID();
if (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE) {
Navigation.navigate(ROUTES.MONEY_REQUEST_CREATE_TAB_DISTANCE.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
} else if (iouRequestType === CONST.IOU.REQUEST_TYPE.MANUAL) {
Navigation.navigate(ROUTES.MONEY_REQUEST_CREATE_TAB_MANUAL.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
} else if (iouRequestType === CONST.IOU.REQUEST_TYPE.SCAN) {
Navigation.navigate(ROUTES.MONEY_REQUEST_CREATE_TAB_SCAN.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
} else if (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE_MAP) {
Navigation.navigate(ROUTES.DISTANCE_REQUEST_CREATE_TAB_MAP.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
} else if (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE_MANUAL) {
Navigation.navigate(ROUTES.DISTANCE_REQUEST_CREATE_TAB_MANUAL.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
} else if (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE_GPS) {
Navigation.navigate(ROUTES.DISTANCE_REQUEST_CREATE_TAB_GPS.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
if (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE && iouType !== CONST.IOU.TYPE.SPLIT) {
startDistanceRequest(iouType, optimisticReportID, undefined);

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-16 (docs)

This effect runs once on mount ([] dependency array) and performs non-idempotent work: it dismisses the modal and then triggers a redirect via startDistanceRequest/startMoneyRequest/Navigation.navigate. React Strict Mode intentionally double-invokes effects in development, so these navigation/request-start calls can fire twice, potentially opening two flows or starting duplicate money requests. The existing if (shouldShowNotFound) return; is a precondition check, not an execution guard — it does not prevent the redirect logic from running again on a second invocation.

Add a module-level (or ref-based) guard that both checks and sets a flag before performing the redirect, e.g.:

let didRedirect = false;

// inside the effect
if (shouldShowNotFound) {
    return;
}
if (didRedirect) {
    return;
}
didRedirect = true;
Navigation.dismissModal();
// ...redirect logic

Reviewed at: 6653af4 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

} else if (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE_ODOMETER) {
Navigation.navigate(ROUTES.DISTANCE_REQUEST_CREATE_TAB_ODOMETER.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
} else if (iouRequestType === CONST.IOU.REQUEST_TYPE.TIME) {
Navigation.navigate(ROUTES.MONEY_REQUEST_CREATE_TAB_TIME.getRoute(CONST.IOU.ACTION.CREATE, iouType, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, optimisticReportID));
} else if (iouRequestType !== CONST.IOU.REQUEST_TYPE.DISTANCE && isDistanceExpenseType(iouRequestType)) {
startDistanceRequest(iouType, optimisticReportID, undefined, iouRequestType);
} else {
startMoneyRequest(iouType, optimisticReportID, undefined, iouRequestType);
}

// This useEffect should only run on mount which is why there are no dependencies being passed in the second parameter
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

if (!isIouTypeValid || !isIouRequestTypeValid) {
if (shouldShowNotFound) {
return (
<ScreenWrapper testID="IOURequestRedirectToStartPage">
<FullPageNotFoundView shouldShow />
Expand Down
Loading