From a0c7f8f38e8793178b29916ff69e791d455a5caa Mon Sep 17 00:00:00 2001 From: paulnjs Date: Thu, 18 Sep 2025 14:44:06 +0700 Subject: [PATCH 1/6] remove inline selector bundle A --- .../MoneyRequestConfirmationList.tsx | 6 ++++-- src/components/PriorityModeController.tsx | 6 +++++- .../ReportActionItem/MoneyRequestView.tsx | 3 ++- src/hooks/useDefaultFundID.ts | 20 ++++++++++++++---- src/hooks/useExpensifyCardFeeds.ts | 21 +++++++++++++++---- src/hooks/useSearchTypeMenuSections.ts | 6 ++++-- src/hooks/useWorkspaceAccountID.ts | 6 +++++- src/pages/Debug/DebugTagPicker.tsx | 6 +++++- src/pages/Search/EmptySearchView.tsx | 5 ++++- .../FloatingActionButtonAndPopover.tsx | 4 +++- .../step/IOURequestEditReportCommon.tsx | 8 ++++++- .../request/step/IOURequestStepCategory.tsx | 3 ++- .../step/IOURequestStepParticipants.tsx | 6 ++++-- .../iou/request/step/IOURequestStepTag.tsx | 3 ++- .../step/IOURequestStepTaxRatePage.tsx | 3 ++- .../settings/Preferences/PriorityModePage.tsx | 5 ++++- .../settings/Wallet/WalletPage/WalletPage.tsx | 6 +++++- src/selectors/Policy.ts | 8 +++++++ 18 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 src/selectors/Policy.ts diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 4d81084a2555..811f5680f82e 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -198,6 +198,8 @@ type MoneyRequestConfirmationListProps = { type MoneyRequestConfirmationListItem = Participant | OptionData; +const mileageRateSelector = (policy: OnyxEntry) => DistanceRequestUtils.getDefaultMileageRate(policy); + function MoneyRequestConfirmationList({ transaction, onSendMoney, @@ -244,11 +246,11 @@ function MoneyRequestConfirmationList({ const [policyReal] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: true}); const [policyDraft] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`, {canBeMissing: true}); const [defaultMileageRateDraft] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`, { - selector: (selectedPolicy) => DistanceRequestUtils.getDefaultMileageRate(selectedPolicy), + selector: mileageRateSelector, canBeMissing: true, }); const [defaultMileageRateReal] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { - selector: (selectedPolicy) => DistanceRequestUtils.getDefaultMileageRate(selectedPolicy), + selector: mileageRateSelector, canBeMissing: true, }); const [policyCategoriesDraft] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT}${policyID}`, {canBeMissing: true}); diff --git a/src/components/PriorityModeController.tsx b/src/components/PriorityModeController.tsx index 5b00f069aefa..7625c4c2fc7a 100644 --- a/src/components/PriorityModeController.tsx +++ b/src/components/PriorityModeController.tsx @@ -1,6 +1,8 @@ import {useNavigation} from '@react-navigation/native'; import {accountIDSelector} from '@selectors/Session'; import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; +import type {OnyxEntry} from 'react-native-onyx'; +import type {ValueOf} from 'type-fest'; import useOnyx from '@hooks/useOnyx'; import {updateChatPriorityMode} from '@libs/actions/User'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; @@ -13,6 +15,8 @@ import SCREENS from '@src/SCREENS'; import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import FocusModeNotification from './FocusModeNotification'; +const isInFocusModeSelector = (priorityMode: OnyxEntry>) => priorityMode === CONST.PRIORITY_MODE.GSD; + /** * This component is used to automatically switch a user into #focus mode when they exceed a certain number of reports. * We do this primarily for performance reasons. Similar to the "Welcome action" we must wait for a number of things to @@ -27,7 +31,7 @@ import FocusModeNotification from './FocusModeNotification'; export default function PriorityModeController() { const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector, canBeMissing: true}); const [isLoadingReportData] = useOnyx(ONYXKEYS.IS_LOADING_REPORT_DATA, {canBeMissing: true}); - const [isInFocusMode, isInFocusModeMetadata] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {selector: (priorityMode) => priorityMode === CONST.PRIORITY_MODE.GSD, canBeMissing: true}); + const [isInFocusMode, isInFocusModeMetadata] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {selector: isInFocusModeSelector, canBeMissing: true}); const [hasTriedFocusMode, hasTriedFocusModeMetadata] = useOnyx(ONYXKEYS.NVP_TRY_FOCUS_MODE, {canBeMissing: true}); const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true}); const currentRouteName = useCurrentRouteName(); diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index 19df5bcb6bf2..e2d0e42e0bed 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -78,6 +78,7 @@ import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; +import {activePolicySelector} from '@src/selectors/Policy'; import type * as OnyxTypes from '@src/types/onyx'; import type {TransactionPendingFieldsKey} from '@src/types/onyx/Transaction'; import MoneyRequestReceiptView from './MoneyRequestReceiptView'; @@ -146,7 +147,7 @@ function MoneyRequestView({ const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { canBeMissing: true, - selector: (policy) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined), + selector: activePolicySelector, }); // If the expense is unreported the policy should be the user's default policy, otherwise it should be the policy the expense was made for const policy = isExpenseUnreported ? activePolicy : expensePolicy; diff --git a/src/hooks/useDefaultFundID.ts b/src/hooks/useDefaultFundID.ts index 3daa3fd2651b..defa9d063eed 100644 --- a/src/hooks/useDefaultFundID.ts +++ b/src/hooks/useDefaultFundID.ts @@ -1,6 +1,9 @@ +import {useCallback} from 'react'; +import type {OnyxCollection} from 'react-native-onyx'; import {getFundIdFromSettingsKey} from '@libs/CardUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {ExpensifyCardSettings} from '@src/types/onyx'; import useOnyx from './useOnyx'; import useWorkspaceAccountID from './useWorkspaceAccountID'; @@ -13,8 +16,8 @@ function useDefaultFundID(policyID: string | undefined) { const [lastSelectedExpensifyCardFeed] = useOnyx(`${ONYXKEYS.COLLECTION.LAST_SELECTED_EXPENSIFY_CARD_FEED}${policyID}`, {canBeMissing: true}); const [lastSelectedCardSettings] = useOnyx(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${lastSelectedExpensifyCardFeed}`, {canBeMissing: true}); - const [domainFundID] = useOnyx(ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS, { - selector: (cardSettings) => { + const getDomainFundID = useCallback( + (cardSettings: OnyxCollection) => { const matchingKey = Object.entries(cardSettings ?? {}).find( // eslint-disable-next-line @typescript-eslint/no-unused-vars ([key, settings]) => settings?.preferredPolicy && settings.preferredPolicy === policyID && !key.includes(workspaceAccountID.toString()), @@ -22,8 +25,17 @@ function useDefaultFundID(policyID: string | undefined) { return getFundIdFromSettingsKey(matchingKey?.[0] ?? ''); }, - canBeMissing: true, - }); + [policyID, workspaceAccountID], + ); + + const [domainFundID] = useOnyx( + ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS, + { + selector: getDomainFundID, + canBeMissing: true, + }, + [getDomainFundID], + ); if (lastSelectedExpensifyCardFeed && lastSelectedCardSettings?.paymentBankAccountID) { return lastSelectedExpensifyCardFeed; diff --git a/src/hooks/useExpensifyCardFeeds.ts b/src/hooks/useExpensifyCardFeeds.ts index d7c72afd925e..760dfc104441 100644 --- a/src/hooks/useExpensifyCardFeeds.ts +++ b/src/hooks/useExpensifyCardFeeds.ts @@ -1,11 +1,15 @@ +import {useCallback} from 'react'; +import type {OnyxCollection} from 'react-native-onyx'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {ExpensifyCardSettings} from '@src/types/onyx'; import useOnyx from './useOnyx'; import useWorkspaceAccountID from './useWorkspaceAccountID'; function useExpensifyCardFeeds(policyID: string | undefined) { const workspaceAccountID = useWorkspaceAccountID(policyID); - const [allExpensifyCardFeeds] = useOnyx(ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS, { - selector: (cardSettings) => { + + const getAllExpensifyCardFeeds = useCallback( + (cardSettings: OnyxCollection) => { const matchingEntries = Object.entries(cardSettings ?? {}).filter(([key, settings]) => { const isDomainFeed = !!(settings?.preferredPolicy && settings.preferredPolicy === policyID); const isWorkspaceFeed = key.includes(workspaceAccountID.toString()) && settings && Object.keys(settings).length > 1; @@ -14,8 +18,17 @@ function useExpensifyCardFeeds(policyID: string | undefined) { return Object.fromEntries(matchingEntries); }, - canBeMissing: true, - }); + [policyID, workspaceAccountID], + ); + + const [allExpensifyCardFeeds] = useOnyx( + ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS, + { + selector: getAllExpensifyCardFeeds, + canBeMissing: true, + }, + [getAllExpensifyCardFeeds], + ); return allExpensifyCardFeeds; } diff --git a/src/hooks/useSearchTypeMenuSections.ts b/src/hooks/useSearchTypeMenuSections.ts index 6197831ab8bc..39752c588958 100644 --- a/src/hooks/useSearchTypeMenuSections.ts +++ b/src/hooks/useSearchTypeMenuSections.ts @@ -1,5 +1,5 @@ import {useMemo} from 'react'; -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {createTypeMenuSections} from '@libs/SearchUIUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Session} from '@src/types/onyx'; @@ -28,6 +28,8 @@ const policySelector = (policy: OnyxEntry): OnyxEntry => achAccount: policy.achAccount, }; +const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); + const currentUserLoginAndAccountIDSelector = (session: OnyxEntry) => ({ email: session?.email, accountID: session?.accountID, @@ -40,7 +42,7 @@ const useSearchTypeMenuSections = () => { const {defaultCardFeed, cardFeedsByPolicy} = useCardFeedsForDisplay(); const {isOffline} = useNetwork(); - const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (policies) => mapOnyxCollectionItems(policies, policySelector), canBeMissing: true}); + const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policiesSelector, canBeMissing: true}); const [currentUserLoginAndAccountID] = useOnyx(ONYXKEYS.SESSION, {selector: currentUserLoginAndAccountIDSelector, canBeMissing: false}); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [savedSearches] = useOnyx(ONYXKEYS.SAVED_SEARCHES, {canBeMissing: true}); diff --git a/src/hooks/useWorkspaceAccountID.ts b/src/hooks/useWorkspaceAccountID.ts index 5f0bca578b83..69fdad5f8c3b 100644 --- a/src/hooks/useWorkspaceAccountID.ts +++ b/src/hooks/useWorkspaceAccountID.ts @@ -1,9 +1,13 @@ +import type {OnyxEntry} from 'react-native-onyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy} from '@src/types/onyx'; import useOnyx from './useOnyx'; +const policyWorkspaceAccountIDSelector = (policy: OnyxEntry) => policy?.workspaceAccountID; + function useWorkspaceAccountID(policyID: string | undefined) { - const [workspaceAccountID = CONST.DEFAULT_NUMBER_ID] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {selector: (policy) => policy?.workspaceAccountID}); + const [workspaceAccountID = CONST.DEFAULT_NUMBER_ID] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {selector: policyWorkspaceAccountIDSelector}); return workspaceAccountID; } diff --git a/src/pages/Debug/DebugTagPicker.tsx b/src/pages/Debug/DebugTagPicker.tsx index 209f86777744..d41e4a666c61 100644 --- a/src/pages/Debug/DebugTagPicker.tsx +++ b/src/pages/Debug/DebugTagPicker.tsx @@ -1,5 +1,6 @@ import React, {useCallback, useMemo, useState} from 'react'; import {View} from 'react-native'; +import type {OnyxEntry} from 'react-native-onyx'; import Button from '@components/Button'; import type {ListItem} from '@components/SelectionList/types'; import TagPicker from '@components/TagPicker'; @@ -11,6 +12,7 @@ import {insertTagIntoTransactionTagsString} from '@libs/IOUUtils'; import {getTagLists} from '@libs/PolicyUtils'; import {getTagArrayFromName} from '@libs/TransactionUtils'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy} from '@src/types/onyx'; type DebugTagPickerProps = { /** The policyID we are getting tags for */ @@ -23,6 +25,8 @@ type DebugTagPickerProps = { onSubmit: (item: ListItem) => void; }; +const policySelector = (policy: OnyxEntry) => policy?.hasMultipleTagLists; + function DebugTagPicker({policyID, tagName = '', onSubmit}: DebugTagPickerProps) { const styles = useThemeStyles(); const {translate} = useLocalize(); @@ -31,7 +35,7 @@ function DebugTagPicker({policyID, tagName = '', onSubmit}: DebugTagPickerProps) const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, {canBeMissing: true}); const policyTagLists = useMemo(() => getTagLists(policyTags), [policyTags]); - const [hasMultipleTagLists] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: true, selector: (policy) => policy?.hasMultipleTagLists}); + const [hasMultipleTagLists] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: true, selector: policySelector}); const updateTagName = useCallback( (index: number) => ({text}: ListItem) => { diff --git a/src/pages/Search/EmptySearchView.tsx b/src/pages/Search/EmptySearchView.tsx index 67e2b3f0d703..d4fc44edf938 100644 --- a/src/pages/Search/EmptySearchView.tsx +++ b/src/pages/Search/EmptySearchView.tsx @@ -88,6 +88,9 @@ const tripsFeatures: FeatureListItem[] = [ }, ]; +const policiesSelector = (policies: OnyxCollection) => + Object.values(policies ?? {}).some((policy) => isPaidGroupPolicy(policy) && isPolicyMember(policy, currentUserPersonalDetails.login)); + function EmptySearchView({similarSearchHash, type, groupBy, hasResults}: EmptySearchViewProps) { const currentUserPersonalDetails = useCurrentUserPersonalDetails(); const {typeMenuSections} = useSearchTypeMenuSections(); @@ -95,7 +98,7 @@ function EmptySearchView({similarSearchHash, type, groupBy, hasResults}: EmptySe const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false}); const [isUserPaidPolicyMember = false] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { canBeMissing: true, - selector: (policies) => Object.values(policies ?? {}).some((policy) => isPaidGroupPolicy(policy) && isPolicyMember(policy, currentUserPersonalDetails.login)), + selector: policiesSelector, }); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, {canBeMissing: true}); diff --git a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx index a6934c2b00ab..0fb29228989c 100644 --- a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx +++ b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx @@ -89,6 +89,8 @@ const policySelector = (policy: OnyxEntry): PolicySelector => areInvoicesEnabled: policy.areInvoicesEnabled, }) as PolicySelector; +const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); + const sessionSelector = (session: OnyxEntry) => ({email: session?.email, accountID: session?.accountID}); /** @@ -116,7 +118,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT return policyChatsForActivePolicy.length > 0 ? policyChatsForActivePolicy.at(0) : ({} as OnyxTypes.Report); }, [activePolicy, activePolicyID, session?.accountID, allReports]); const [quickActionPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${quickActionReport?.policyID}`, {canBeMissing: true}); - const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (c) => mapOnyxCollectionItems(c, policySelector), canBeMissing: true}); + const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policiesSelector, canBeMissing: true}); const [lastDistanceExpenseType] = useOnyx(ONYXKEYS.NVP_LAST_DISTANCE_EXPENSE_TYPE, {canBeMissing: true}); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); const {isDelegateAccessRestricted, showDelegateNoAccessModal} = useContext(DelegateNoAccessContext); diff --git a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx index 766893ed7ea2..b0b9a4c3ca79 100644 --- a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx +++ b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx @@ -1,4 +1,5 @@ import React, {useMemo} from 'react'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import * as Expensicons from '@components/Icon/Expensicons'; import MenuItem from '@components/MenuItem'; import {useOptionsList} from '@components/OptionListContextProvider'; @@ -17,6 +18,7 @@ import {getOutstandingReportsForUser, getPolicyName, isIOUReport, isOpenReport, import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; +import type {Policy} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; import StepScreenWrapper from './StepScreenWrapper'; @@ -38,6 +40,10 @@ type Props = { shouldShowNotFoundPage?: boolean; }; +const policySelector = (policy: OnyxEntry) => policy?.id; + +const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); + function IOURequestEditReportCommon({ backTo, transactionIDs, @@ -57,7 +63,7 @@ function IOURequestEditReportCommon({ const reportOwnerAccountID = useMemo(() => selectedReport?.ownerAccountID ?? currentUserPersonalDetails.accountID, [selectedReport, currentUserPersonalDetails.accountID]); const reportPolicy = usePolicy(selectedReport?.policyID); const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: true}); - const [allPoliciesID] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (policies) => mapOnyxCollectionItems(policies, (policy) => policy?.id), canBeMissing: false}); + const [allPoliciesID] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policiesSelector, canBeMissing: false}); const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState(''); const isOwner = selectedReport ? selectedReport.ownerAccountID === currentUserPersonalDetails.accountID : false; diff --git a/src/pages/iou/request/step/IOURequestStepCategory.tsx b/src/pages/iou/request/step/IOURequestStepCategory.tsx index b549595aeee1..a6d65603349a 100644 --- a/src/pages/iou/request/step/IOURequestStepCategory.tsx +++ b/src/pages/iou/request/step/IOURequestStepCategory.tsx @@ -28,6 +28,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; +import {activePolicySelector} from '@src/selectors/Policy'; import StepScreenWrapper from './StepScreenWrapper'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; @@ -56,7 +57,7 @@ function IOURequestStepCategory({ const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { canBeMissing: true, - selector: (policy) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined), + selector: activePolicySelector, }); const isExpenseUnreported = isExpenseUnreportedTransactionUtils(transaction); const policy = isExpenseUnreported ? activePolicy : (policyReal ?? policyDraft); diff --git a/src/pages/iou/request/step/IOURequestStepParticipants.tsx b/src/pages/iou/request/step/IOURequestStepParticipants.tsx index 5c950951b0c3..276f01d55649 100644 --- a/src/pages/iou/request/step/IOURequestStepParticipants.tsx +++ b/src/pages/iou/request/step/IOURequestStepParticipants.tsx @@ -1,6 +1,6 @@ import {useIsFocused} from '@react-navigation/core'; import React, {useCallback, useEffect, useMemo, useRef} from 'react'; -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import FormHelpMessage from '@components/FormHelpMessage'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; @@ -58,6 +58,8 @@ const policySelector = (policy: OnyxEntry): OnyxEntry => customUnits: policy.customUnits, }; +const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); + type IOURequestStepParticipantsProps = WithWritableReportOrNotFoundProps & WithFullTransactionOrNotFoundProps; @@ -77,7 +79,7 @@ function IOURequestStepParticipants({ selector: (items) => Object.values(items ?? {}), canBeMissing: true, }); - const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (policies) => mapOnyxCollectionItems(policies, policySelector), canBeMissing: true}); + const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policiesSelector, canBeMissing: true}); const [lastSelectedDistanceRates] = useOnyx(ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES, {canBeMissing: true}); const transactions = useMemo(() => { const allTransactions = optimisticTransactions && optimisticTransactions.length > 1 ? optimisticTransactions : [initialTransaction]; diff --git a/src/pages/iou/request/step/IOURequestStepTag.tsx b/src/pages/iou/request/step/IOURequestStepTag.tsx index 985000b9dc33..733598e2f10b 100644 --- a/src/pages/iou/request/step/IOURequestStepTag.tsx +++ b/src/pages/iou/request/step/IOURequestStepTag.tsx @@ -22,6 +22,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; +import {activePolicySelector} from '@src/selectors/Policy'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import StepScreenWrapper from './StepScreenWrapper'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; @@ -43,7 +44,7 @@ function IOURequestStepTag({ const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { canBeMissing: true, - selector: (policy) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined), + selector: activePolicySelector, }); const isExpenseUnreported = isExpenseUnreportedTransactionUtils(transaction); const policy = isExpenseUnreported ? activePolicy : reportPolicy; diff --git a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx index 4e7c6bbfd0b0..28cea9c574fd 100644 --- a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx +++ b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx @@ -11,6 +11,7 @@ import {setDraftSplitTransaction, setMoneyRequestTaxAmount, setMoneyRequestTaxRa import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; +import {activePolicySelector} from '@src/selectors/Policy'; import type {Policy, Transaction} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import StepScreenWrapper from './StepScreenWrapper'; @@ -42,7 +43,7 @@ function IOURequestStepTaxRatePage({ const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { canBeMissing: true, - selector: (policy) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined), + selector: activePolicySelector, }); const isExpenseUnreported = isExpenseUnreportedTransactionUtils(transaction); const policy = isExpenseUnreported ? activePolicy : reportPolicy; diff --git a/src/pages/settings/Preferences/PriorityModePage.tsx b/src/pages/settings/Preferences/PriorityModePage.tsx index 0d419ec6ebc2..1fc3dc42bc9a 100644 --- a/src/pages/settings/Preferences/PriorityModePage.tsx +++ b/src/pages/settings/Preferences/PriorityModePage.tsx @@ -1,4 +1,5 @@ import React, {useCallback} from 'react'; +import type {OnyxEntry} from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import ScreenWrapper from '@components/ScreenWrapper'; @@ -21,9 +22,11 @@ type PriorityModeItem = { isSelected: boolean; }; +const priorityModeSelector = (mode: OnyxEntry>) => mode ?? CONST.PRIORITY_MODE.DEFAULT; + function PriorityModePage() { const {translate} = useLocalize(); - const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {selector: (mode) => mode ?? CONST.PRIORITY_MODE.DEFAULT}); + const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {selector: priorityModeSelector}); const styles = useThemeStyles(); const priorityModes = Object.values(CONST.PRIORITY_MODE).map((mode) => ({ value: mode, diff --git a/src/pages/settings/Wallet/WalletPage/WalletPage.tsx b/src/pages/settings/Wallet/WalletPage/WalletPage.tsx index a89d9995b379..35c60ae0109b 100644 --- a/src/pages/settings/Wallet/WalletPage/WalletPage.tsx +++ b/src/pages/settings/Wallet/WalletPage/WalletPage.tsx @@ -4,6 +4,7 @@ import type {ForwardedRef, RefObject} from 'react'; import React, {useCallback, useContext, useEffect, useLayoutEffect, useRef, useState} from 'react'; import type {GestureResponderEvent} from 'react-native'; import {ActivityIndicator, View} from 'react-native'; +import type {OnyxCollection} from 'react-native-onyx'; import ConfirmModal from '@components/ConfirmModal'; import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; @@ -56,12 +57,15 @@ type WalletPageProps = { shouldListenForResize?: boolean; }; +const fundListSelector = (allFunds: OnyxCollection) => + Object.fromEntries(Object.entries(allFunds ?? {}).filter(([, item]) => item.accountData?.additionalData?.isP2PDebitCard === true)); + function WalletPage({shouldListenForResize = false}: WalletPageProps) { const [bankAccountList = getEmptyObject()] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true}); const [cardList = getEmptyObject()] = useOnyx(ONYXKEYS.CARD_LIST, {canBeMissing: true}); const [fundList = getEmptyObject()] = useOnyx(ONYXKEYS.FUND_LIST, { canBeMissing: true, - selector: (allFunds) => Object.fromEntries(Object.entries(allFunds ?? {}).filter(([, item]) => item.accountData?.additionalData?.isP2PDebitCard === true)), + selector: fundListSelector, }); const [isLoadingPaymentMethods = true] = useOnyx(ONYXKEYS.IS_LOADING_PAYMENT_METHODS, {canBeMissing: true}); const [userWallet] = useOnyx(ONYXKEYS.USER_WALLET, {canBeMissing: true}); diff --git a/src/selectors/Policy.ts b/src/selectors/Policy.ts new file mode 100644 index 000000000000..fd98107470e1 --- /dev/null +++ b/src/selectors/Policy.ts @@ -0,0 +1,8 @@ +import type {OnyxEntry} from 'react-native-onyx'; +import CONST from '@src/CONST'; +import type {Policy} from '@src/types/onyx'; + +const activePolicySelector = (policy: OnyxEntry) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined); + +// eslint-disable-next-line import/prefer-default-export we can have other selectors in the further +export {activePolicySelector}; From 5c283f0b7b0ddda368607faf1e06e4ca7b89e8ae Mon Sep 17 00:00:00 2001 From: paulnjs Date: Mon, 22 Sep 2025 11:37:51 +0700 Subject: [PATCH 2/6] update createPoliciesSelector --- src/hooks/useSearchTypeMenuSections.ts | 4 ++-- src/hooks/useSidebarOrderedReports.tsx | 4 ++-- .../home/sidebar/FloatingActionButtonAndPopover.tsx | 4 ++-- .../iou/request/step/IOURequestEditReportCommon.tsx | 4 ++-- .../iou/request/step/IOURequestStepParticipants.tsx | 4 ++-- src/selectors/Policy.ts | 10 +++++++--- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/hooks/useSearchTypeMenuSections.ts b/src/hooks/useSearchTypeMenuSections.ts index 39752c588958..c0220b7c5ba4 100644 --- a/src/hooks/useSearchTypeMenuSections.ts +++ b/src/hooks/useSearchTypeMenuSections.ts @@ -2,8 +2,8 @@ import {useMemo} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {createTypeMenuSections} from '@libs/SearchUIUtils'; import ONYXKEYS from '@src/ONYXKEYS'; +import {createPoliciesSelector} from '@src/selectors/Policy'; import type {Policy, Session} from '@src/types/onyx'; -import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; import useCardFeedsForDisplay from './useCardFeedsForDisplay'; import useNetwork from './useNetwork'; import useOnyx from './useOnyx'; @@ -28,7 +28,7 @@ const policySelector = (policy: OnyxEntry): OnyxEntry => achAccount: policy.achAccount, }; -const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); +const policiesSelector = (policies: OnyxCollection) => createPoliciesSelector(policies, policySelector); const currentUserLoginAndAccountIDSelector = (session: OnyxEntry) => ({ email: session?.email, diff --git a/src/hooks/useSidebarOrderedReports.tsx b/src/hooks/useSidebarOrderedReports.tsx index 55fe35a9787c..10e486f14404 100644 --- a/src/hooks/useSidebarOrderedReports.tsx +++ b/src/hooks/useSidebarOrderedReports.tsx @@ -6,8 +6,8 @@ import {getPolicyEmployeeListByIdWithoutCurrentUser} from '@libs/PolicyUtils'; import SidebarUtils from '@libs/SidebarUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import {createPoliciesSelector} from '@src/selectors/Policy'; import type * as OnyxTypes from '@src/types/onyx'; -import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; import useCurrentReportID from './useCurrentReportID'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; import useDeepCompareRef from './useDeepCompareRef'; @@ -49,7 +49,7 @@ const policySelector = (policy: OnyxEntry): PartialPolicyForSi employeeList: policy.employeeList, }) as PartialPolicyForSidebar; -const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); +const policiesSelector = (policies: OnyxCollection) => createPoliciesSelector(policies, policySelector); function SidebarOrderedReportsContextProvider({ children, diff --git a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx index 1643c1b82aee..d6e6b0652eda 100644 --- a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx +++ b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx @@ -52,10 +52,10 @@ import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; +import {createPoliciesSelector} from '@src/selectors/Policy'; import type * as OnyxTypes from '@src/types/onyx'; import type {QuickActionName} from '@src/types/onyx/QuickAction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; type PolicySelector = Pick; @@ -89,7 +89,7 @@ const policySelector = (policy: OnyxEntry): PolicySelector => areInvoicesEnabled: policy.areInvoicesEnabled, }) as PolicySelector; -const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); +const policiesSelector = (policies: OnyxCollection) => createPoliciesSelector(policies, policySelector); const sessionSelector = (session: OnyxEntry) => ({email: session?.email, accountID: session?.accountID}); diff --git a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx index de8dee5deb19..bd9f1e74ce7e 100644 --- a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx +++ b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx @@ -18,9 +18,9 @@ import {getOutstandingReportsForUser, getPolicyName, isIOUReport, isOpenReport, import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; +import {createPoliciesSelector} from '@src/selectors/Policy'; import type {Policy} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; import StepScreenWrapper from './StepScreenWrapper'; type TransactionGroupListItem = ListItem & { @@ -43,7 +43,7 @@ type Props = { const policySelector = (policy: OnyxEntry) => policy?.id; -const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); +const policiesSelector = (policies: OnyxCollection) => createPoliciesSelector(policies, policySelector); function IOURequestEditReportCommon({ backTo, diff --git a/src/pages/iou/request/step/IOURequestStepParticipants.tsx b/src/pages/iou/request/step/IOURequestStepParticipants.tsx index 7bd46d26f73f..64a0c1bc7f08 100644 --- a/src/pages/iou/request/step/IOURequestStepParticipants.tsx +++ b/src/pages/iou/request/step/IOURequestStepParticipants.tsx @@ -36,11 +36,11 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; +import {createPoliciesSelector} from '@src/selectors/Policy'; import type {Policy} from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; import type Transaction from '@src/types/onyx/Transaction'; import KeyboardUtils from '@src/utils/keyboard'; -import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; import StepScreenWrapper from './StepScreenWrapper'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; @@ -59,7 +59,7 @@ const policySelector = (policy: OnyxEntry): OnyxEntry => customUnits: policy.customUnits, }; -const policiesSelector = (policies: OnyxCollection) => mapOnyxCollectionItems(policies, policySelector); +const policiesSelector = (policies: OnyxCollection) => createPoliciesSelector(policies, policySelector); type IOURequestStepParticipantsProps = WithWritableReportOrNotFoundProps & WithFullTransactionOrNotFoundProps; diff --git a/src/selectors/Policy.ts b/src/selectors/Policy.ts index fd98107470e1..4af5ae1b98e2 100644 --- a/src/selectors/Policy.ts +++ b/src/selectors/Policy.ts @@ -1,8 +1,12 @@ -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import CONST from '@src/CONST'; import type {Policy} from '@src/types/onyx'; +import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; + +type PolicySelector = (policy: OnyxEntry) => T; + +const createPoliciesSelector = (policies: OnyxCollection, policySelector: PolicySelector) => mapOnyxCollectionItems(policies, policySelector); const activePolicySelector = (policy: OnyxEntry) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined); -// eslint-disable-next-line import/prefer-default-export we can have other selectors in the further -export {activePolicySelector}; +export {activePolicySelector, createPoliciesSelector}; From 47cd1ada85480c287d3ee06995ce96568cf840fc Mon Sep 17 00:00:00 2001 From: paulnjs Date: Mon, 22 Sep 2025 22:55:41 +0700 Subject: [PATCH 3/6] fix import --- src/components/ReportActionItem/MoneyRequestView.tsx | 2 +- src/hooks/useSearchTypeMenuSections.ts | 2 +- src/hooks/useSidebarOrderedReports.tsx | 2 +- src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx | 2 +- src/pages/iou/request/step/IOURequestEditReportCommon.tsx | 2 +- src/pages/iou/request/step/IOURequestStepCategory.tsx | 2 +- src/pages/iou/request/step/IOURequestStepParticipants.tsx | 2 +- src/pages/iou/request/step/IOURequestStepTag.tsx | 2 +- src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index 15689b880ac7..c55c4d89644b 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -1,3 +1,4 @@ +import {activePolicySelector} from '@selectors/Policy'; import {Str} from 'expensify-common'; import React, {useCallback, useEffect, useMemo, useState} from 'react'; import {View} from 'react-native'; @@ -78,7 +79,6 @@ import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; -import {activePolicySelector} from '@src/selectors/Policy'; import type * as OnyxTypes from '@src/types/onyx'; import type {TransactionPendingFieldsKey} from '@src/types/onyx/Transaction'; import MoneyRequestReceiptView from './MoneyRequestReceiptView'; diff --git a/src/hooks/useSearchTypeMenuSections.ts b/src/hooks/useSearchTypeMenuSections.ts index c0220b7c5ba4..4c8785436c55 100644 --- a/src/hooks/useSearchTypeMenuSections.ts +++ b/src/hooks/useSearchTypeMenuSections.ts @@ -1,8 +1,8 @@ +import {createPoliciesSelector} from '@selectors/Policy'; import {useMemo} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {createTypeMenuSections} from '@libs/SearchUIUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -import {createPoliciesSelector} from '@src/selectors/Policy'; import type {Policy, Session} from '@src/types/onyx'; import useCardFeedsForDisplay from './useCardFeedsForDisplay'; import useNetwork from './useNetwork'; diff --git a/src/hooks/useSidebarOrderedReports.tsx b/src/hooks/useSidebarOrderedReports.tsx index 10e486f14404..7a9b19cbfb7d 100644 --- a/src/hooks/useSidebarOrderedReports.tsx +++ b/src/hooks/useSidebarOrderedReports.tsx @@ -1,3 +1,4 @@ +import {createPoliciesSelector} from '@selectors/Policy'; import {deepEqual} from 'fast-equals'; import React, {createContext, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; @@ -6,7 +7,6 @@ import {getPolicyEmployeeListByIdWithoutCurrentUser} from '@libs/PolicyUtils'; import SidebarUtils from '@libs/SidebarUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import {createPoliciesSelector} from '@src/selectors/Policy'; import type * as OnyxTypes from '@src/types/onyx'; import useCurrentReportID from './useCurrentReportID'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; diff --git a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx index d6e6b0652eda..9428913fc5ff 100644 --- a/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx +++ b/src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx @@ -1,4 +1,5 @@ import {useIsFocused} from '@react-navigation/native'; +import {createPoliciesSelector} from '@selectors/Policy'; import {Str} from 'expensify-common'; import type {ImageContentFit} from 'expo-image'; import type {ForwardedRef} from 'react'; @@ -52,7 +53,6 @@ import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; -import {createPoliciesSelector} from '@src/selectors/Policy'; import type * as OnyxTypes from '@src/types/onyx'; import type {QuickActionName} from '@src/types/onyx/QuickAction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; diff --git a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx index bd9f1e74ce7e..b965cea85b7b 100644 --- a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx +++ b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx @@ -1,3 +1,4 @@ +import {createPoliciesSelector} from '@selectors/Policy'; import React, {useMemo} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import * as Expensicons from '@components/Icon/Expensicons'; @@ -18,7 +19,6 @@ import {getOutstandingReportsForUser, getPolicyName, isIOUReport, isOpenReport, import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Route} from '@src/ROUTES'; -import {createPoliciesSelector} from '@src/selectors/Policy'; import type {Policy} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import StepScreenWrapper from './StepScreenWrapper'; diff --git a/src/pages/iou/request/step/IOURequestStepCategory.tsx b/src/pages/iou/request/step/IOURequestStepCategory.tsx index a6d65603349a..b19bf76573e6 100644 --- a/src/pages/iou/request/step/IOURequestStepCategory.tsx +++ b/src/pages/iou/request/step/IOURequestStepCategory.tsx @@ -1,3 +1,4 @@ +import {activePolicySelector} from '@selectors/Policy'; import lodashIsEmpty from 'lodash/isEmpty'; import React, {useEffect} from 'react'; import {ActivityIndicator, InteractionManager, View} from 'react-native'; @@ -28,7 +29,6 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import {activePolicySelector} from '@src/selectors/Policy'; import StepScreenWrapper from './StepScreenWrapper'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; diff --git a/src/pages/iou/request/step/IOURequestStepParticipants.tsx b/src/pages/iou/request/step/IOURequestStepParticipants.tsx index 64a0c1bc7f08..6f9b4cf95f65 100644 --- a/src/pages/iou/request/step/IOURequestStepParticipants.tsx +++ b/src/pages/iou/request/step/IOURequestStepParticipants.tsx @@ -1,4 +1,5 @@ import {useIsFocused} from '@react-navigation/core'; +import {createPoliciesSelector} from '@selectors/Policy'; import React, {useCallback, useEffect, useMemo, useRef} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import FormHelpMessage from '@components/FormHelpMessage'; @@ -36,7 +37,6 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import {createPoliciesSelector} from '@src/selectors/Policy'; import type {Policy} from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; import type Transaction from '@src/types/onyx/Transaction'; diff --git a/src/pages/iou/request/step/IOURequestStepTag.tsx b/src/pages/iou/request/step/IOURequestStepTag.tsx index 733598e2f10b..dc7c33ea40de 100644 --- a/src/pages/iou/request/step/IOURequestStepTag.tsx +++ b/src/pages/iou/request/step/IOURequestStepTag.tsx @@ -1,3 +1,4 @@ +import {activePolicySelector} from '@selectors/Policy'; import React, {useMemo} from 'react'; import {View} from 'react-native'; import Button from '@components/Button'; @@ -22,7 +23,6 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import {activePolicySelector} from '@src/selectors/Policy'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import StepScreenWrapper from './StepScreenWrapper'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; diff --git a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx index 28cea9c574fd..2b10f738b72d 100644 --- a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx +++ b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx @@ -1,3 +1,4 @@ +import {activePolicySelector} from '@selectors/Policy'; import React from 'react'; import type {OnyxEntry} from 'react-native-onyx'; import TaxPicker from '@components/TaxPicker'; @@ -11,7 +12,6 @@ import {setDraftSplitTransaction, setMoneyRequestTaxAmount, setMoneyRequestTaxRa import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; -import {activePolicySelector} from '@src/selectors/Policy'; import type {Policy, Transaction} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import StepScreenWrapper from './StepScreenWrapper'; From e21ec1eb0bbd89dd43e9424cf0c79d2ef5b18477 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Mon, 22 Sep 2025 23:36:06 +0700 Subject: [PATCH 4/6] rename selector --- src/pages/Debug/DebugTagPicker.tsx | 4 +-- src/pages/Search/EmptySearchView.tsx | 26 +++++++++++++------ .../step/IOURequestEditReportCommon.tsx | 4 +-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/pages/Debug/DebugTagPicker.tsx b/src/pages/Debug/DebugTagPicker.tsx index d41e4a666c61..3885d6773627 100644 --- a/src/pages/Debug/DebugTagPicker.tsx +++ b/src/pages/Debug/DebugTagPicker.tsx @@ -25,7 +25,7 @@ type DebugTagPickerProps = { onSubmit: (item: ListItem) => void; }; -const policySelector = (policy: OnyxEntry) => policy?.hasMultipleTagLists; +const policyHasMultipleTagListsSelector = (policy: OnyxEntry) => policy?.hasMultipleTagLists; function DebugTagPicker({policyID, tagName = '', onSubmit}: DebugTagPickerProps) { const styles = useThemeStyles(); @@ -35,7 +35,7 @@ function DebugTagPicker({policyID, tagName = '', onSubmit}: DebugTagPickerProps) const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, {canBeMissing: true}); const policyTagLists = useMemo(() => getTagLists(policyTags), [policyTags]); - const [hasMultipleTagLists] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: true, selector: policySelector}); + const [hasMultipleTagLists] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: true, selector: policyHasMultipleTagListsSelector}); const updateTagName = useCallback( (index: number) => ({text}: ListItem) => { diff --git a/src/pages/Search/EmptySearchView.tsx b/src/pages/Search/EmptySearchView.tsx index d4fc44edf938..36bb3c7d8b9f 100644 --- a/src/pages/Search/EmptySearchView.tsx +++ b/src/pages/Search/EmptySearchView.tsx @@ -1,4 +1,4 @@ -import React, {useMemo, useRef, useState} from 'react'; +import React, {useCallback, useMemo, useRef, useState} from 'react'; // eslint-disable-next-line no-restricted-imports import type {GestureResponderEvent, ImageStyle, Text as RNText, TextStyle, ViewStyle} from 'react-native'; import {Linking, View} from 'react-native'; @@ -88,18 +88,12 @@ const tripsFeatures: FeatureListItem[] = [ }, ]; -const policiesSelector = (policies: OnyxCollection) => - Object.values(policies ?? {}).some((policy) => isPaidGroupPolicy(policy) && isPolicyMember(policy, currentUserPersonalDetails.login)); - function EmptySearchView({similarSearchHash, type, groupBy, hasResults}: EmptySearchViewProps) { const currentUserPersonalDetails = useCurrentUserPersonalDetails(); const {typeMenuSections} = useSearchTypeMenuSections(); const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false}); - const [isUserPaidPolicyMember = false] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { - canBeMissing: true, - selector: policiesSelector, - }); + const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, {canBeMissing: true}); @@ -111,6 +105,22 @@ function EmptySearchView({similarSearchHash, type, groupBy, hasResults}: EmptySe canBeMissing: true, }); + const isUserPaidPolicyMemberSelector = useCallback( + (policies: OnyxCollection) => { + return Object.values(policies ?? {}).some((policy) => isPaidGroupPolicy(policy) && isPolicyMember(policy, currentUserPersonalDetails.login)); + }, + [currentUserPersonalDetails.login], + ); + + const [isUserPaidPolicyMember = false] = useOnyx( + ONYXKEYS.COLLECTION.POLICY, + { + canBeMissing: true, + selector: isUserPaidPolicyMemberSelector, + }, + [isUserPaidPolicyMemberSelector], + ); + return ( void; }; -const policySelector = (policy: OnyxEntry) => policy?.id; +const policyIdSelector = (policy: OnyxEntry) => policy?.id; -const policiesSelector = (policies: OnyxCollection) => createPoliciesSelector(policies, policySelector); +const policiesSelector = (policies: OnyxCollection) => createPoliciesSelector(policies, policyIdSelector); function IOURequestEditReportCommon({ backTo, From 13b10472848edc2a9cbdd335f995137051ca293e Mon Sep 17 00:00:00 2001 From: paulnjs Date: Wed, 24 Sep 2025 02:04:25 +0700 Subject: [PATCH 5/6] fix the changefile error --- src/hooks/useSidebarOrderedReports.tsx | 2 +- src/hooks/useWorkspaceAccountID.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSidebarOrderedReports.tsx b/src/hooks/useSidebarOrderedReports.tsx index cdb3cf4f4669..6ca5c3b1e362 100644 --- a/src/hooks/useSidebarOrderedReports.tsx +++ b/src/hooks/useSidebarOrderedReports.tsx @@ -1,5 +1,5 @@ -import {createPoliciesSelector} from '@selectors/Policy'; import reportsSelector from '@selectors/Attributes'; +import {createPoliciesSelector} from '@selectors/Policy'; import {deepEqual} from 'fast-equals'; import React, {createContext, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; diff --git a/src/hooks/useWorkspaceAccountID.ts b/src/hooks/useWorkspaceAccountID.ts index 69fdad5f8c3b..361c80603cfd 100644 --- a/src/hooks/useWorkspaceAccountID.ts +++ b/src/hooks/useWorkspaceAccountID.ts @@ -7,7 +7,7 @@ import useOnyx from './useOnyx'; const policyWorkspaceAccountIDSelector = (policy: OnyxEntry) => policy?.workspaceAccountID; function useWorkspaceAccountID(policyID: string | undefined) { - const [workspaceAccountID = CONST.DEFAULT_NUMBER_ID] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {selector: policyWorkspaceAccountIDSelector}); + const [workspaceAccountID = CONST.DEFAULT_NUMBER_ID] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {selector: policyWorkspaceAccountIDSelector, canBeMissing: true}); return workspaceAccountID; } From cd5ab5a72e3f5a6ca91f06b304163bf38c975b61 Mon Sep 17 00:00:00 2001 From: paulnjs Date: Wed, 24 Sep 2025 09:12:46 +0700 Subject: [PATCH 6/6] fix import error --- src/pages/settings/Preferences/PriorityModePage.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/settings/Preferences/PriorityModePage.tsx b/src/pages/settings/Preferences/PriorityModePage.tsx index 3ff2e2bc316d..714a1fa46308 100644 --- a/src/pages/settings/Preferences/PriorityModePage.tsx +++ b/src/pages/settings/Preferences/PriorityModePage.tsx @@ -8,8 +8,8 @@ import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; import useThemeStyles from '@hooks/useThemeStyles'; +import {updateChatPriorityMode} from '@libs/actions/User'; import Navigation from '@libs/Navigation/Navigation'; -import * as User from '@userActions/User'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -39,7 +39,7 @@ function PriorityModePage() { Navigation.goBack(); return; } - User.updateChatPriorityMode(mode.value); + updateChatPriorityMode(mode.value); }, [priorityMode], );