diff --git a/ui/pages/bridge/hooks/useDestinationAccount.ts b/ui/pages/bridge/hooks/useDestinationAccount.ts index 2b2b9c45d8b7..19072f889c4c 100644 --- a/ui/pages/bridge/hooks/useDestinationAccount.ts +++ b/ui/pages/bridge/hooks/useDestinationAccount.ts @@ -1,10 +1,6 @@ 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, getToChain, @@ -24,7 +20,6 @@ export const useDestinationAccount = () => { const [isDestinationAccountPickerOpen, setIsDestinationAccountPickerOpen] = useState(false); const toChain = useSelector(getToChain); - const previousChainIdRef = useRef(undefined); // For bridges, use the appropriate account type for the destination chain const defaultInternalDestinationAccount = useSelector((state) => @@ -44,49 +39,20 @@ 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)); - - // 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 - // 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 - - if (shouldOpenPicker) { + if (defaultInternalDestinationAccount) { + setSelectedDestinationAccount({ + ...defaultInternalDestinationAccount, + isExternal: false, + displayName: displayName ?? '', + }); + setIsDestinationAccountPickerOpen(false); + } else { + // 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 setSelectedDestinationAccount(null); setIsDestinationAccountPickerOpen(true); - } else { - setSelectedDestinationAccount( - defaultInternalDestinationAccount - ? { - ...defaultInternalDestinationAccount, - isExternal: false, - displayName: displayName ?? '', - } - : null, - ); } - - // Update the ref for next comparison - previousChainIdRef.current = currentChainId; - }, [defaultInternalDestinationAccount, displayName, toChain?.chainId]); + }, [defaultInternalDestinationAccount, displayName]); return { selectedDestinationAccount,