Skip to content

Commit 9ced757

Browse files
committed
fix: prevent use of snapshot policies onyx data
1 parent ee1b6da commit 9ced757

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/components/SettlementButton/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {useSearchContext} from '@components/Search/SearchContext';
1313
import useLocalize from '@hooks/useLocalize';
1414
import useNetwork from '@hooks/useNetwork';
1515
import useOnyx from '@hooks/useOnyx';
16+
import usePolicy from '@hooks/usePolicy';
1617
import useThemeStyles from '@hooks/useThemeStyles';
1718
import {isCurrencySupportedForDirectReimbursement} from '@libs/actions/Policy/Policy';
1819
import {getCurrentUserAccountID} from '@libs/actions/Report';
@@ -110,14 +111,18 @@ function SettlementButton({
110111
const [fundList = {}] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true});
111112
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
112113
const currentUserAccountID = getCurrentUserAccountID().toString();
113-
const activeAdminPolicies = getActiveAdminWorkspaces(!isOnSearch ? policies : undefined, currentUserAccountID).sort((a, b) => (a.name || '').localeCompare(b.name || ''));
114+
115+
// Passing `undefined` to ensure we use the Onyx connection from PolicyUtils,
116+
// since `useOnyx` only returns snapshot data on the SearchPage.
117+
// By passing `undefined`, we fall back to the function's default value.
118+
const activeAdminPolicies = getActiveAdminWorkspaces(undefined, currentUserAccountID).sort((a, b) => (a.name || '').localeCompare(b.name || ''));
114119
const reportID = iouReport?.reportID;
115120

116121
const hasPreferredPaymentMethod = !!lastPaymentMethod;
117122
const isLoadingLastPaymentMethod = isLoadingOnyxValue(lastPaymentMethodResult);
118123
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
119-
const isLastPaymentPolicy = !Object.values({...CONST.PAYMENT_METHODS, ...CONST.IOU.PAYMENT_TYPE}).includes(lastPaymentMethod as PaymentMethod);
120-
const lastPaymentPolicy = isLastPaymentPolicy ? policies?.[`${ONYXKEYS.COLLECTION.POLICY}${lastPaymentMethod}`] : undefined;
124+
const lastPaymentPolicy = usePolicy(lastPaymentMethod);
125+
121126
const [bankAccountList = {}] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true});
122127
const bankAccount = bankAccountList[lastBankAccountID ?? CONST.DEFAULT_NUMBER_ID];
123128
const isExpenseReport = isExpenseReportUtil(iouReport);

src/libs/PolicyUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ function getPolicy(policyID: string | undefined, policies: OnyxCollection<Policy
755755
}
756756

757757
/** Return active policies where current user is an admin */
758-
function getActiveAdminWorkspaces(policies: OnyxCollection<Policy> | null = allPolicies, currentUserLogin: string | undefined): Policy[] {
758+
function getActiveAdminWorkspaces(policies: OnyxCollection<Policy> | null | undefined = allPolicies, currentUserLogin: string | undefined): Policy[] {
759759
const activePolicies = getActivePolicies(policies, currentUserLogin);
760760
return activePolicies.filter((policy) => shouldShowPolicy(policy, isOfflineNetworkStore(), currentUserLogin) && isPolicyAdmin(policy, currentUserLogin));
761761
}

0 commit comments

Comments
 (0)