Skip to content
Merged
Changes from all commits
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
60 changes: 13 additions & 47 deletions ui/pages/bridge/hooks/useDestinationAccount.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,7 +20,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 @@ -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,
Expand Down
Loading