Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 8 additions & 33 deletions ui/pages/bridge/hooks/useDestinationAccount.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { useSelector } from 'react-redux';
import { useEffect, useState, useRef } from 'react';
import {
formatChainIdToCaip,
isSolanaChainId,
isBitcoinChainId,
} from '@metamask/bridge-controller';
import { useEffect, useState } from 'react';
import { formatChainIdToCaip } from '@metamask/bridge-controller';
import {
getAccountGroupNameByInternalAccount,
getIsToOrFromNonEvm,
getToChain,
} from '../../../ducks/bridge/selectors';
import { getInternalAccountBySelectedAccountGroupAndCaip } from '../../../selectors/multichain-accounts/account-tree';
Expand All @@ -24,7 +21,6 @@ export const useDestinationAccount = () => {
const [isDestinationAccountPickerOpen, setIsDestinationAccountPickerOpen] =
useState(false);
const toChain = useSelector(getToChain);
const previousChainIdRef = useRef<string | undefined>(undefined);

// For bridges, use the appropriate account type for the destination chain
const defaultInternalDestinationAccount = useSelector((state) =>
Expand All @@ -43,31 +39,13 @@ export const useDestinationAccount = () => {
),
);

useEffect(() => {
const currentChainId = toChain?.chainId;
const previousChainId = previousChainIdRef.current;

// Check if current and previous chains are non-EVM
const isPreviousNonEvm =
previousChainId &&
(isSolanaChainId(previousChainId) || isBitcoinChainId(previousChainId));
const isCurrentNonEvm =
currentChainId &&
(isSolanaChainId(currentChainId) || isBitcoinChainId(currentChainId));
const isToOrFromNonEvm = useSelector(getIsToOrFromNonEvm);

// Check if previous chain was EVM (not non-EVM and exists)
const wasPreviousEvm = previousChainId && !isPreviousNonEvm;
// Check if current chain is EVM (not non-EVM and exists)
const isCurrentEvm = currentChainId && !isCurrentNonEvm;

// Open account picker when bridging between non-EVM and EVM chains
useEffect(() => {
// Open account picker when bridging between non-EVM and EVM chains and there is no matching account (edge case)
// Cases: non-EVM -> EVM, EVM -> non-EVM, or switching between different non-EVM chains
const shouldOpenPicker =
(isPreviousNonEvm && isCurrentEvm) || // non-EVM -> EVM
(wasPreviousEvm && isCurrentNonEvm) || // EVM -> non-EVM
(isPreviousNonEvm &&
isCurrentNonEvm &&
previousChainId !== currentChainId); // different non-EVM chains
!defaultInternalDestinationAccount && isToOrFromNonEvm;

if (shouldOpenPicker) {
setSelectedDestinationAccount(null);
Expand All @@ -83,10 +61,7 @@ export const useDestinationAccount = () => {
: null,
);
}

// Update the ref for next comparison
previousChainIdRef.current = currentChainId;
}, [defaultInternalDestinationAccount, displayName, toChain?.chainId]);
}, [defaultInternalDestinationAccount, isToOrFromNonEvm, displayName]);

return {
selectedDestinationAccount,
Expand Down
Loading