Skip to content

Commit d215889

Browse files
refactor: remove useRef usages
1 parent 91b6882 commit d215889

1 file changed

Lines changed: 24 additions & 49 deletions

File tree

app/components/UI/TokenDetails/hooks/useTokenAtomicActions.ts

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef, useState } from 'react';
1+
import { useCallback, useEffect, useState } from 'react';
22
import { useNavigation } from '@react-navigation/native';
33
import { useStore } from 'react-redux';
44
import { Hex, CaipChainId, isCaipAssetType } from '@metamask/utils';
@@ -272,12 +272,8 @@ export const useHandleOnBuy = ({ token }: { token: TokenActionInput }) => {
272272
const rampUnifiedV1Enabled = useRampsUnifiedV1Enabled();
273273
const isAuthenticated = useIsRampAuthenticated();
274274

275-
const tokenRef = useRef(token);
276-
tokenRef.current = token;
277-
278275
return useCallback(() => {
279-
const currentToken = tokenRef.current;
280-
const tokenChainIdHex = currentToken.chainId as Hex;
276+
const tokenChainIdHex = token.chainId as Hex;
281277

282278
trackActionButtonClick(trackEvent, createEventBuilder, {
283279
action_name: ActionButtonType.BUY,
@@ -288,12 +284,12 @@ export const useHandleOnBuy = ({ token }: { token: TokenActionInput }) => {
288284

289285
let assetId: string | undefined;
290286
try {
291-
if (isCaipAssetType(currentToken.address)) {
292-
assetId = currentToken.address;
287+
if (isCaipAssetType(token.address)) {
288+
assetId = token.address;
293289
} else {
294290
assetId = parseRampIntent({
295291
chainId: getDecimalChainId(tokenChainIdHex),
296-
address: currentToken.address,
292+
address: token.address,
297293
})?.assetId;
298294
}
299295
} catch {
@@ -330,14 +326,15 @@ export const useHandleOnBuy = ({ token }: { token: TokenActionInput }) => {
330326
is_authenticated: isAuthenticated,
331327
preferred_provider: preferredProvider,
332328
order_count: orders.length + controllerOrders.length,
333-
asset_symbol: currentToken.symbol,
329+
asset_symbol: token.symbol,
334330
})
335331
.build(),
336332
);
337333

338334
goToBuy({ assetId }, { buyFlowOrigin: 'tokenInfo' });
339335
}, [
340336
store,
337+
token,
341338
trackEvent,
342339
createEventBuilder,
343340
rampUnifiedV1Enabled,
@@ -367,30 +364,18 @@ export const useHandleOnSwap = ({
367364
// location from the session that opened the bridge (e.g. "Main View").
368365
const isFromBridgeAssetPicker = token.source === TokenDetailsSource.Swap;
369366

370-
// Source and dest tokens are passed at click time so `goToSwaps` doesn't
371-
// recreate when upstream rebuilds the token reference. `useSwapBridgeNavigation`
372-
// no longer carries the heavy `useInitialBridgeTokens` subtree at render
373-
// time — the popular-tokens prefetch is now wired via the lightweight
374-
// `useFetchPopularTokens` and fires lazily on click.
375367
const { goToSwaps } = useSwapBridgeNavigation({
376368
location: SwapBridgeNavigationLocation.TokenView,
377369
sourcePage,
378370
transactionActiveAbTests: token.transactionActiveAbTests,
379371
skipLocationUpdate: isFromBridgeAssetPicker,
380372
});
381373

382-
const tokenRef = useRef(token);
383-
tokenRef.current = token;
384-
const currentTokenBalanceRef = useRef(currentTokenBalance);
385-
currentTokenBalanceRef.current = currentTokenBalance;
386-
387374
return useCallback(() => {
388375
if (!goToSwaps) return;
389376

390-
const currentToken = tokenRef.current;
391-
const currentTokenAsBridgeToken = toCurrentTokenAsBridgeToken(currentToken);
392-
const balanceForCheck =
393-
currentTokenBalanceRef.current ?? currentToken.balance;
377+
const currentTokenAsBridgeToken = toCurrentTokenAsBridgeToken(token);
378+
const balanceForCheck = currentTokenBalance ?? token.balance;
394379

395380
if (hasPositiveBalance(balanceForCheck)) {
396381
goToSwaps(currentTokenAsBridgeToken, undefined, undefined, true);
@@ -401,8 +386,8 @@ export const useHandleOnSwap = ({
401386
const userAssetsMap = selectAssetsBySelectedAccountGroup(store.getState());
402387
const buySourceToken = computeBuySourceToken(
403388
userAssetsMap,
404-
currentToken.chainId,
405-
currentToken.address,
389+
token.chainId,
390+
token.address,
406391
);
407392

408393
if (buySourceToken) {
@@ -411,7 +396,7 @@ export const useHandleOnSwap = ({
411396
}
412397

413398
goToSwaps(currentTokenAsBridgeToken, undefined, undefined, true);
414-
}, [goToSwaps, store]);
399+
}, [goToSwaps, store, token, currentTokenBalance]);
415400
};
416401

417402
/**
@@ -430,12 +415,7 @@ export const useHandleOnSend = ({ token }: { token: TokenActionInput }) => {
430415
const { sendNonEvmAsset } = useSendNonEvmAsset({ asset: token });
431416
///: END:ONLY_INCLUDE_IF
432417

433-
const tokenRef = useRef(token);
434-
tokenRef.current = token;
435-
436418
return useCallback(async () => {
437-
const currentToken = tokenRef.current;
438-
439419
trackEvent(
440420
createEventBuilder(MetaMetricsEvents.ACTION_BUTTON_CLICKED)
441421
.addProperties({
@@ -463,11 +443,11 @@ export const useHandleOnSend = ({ token }: { token: TokenActionInput }) => {
463443

464444
const selectedChainId = selectEvmChainId(store.getState());
465445

466-
if (currentToken.chainId !== selectedChainId) {
446+
if (token.chainId !== selectedChainId) {
467447
const { NetworkController, MultichainNetworkController } = Engine.context;
468448
const networkConfiguration =
469449
NetworkController.getNetworkConfigurationByChainId(
470-
currentToken.chainId as Hex,
450+
token.chainId as Hex,
471451
);
472452

473453
const networkClientId =
@@ -482,7 +462,7 @@ export const useHandleOnSend = ({ token }: { token: TokenActionInput }) => {
482462

483463
navigateToSendPage({
484464
location: InitSendLocation.AssetOverview,
485-
asset: currentToken,
465+
asset: token,
486466
});
487467
}, [
488468
trackEvent,
@@ -491,6 +471,7 @@ export const useHandleOnSend = ({ token }: { token: TokenActionInput }) => {
491471
navigation,
492472
store,
493473
navigateToSendPage,
474+
token,
494475
]);
495476
};
496477

@@ -509,14 +490,8 @@ export const useHandleOnReceive = ({
509490
const store = useStore<RootState>();
510491
const { trackEvent, createEventBuilder } = useAnalytics();
511492

512-
const tokenRef = useRef(token);
513-
tokenRef.current = token;
514-
const networkNameRef = useRef(networkName);
515-
networkNameRef.current = networkName;
516-
517493
return useCallback(() => {
518-
const currentToken = tokenRef.current;
519-
const chainId = currentToken.chainId as Hex;
494+
const chainId = token.chainId as Hex;
520495

521496
trackActionButtonClick(trackEvent, createEventBuilder, {
522497
action_name: ActionButtonType.RECEIVE,
@@ -530,9 +505,9 @@ export const useHandleOnReceive = ({
530505
const selectedAccountGroup = selectSelectedAccountGroup(state);
531506
const getAccountByScope = selectSelectedInternalAccountByScope(state);
532507

533-
const accountForChain = currentToken.chainId
508+
const accountForChain = token.chainId
534509
? (getAccountByScope(
535-
formatChainIdToCaip(currentToken.chainId as Hex) as CaipChainId,
510+
formatChainIdToCaip(token.chainId as Hex) as CaipChainId,
536511
) ?? selectedInternalAccount)
537512
: selectedInternalAccount;
538513

@@ -543,14 +518,14 @@ export const useHandleOnReceive = ({
543518
screen: Routes.SHEET.MULTICHAIN_ACCOUNT_DETAILS.SHARE_ADDRESS_QR,
544519
params: {
545520
address: addressForChain,
546-
networkName: networkNameRef.current || 'Unknown Network',
521+
networkName: networkName || 'Unknown Network',
547522
chainId,
548523
groupId: selectedAccountGroup.id,
549524
},
550525
});
551526
} else {
552-
const resultChainId = formatChainIdToCaip(currentToken.chainId as Hex);
553-
const isNonEvmToken = resultChainId === currentToken.chainId;
527+
const resultChainId = formatChainIdToCaip(token.chainId as Hex);
528+
const isNonEvmToken = resultChainId === token.chainId;
554529

555530
Logger.error(
556531
new Error('useHandleOnReceive - Missing required data for navigation'),
@@ -559,9 +534,9 @@ export const useHandleOnReceive = ({
559534
hasAccountGroup: !!selectedAccountGroup,
560535
hasChainId: !!chainId,
561536
isNonEvmAsset: isNonEvmToken,
562-
assetChainId: currentToken.chainId,
537+
assetChainId: token.chainId,
563538
},
564539
);
565540
}
566-
}, [trackEvent, createEventBuilder, store, navigation]);
541+
}, [trackEvent, createEventBuilder, store, navigation, token, networkName]);
567542
};

0 commit comments

Comments
 (0)