11import { useFocusEffect } from '@react-navigation/native' ;
22import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
33import { View } from 'react-native' ;
4+ import type { OnyxCollection } from 'react-native-onyx' ;
45import type { ValueOf } from 'type-fest' ;
56import AddToWalletButton from '@components/AddToWalletButton/index' ;
67import Button from '@components/Button' ;
@@ -46,6 +47,7 @@ import type {TranslationPaths} from '@src/languages/types';
4647import ONYXKEYS from '@src/ONYXKEYS' ;
4748import ROUTES from '@src/ROUTES' ;
4849import SCREENS from '@src/SCREENS' ;
50+ import type { Policy } from '@src/types/onyx' ;
4951import type { SelectedTimezone } from '@src/types/onyx/PersonalDetails' ;
5052import { useExpensifyCardActions , useExpensifyCardState } from './ExpensifyCardContextProvider' ;
5153import FrozenCardIndicator from './FrozenCardIndicator' ;
@@ -93,7 +95,6 @@ const getCardHintText = (validFrom: string | undefined, validThru: string | unde
9395function ExpensifyCardPage ( { route} : ExpensifyCardPageProps ) {
9496 const { cardID} = route . params ;
9597 const [ account ] = useOnyx ( ONYXKEYS . ACCOUNT ) ;
96- const [ policies ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY ) ;
9798 const cardList = useNonPersonalCardList ( ) ;
9899 const [ privatePersonalDetails ] = useOnyx ( ONYXKEYS . PRIVATE_PERSONAL_DETAILS ) ;
99100 const { currencyList} = useCurrencyListState ( ) ;
@@ -167,16 +168,18 @@ function ExpensifyCardPage({route}: ExpensifyCardPageProps) {
167168 const canManageCardFreeze = isBetaEnabled ( CONST . BETAS . FREEZE_CARD ) && isCardHolder && ! ! currentCard && ! isAccountLocked ;
168169 const canUnfreezeCard = canManageCardFreeze && frozenByAccountID === session ?. accountID ;
169170
170- const policyIDForCurrentCard = useMemo ( ( ) => {
171- const workspaceAccountID = Number ( currentCard ?. fundID ) ;
172- if ( ! workspaceAccountID || Number . isNaN ( workspaceAccountID ) ) {
173- return undefined ;
174- }
175-
176- return Object . values ( policies ?? { } ) . find ( ( policy ) => policy ?. workspaceAccountID === workspaceAccountID ) ?. id ;
177- } , [ currentCard ?. fundID , policies ] ) ;
171+ const policyIDSelector = useCallback (
172+ ( allPolicies : OnyxCollection < Policy > ) : string | undefined => {
173+ const workspaceAccountID = Number ( currentCard ?. fundID ) ;
174+ if ( ! workspaceAccountID || Number . isNaN ( workspaceAccountID ) ) {
175+ return undefined ;
176+ }
178177
179- const cardHolderWorkspaceChatReportID = useMemo ( ( ) => getPolicyExpenseChat ( currentCard ?. accountID , policyIDForCurrentCard ) ?. reportID , [ currentCard ?. accountID , policyIDForCurrentCard ] ) ;
178+ return Object . values ( allPolicies ?? { } ) . find ( ( policy ) => policy ?. workspaceAccountID === workspaceAccountID ) ?. id ;
179+ } ,
180+ [ currentCard ?. fundID ] ,
181+ ) ;
182+ const [ policyIDForCurrentCard ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY , { selector : policyIDSelector } , [ policyIDSelector ] ) ;
180183
181184 const [ isFreezeModalVisible , setIsFreezeModalVisible ] = useState ( false ) ;
182185 const [ isUnfreezeModalVisible , setIsUnfreezeModalVisible ] = useState ( false ) ;
@@ -202,11 +205,12 @@ function ExpensifyCardPage({route}: ExpensifyCardPageProps) {
202205 } , [ ] ) ;
203206
204207 const handleAskToUnfreezePress = useCallback ( ( ) => {
208+ const cardHolderWorkspaceChatReportID = getPolicyExpenseChat ( currentCard ?. accountID , policyIDForCurrentCard ) ?. reportID ;
205209 if ( ! cardHolderWorkspaceChatReportID ) {
206210 return ;
207211 }
208212 Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( cardHolderWorkspaceChatReportID ) ) ;
209- } , [ cardHolderWorkspaceChatReportID ] ) ;
213+ } , [ currentCard ?. accountID , policyIDForCurrentCard ] ) ;
210214
211215 const handleDismissUnfreezeModal = useCallback ( ( ) => {
212216 setIsUnfreezeModalVisible ( false ) ;
0 commit comments