diff --git a/src/pages/workspace/accounting/PolicyAccountingPage.tsx b/src/pages/workspace/accounting/PolicyAccountingPage.tsx index 48888c054813..8fb3fd57d6e7 100644 --- a/src/pages/workspace/accounting/PolicyAccountingPage.tsx +++ b/src/pages/workspace/accounting/PolicyAccountingPage.tsx @@ -33,9 +33,10 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {Policy, PolicyConnectionSyncProgress} from '@src/types/onyx'; -import type {PolicyConnectionName, Tenant} from '@src/types/onyx/Policy'; +import type {PolicyConnectionName} from '@src/types/onyx/Policy'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type IconAsset from '@src/types/utils/IconAsset'; +import { findCurrentXeroOrganization, getXeroTenants } from '@libs/PolicyUtils'; type PolicyAccountingPageOnyxProps = { connectionSyncProgress: OnyxEntry; @@ -116,15 +117,9 @@ function PolicyAccountingPage({policy, connectionSyncProgress}: PolicyAccounting const policyConnectedToXero = connectedIntegration === CONST.POLICY.CONNECTIONS.NAME.XERO; - const tenants = useMemo(() => { - // Due to the way optional chain is being handled in this useMemo we are forced to use this approach to properly handle undefined values - // eslint-disable-next-line @typescript-eslint/prefer-optional-chain - if (!policy || !policy.connections || !policy.connections.xero || !policy.connections.xero.data) { - return []; - } - return policy?.connections?.xero?.data?.tenants ?? []; - }, [policy]); - const currentXeroOrganization = tenants.find((tenant) => tenant.id === policy?.connections?.xero.config.tenantID); + const tenants = useMemo(() => getXeroTenants(policy), [policy]); + + const currentXeroOrganization = findCurrentXeroOrganization(tenants, policy?.connections?.xero?.config?.tenantID); const overflowMenu: ThreeDotsMenuProps['menuItems'] = useMemo( () => [ diff --git a/src/pages/workspace/accounting/xero/XeroImportPage.tsx b/src/pages/workspace/accounting/xero/XeroImportPage.tsx index af36bfcc42cd..0c842d70c93a 100644 --- a/src/pages/workspace/accounting/xero/XeroImportPage.tsx +++ b/src/pages/workspace/accounting/xero/XeroImportPage.tsx @@ -13,7 +13,7 @@ import withPolicy from '@pages/workspace/withPolicy'; import type {WithPolicyProps} from '@pages/workspace/withPolicy'; import CONST from '@src/CONST'; import ROUTES from '@src/ROUTES'; -import type {Tenant} from '@src/types/onyx/Policy'; +import { getXeroTenants } from '@libs/PolicyUtils'; function XeroImportPage({policy}: WithPolicyProps) { const {translate} = useLocalize(); @@ -22,7 +22,7 @@ function XeroImportPage({policy}: WithPolicyProps) { const policyID = policy?.id ?? ''; const {importCustomers, importTaxRates, importTrackingCategories, pendingFields} = policy?.connections?.xero?.config ?? {}; - const tenants = useMemo(() => policy?.connections?.xero?.data?.tenants ?? [], [policy?.connections?.xero.data.tenants]); + const tenants = useMemo(() => getXeroTenants(policy ?? undefined), [policy]); const currentXeroOrganization = tenants.find((tenant) => tenant.id === policy?.connections?.xero.config.tenantID); const sections = useMemo(