From c7b8fe0bd910cc9495d1d0e069986e14c6918d5f Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 14 Jul 2025 12:54:39 -0600 Subject: [PATCH] Revert "[CP Staging] fix: dont use snapshot data in search page in settlement button" --- src/components/SettlementButton/index.tsx | 12 +++--------- src/libs/PolicyUtils.ts | 4 ++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/components/SettlementButton/index.tsx b/src/components/SettlementButton/index.tsx index 41564a5e83560..36f05a780dd4f 100644 --- a/src/components/SettlementButton/index.tsx +++ b/src/components/SettlementButton/index.tsx @@ -12,7 +12,6 @@ import {LockedAccountContext} from '@components/LockedAccountModalProvider'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; -import usePolicy from '@hooks/usePolicy'; import useThemeStyles from '@hooks/useThemeStyles'; import {isCurrencySupportedForDirectReimbursement} from '@libs/actions/Policy/Policy'; import {getCurrentUserAccountID} from '@libs/actions/Report'; @@ -89,7 +88,6 @@ function SettlementButton({ const styles = useThemeStyles(); const {translate} = useLocalize(); const {isOffline} = useNetwork(); - // The app would crash due to subscribing to the entire report collection if chatReportID is an empty string. So we should have a fallback ID here. // eslint-disable-next-line rulesdir/no-default-id-values const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID || CONST.DEFAULT_NUMBER_ID}`, {canBeMissing: true}); @@ -109,18 +107,14 @@ function SettlementButton({ const [fundList = {}] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true}); const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true}); const currentUserAccountID = getCurrentUserAccountID().toString(); - - // Passing `undefined` to ensure we use the Onyx connection from PolicyUtils, - // since `useOnyx` only returns snapshot data on the SearchPage. - // By passing `undefined`, we fall back to the function's default value. - const activeAdminPolicies = getActiveAdminWorkspaces(undefined, currentUserAccountID).sort((a, b) => (a.name || '').localeCompare(b.name || '')); + const activeAdminPolicies = getActiveAdminWorkspaces(policies, currentUserAccountID).sort((a, b) => (a.name || '').localeCompare(b.name || '')); const reportID = iouReport?.reportID; const hasPreferredPaymentMethod = !!lastPaymentMethod; const isLoadingLastPaymentMethod = isLoadingOnyxValue(lastPaymentMethodResult); const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; - const lastPaymentPolicy = usePolicy(lastPaymentMethod); - + const isLastPaymentPolicy = !Object.values({...CONST.PAYMENT_METHODS, ...CONST.IOU.PAYMENT_TYPE}).includes(lastPaymentMethod as PaymentMethod); + const lastPaymentPolicy = isLastPaymentPolicy ? policies?.[`${ONYXKEYS.COLLECTION.POLICY}${lastPaymentMethod}`] : undefined; const [bankAccountList = {}] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true}); const bankAccount = bankAccountList[lastBankAccountID ?? CONST.DEFAULT_NUMBER_ID]; const isExpenseReport = isExpenseReportUtil(iouReport); diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 88f77ab522a84..e4abd2304f661 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -755,8 +755,8 @@ function getPolicy(policyID: string | undefined, policies: OnyxCollection | null | undefined, currentUserLogin: string | undefined): Policy[] { - const activePolicies = getActivePolicies(policies === undefined ? allPolicies : policies, currentUserLogin); +function getActiveAdminWorkspaces(policies: OnyxCollection | null, currentUserLogin: string | undefined): Policy[] { + const activePolicies = getActivePolicies(policies, currentUserLogin); return activePolicies.filter((policy) => shouldShowPolicy(policy, isOfflineNetworkStore(), currentUserLogin) && isPolicyAdmin(policy, currentUserLogin)); }