Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import useOnyx from '@hooks/useOnyx';
import useSubPage from '@hooks/useSubPage';
import type {SubPageProps} from '@hooks/useSubPage/types';
import Navigation from '@libs/Navigation/Navigation';
import getCurrencyForNonUSDBankAccount from '@pages/ReimbursementAccount/NonUSD/utils/getCurrencyForNonUSDBankAccount';
import getNeededDocumentsStatusForBeneficialOwner from '@pages/ReimbursementAccount/NonUSD/utils/getNeededDocumentsStatusForBeneficialOwner';
import {clearErrors, setDraftValues} from '@userActions/FormActions';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import INPUT_IDS from '@src/types/form/ReimbursementAccountForm';
import SafeString from '@src/utils/SafeString';
import Address from './BeneficialOwnerDetailsFormSubSteps/Address';
import Confirmation from './BeneficialOwnerDetailsFormSubSteps/Confirmation';
Expand Down Expand Up @@ -59,6 +60,8 @@ type BeneficialOwnerDetailsFormPagesProps = {
function BeneficialOwnerDetailsFormPages({stepNames, policyID, onFinished, backTo}: BeneficialOwnerDetailsFormPagesProps) {
const {translate} = useLocalize();
const [reimbursementAccountDraft] = useOnyx(ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM_DRAFT);
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
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-11 (docs)

useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT) subscribes to the entire reimbursement account object, but only achData.country is used (via getCurrencyForNonUSDBankAccount). This causes unnecessary re-renders when any unrelated field on the reimbursement account changes.

Use a selector to extract only the needed field:

const [reimbursementAccountCountry] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {
    selector: (ra) => ra?.achData?.additionalData?.country,
});

Then pass the extracted value directly instead of the full object, or restructure getCurrencyForNonUSDBankAccount to accept the primitive values it actually needs.


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

const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
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-11 (docs)

useOnyx(ONYXKEYS.COLLECTION.POLICY + policyID) subscribes to the entire policy object, but only outputCurrency is used (via getCurrencyForNonUSDBankAccount). Policy objects are large and change frequently, causing unnecessary re-renders.

Use a selector to extract only the needed field:

const [outputCurrency] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
    selector: (p) => p?.outputCurrency,
});

Then pass the extracted primitive value directly instead of the full policy object.


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


const ownerBeingModifiedID = reimbursementAccountDraft?.ownerBeingModifiedID ?? CONST.NON_USD_BANK_ACCOUNT.CURRENT_USER_KEY;
const isUserEnteringHisOwnData = ownerBeingModifiedID === CONST.NON_USD_BANK_ACCOUNT.CURRENT_USER_KEY;
Expand All @@ -67,7 +70,7 @@ function BeneficialOwnerDetailsFormPages({stepNames, policyID, onFinished, backT

const beneficialOwnerNationalityInputID = `${PREFIX}_${ownerBeingModifiedID}_${NATIONALITY}` as const;
const beneficialOwnerNationality = SafeString(reimbursementAccountDraft?.[beneficialOwnerNationalityInputID]);
const countryStepCountryValue = reimbursementAccountDraft?.[INPUT_IDS.ADDITIONAL_DATA.COUNTRY] ?? '';
const {country: countryStepCountryValue, currency} = getCurrencyForNonUSDBankAccount(policy, reimbursementAccountDraft, reimbursementAccount);

const totalOwnedPercentage = Object.fromEntries(
ownerKeys.map((key) => {
Expand All @@ -81,11 +84,12 @@ function BeneficialOwnerDetailsFormPages({stepNames, policyID, onFinished, backT
if (beneficialOwnerNationality !== CONST.COUNTRY.US) {
pagesToSkip.push(SUB_PAGE_NAMES.LAST_4_SSN);
}
if (countryStepCountryValue === CONST.COUNTRY.GB && beneficialOwnerNationality === CONST.COUNTRY.GB) {
const documentsStatus = getNeededDocumentsStatusForBeneficialOwner(currency, countryStepCountryValue, beneficialOwnerNationality);
if (!documentsStatus.isProofOfOwnershipNeeded && !documentsStatus.isCopyOfIDNeeded && !documentsStatus.isProofOfAddressNeeded && !documentsStatus.isCodiceFiscaleNeeded) {
pagesToSkip.push(SUB_PAGE_NAMES.DOCUMENTS);
}
return pagesToSkip;
}, [beneficialOwnerNationality, countryStepCountryValue]);
}, [beneficialOwnerNationality, countryStepCountryValue, currency]);

const buildRoute = useCallback(
(pageName: string, action?: 'edit') => ROUTES.BANK_ACCOUNT_NON_USD_SETUP.getRoute({policyID, page: PAGE_NAME.BENEFICIAL_OWNER_INFO, subPage: pageName, action, backTo}),
Expand Down
Loading