Skip to content

feat(solana): update add account from network picker #31358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions ui/components/multichain/create-account/create-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const CreateAccount: CreateAccountComponent = React.memo(
getMetaMaskAccountsOrdered,
);

const [loading, setLoading] = useState(false);
const [defaultAccountName, setDefaultAccountName] = useState('');
// We are not using `accounts` as a dependency here to avoid having the input
// updating when the new account will be created.
Expand Down Expand Up @@ -137,8 +138,8 @@ export const CreateAccount: CreateAccountComponent = React.memo(

const onSubmit = useCallback(
async (event: KeyboardEvent<HTMLFormElement>) => {
setLoading(true);
event.preventDefault();

try {
await onCreateAccount(trimmedAccountName || defaultAccountName);
trackEvent({
Expand Down Expand Up @@ -216,7 +217,8 @@ export const CreateAccount: CreateAccountComponent = React.memo(
<ButtonPrimary
data-testid="submit-add-account-with-name"
type="submit"
disabled={!isValidAccountName}
disabled={!isValidAccountName || loading}
loading={loading}
block
>
{t('addAccount')}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react';
import React, { useCallback } from 'react';
import { CaipChainId } from '@metamask/utils';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

import { useAccountCreationOnNetworkChange } from '../../../../hooks/accounts/useAccountCreationOnNetworkChange';
import { toggleNetworkMenu } from '../../../../store/actions';
import { MULTICHAIN_NETWORK_TO_NICKNAME } from '../../../../../shared/constants/multichain/networks';
import {
MULTICHAIN_NETWORK_TO_NICKNAME,
MultichainNetworks,
} from '../../../../../shared/constants/multichain/networks';
import {
Box,
Text,
Expand All @@ -20,19 +22,111 @@ import {
TextAlign,
} from '../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import { getMetaMaskHdKeyrings } from '../../../../selectors';
import { CreateSnapAccount } from '../../create-snap-account';
import { SrpList } from '../../multi-srp/srp-list';
import { WalletClientType } from '../../../../hooks/accounts/useMultichainWalletSnapClient';

const SNAP_CLIENT_CONFIG_MAP: Record<
string,
{ clientType: WalletClientType | null; chainId: CaipChainId | null }
> = {
[MultichainNetworks.BITCOIN]: {
clientType: WalletClientType.Bitcoin,
chainId: MultichainNetworks.BITCOIN,
},
[MultichainNetworks.BITCOIN_TESTNET]: {
clientType: WalletClientType.Bitcoin,
chainId: MultichainNetworks.BITCOIN_TESTNET,
},
[MultichainNetworks.SOLANA]: {
clientType: WalletClientType.Solana,
chainId: MultichainNetworks.SOLANA,
},
};

const AddNonEvmAccountModal = ({ chainId }: { chainId: CaipChainId }) => {
const t = useI18nContext();
const dispatch = useDispatch();
const { createAccount } = useAccountCreationOnNetworkChange();
const [primaryKeyring] = useSelector(getMetaMaskHdKeyrings);
const [showSrpSelection, setShowSrpSelection] = React.useState(false);
const [showCreateAccount, setShowCreateAccount] = React.useState(false);
const [selectedKeyringId, setSelectedKeyringId] = React.useState(
primaryKeyring.metadata.id,
);

const handleActionComplete = useCallback(
async (confirmed: boolean) => {
if (confirmed) {
dispatch(toggleNetworkMenu());
} else {
setShowCreateAccount(false);
}
},
[dispatch],
);

const handleSelectSrp = useCallback(() => {
setShowSrpSelection(true);
setShowCreateAccount(false);
}, []);

const handleSrpActionComplete = useCallback((keyringId: string) => {
setSelectedKeyringId(keyringId);
setShowCreateAccount(true);
}, []);

const handleAddAccount = useCallback(() => {
setShowCreateAccount(true);
}, []);

const { clientType, chainId: mappedChainId } = SNAP_CLIENT_CONFIG_MAP[
chainId
] || {
clientType: null,
chainId: null,
};

if (showCreateAccount && clientType && mappedChainId) {
return (
<Box
className="add-non-evm-account-modal"
display={Display.Flex}
flexDirection={FlexDirection.Column}
justifyContent={JustifyContent.spaceBetween}
>
<Box paddingLeft={4} paddingRight={4} paddingBottom={4}>
<CreateSnapAccount
onActionComplete={handleActionComplete}
selectedKeyringId={selectedKeyringId}
onSelectSrp={handleSelectSrp}
clientType={clientType}
chainId={mappedChainId}
/>
</Box>
</Box>
);
}

if (showSrpSelection) {
return (
<Box
className="add-non-evm-account-modal"
display={Display.Flex}
flexDirection={FlexDirection.Column}
justifyContent={JustifyContent.spaceBetween}
>
<SrpList onActionComplete={handleSrpActionComplete} />
</Box>
);
}

return (
<Box
className="add-non-evm-account-modal"
display={Display.Flex}
flexDirection={FlexDirection.Column}
justifyContent={JustifyContent.spaceBetween}
height={BlockSize.Screen}
>
<Box paddingLeft={4} paddingRight={4}>
<Text textAlign={TextAlign.Left} variant={TextVariant.bodyMd}>
Expand All @@ -49,14 +143,7 @@ const AddNonEvmAccountModal = ({ chainId }: { chainId: CaipChainId }) => {
<ButtonPrimary
width={BlockSize.Full}
size={ButtonPrimarySize.Lg}
onClick={async () => {
// This modal is being part of the `NetworkListMenu`. So
// we need to explicitly close it before triggering
// the account creation.
// See: `NetworkMenuList.ACTION_MODE.ADD_NON_EVM_ACCOUNT`.
dispatch(toggleNetworkMenu());
await createAccount(chainId);
}}
onClick={handleAddAccount}
>
{t('addAccount')}
</ButtonPrimary>
Expand Down
2 changes: 0 additions & 2 deletions ui/components/multichain/network-list-menu/index.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
.multichain-network-list-menu-content-wrapper {
&__dialog {
height: 100vh;
max-height: 100%;
// Needed to ensure the drag and drop works. All other modals use a slide up animation with transform: translateY(0)
// https://github.com/MetaMask/metamask-extension/issues/30436
transform: unset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ import {
TextAlign,
TextColor,
TextVariant,
BlockSize,
} from '../../../helpers/constants/design-system';
import {
Box,
Expand Down Expand Up @@ -792,11 +791,6 @@ export const NetworkListMenu = ({ onClose }: { onClose: () => void }) => {
paddingTop: 0,
paddingBottom: 0,
}}
height={
actionMode === ACTION_MODE.ADD_NON_EVM_ACCOUNT
? BlockSize.TwoFifths
: BlockSize.Screen
}
>
<ModalHeader
paddingTop={4}
Expand Down
Loading