11import React , { useEffect } from 'react' ;
2- import { useSelector } from 'react-redux' ;
2+ import { useDispatch , useSelector } from 'react-redux' ;
33import { useLocation } from 'react-router-dom' ;
44import { CaipChainId } from '@metamask/utils' ;
55import { InternalAccount } from '@metamask/keyring-internal-api' ;
6- import { Box } from '../../components/component-library' ;
6+ import {
7+ Box ,
8+ Modal ,
9+ ModalOverlay ,
10+ ModalContent ,
11+ } from '../../components/component-library' ;
712import {
813 BlockSize ,
914 Display ,
1015 FlexDirection ,
1116 JustifyContent ,
1217} from '../../helpers/constants/design-system' ;
18+ import { setSelectedAccount } from '../../store/actions' ;
1319import { useMultichainBalances } from '../../hooks/useMultichainBalances' ;
1420import { NonEvmQueryParams } from '../../../shared/lib/deep-links/routes/nonevm' ;
1521import { SWAP_ROUTE } from '../../../shared/lib/deep-links/routes/route' ;
1622import { BridgeQueryParams } from '../../../shared/lib/deep-links/routes/swap' ;
1723import { RampsMetaMaskEntry } from '../../hooks/ramps/useRamps/useRamps' ;
24+ import { getLastSelectedNonEvmAccount } from '../../selectors/multichain' ;
1825import {
1926 getDataCollectionForMarketing ,
2027 getMetaMaskAccountsOrdered ,
@@ -60,10 +67,12 @@ const getBuyUrl = (
6067
6168export const NonEvmBalanceCheck = ( ) => {
6269 const location = useLocation ( ) ;
70+ const dispatch = useDispatch ( ) ;
6371 const metaMetricsId = useSelector ( getMetaMetricsId ) ;
6472 const isMetaMetricsEnabled = useSelector ( getParticipateInMetaMetrics ) ;
6573 const isMarketingEnabled = useSelector ( getDataCollectionForMarketing ) ;
6674 const accounts = useSelector ( getMetaMaskAccountsOrdered ) ;
75+ const lastSelectedNonEvmAccount = useSelector ( getLastSelectedNonEvmAccount ) ;
6776
6877 const params = new URLSearchParams ( location . search ) ;
6978 const chainId = params . get ( NonEvmQueryParams . ChainId ) as CaipChainId ;
@@ -80,6 +89,11 @@ export const NonEvmBalanceCheck = () => {
8089 }
8190
8291 if ( hasAccountForChain ) {
92+ // If we have a "last selected" non-EVM account that matches the chain -> switch to it
93+ if ( lastSelectedNonEvmAccount ?. scopes ?. includes ( chainId ) ) {
94+ dispatch ( setSelectedAccount ( lastSelectedNonEvmAccount . address ) ) ;
95+ }
96+
8397 const hasPositiveBalance = assetsWithBalance . some (
8498 ( asset ) =>
8599 asset . chainId === chainId && asset . balance && asset . balance !== '0' ,
@@ -106,15 +120,31 @@ export const NonEvmBalanceCheck = () => {
106120
107121 if ( ! hasAccountForChain ) {
108122 return (
109- < Box
110- display = { Display . Flex }
111- flexDirection = { FlexDirection . Column }
112- justifyContent = { JustifyContent . spaceBetween }
113- width = { BlockSize . OneFifth }
114- padding = { 4 }
123+ < Modal
124+ isOpen
125+ onClose = { ( ) => {
126+ // do nothing
127+ } }
115128 >
116- < AddNonEvmAccountModal chainId = { chainId } />
117- </ Box >
129+ < ModalOverlay />
130+ < ModalContent
131+ modalDialogProps = { {
132+ padding : 0 ,
133+ display : Display . Flex ,
134+ flexDirection : FlexDirection . Column ,
135+ } }
136+ >
137+ < Box
138+ display = { Display . Flex }
139+ flexDirection = { FlexDirection . Column }
140+ justifyContent = { JustifyContent . spaceBetween }
141+ width = { BlockSize . Full }
142+ padding = { 2 }
143+ >
144+ < AddNonEvmAccountModal chainId = { chainId } />
145+ </ Box >
146+ </ ModalContent >
147+ </ Modal >
118148 ) ;
119149 }
120150
0 commit comments