-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace wallet selector modal
- Loading branch information
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.ui.modal.dcl.login-modal { | ||
width: 90%; | ||
max-width: 520px; | ||
} | ||
|
||
.ui.modal.dcl.login-modal .content { | ||
margin: 28px 32px; | ||
} | ||
|
||
.ui.modal.dcl.login-modal .dcl.option { | ||
width: 100%; | ||
max-width: 360px; | ||
} | ||
|
||
.ui.modal.dcl.login-modal small p.dg.Paragraph { | ||
font-size: 13px; | ||
line-height: 1.5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React, { useCallback, useEffect, useState } from 'react' | ||
|
||
import { ChainId } from '@dcl/schemas/dist/dapps/chain-id' | ||
import { ProviderType } from '@dcl/schemas/dist/dapps/provider-type' | ||
import { connection } from 'decentraland-connect/dist/ConnectionManager' | ||
import { toModalOptionType } from 'decentraland-dapps/dist/containers/LoginModal/utils' | ||
import Markdown from 'decentraland-gatsby/dist/components/Text/Markdown' | ||
import { useFeatureFlagContext } from 'decentraland-gatsby/dist/context/FeatureFlag' | ||
import useAnchor from 'decentraland-gatsby/dist/hooks/useAnchor' | ||
import useFormatMessage from 'decentraland-gatsby/dist/hooks/useFormatMessage' | ||
import { Loader } from 'decentraland-ui/dist/components/Loader/Loader' | ||
import { LoginModal, LoginModalOptionType } from 'decentraland-ui/dist/components/LoginModal/LoginModal' | ||
import 'decentraland-ui/dist/components/LoginModal/LoginModal.css' | ||
import { Modal } from 'decentraland-ui/dist/components/Modal/Modal' | ||
import { ModalNavigation } from 'decentraland-ui/dist/components/ModalNavigation/ModalNavigation' | ||
import ModalContent from 'semantic-ui-react/dist/commonjs/modules/Modal/ModalContent' | ||
|
||
import './WalletSelectorModal.css' | ||
|
||
type Props = { | ||
open: boolean | ||
loading: boolean | ||
chainId: ChainId | ||
error: string | null | ||
onConnect: (providerType: ProviderType, chainId: ChainId) => void | ||
onClose: () => void | ||
} | ||
|
||
const availableProviders = new Set(connection.getAvailableProviders()) | ||
|
||
export default function WalletSelectorModal({ chainId, onConnect, onClose, error, open, loading }: Props) { | ||
const t = useFormatMessage() | ||
const [provider, setProvider] = useState(LoginModalOptionType.METAMASK) | ||
const [ff] = useFeatureFlagContext() | ||
|
||
useEffect(() => { | ||
setProvider(toModalOptionType(ProviderType.INJECTED) || LoginModalOptionType.METAMASK) | ||
}, []) | ||
|
||
const handleConnect = useCallback( | ||
(providerType: ProviderType, chainId: ChainId) => { | ||
if (onConnect) { | ||
onConnect(providerType, chainId) | ||
} | ||
}, | ||
[onConnect] | ||
) | ||
|
||
const handleDownloadMetamaskClick = useAnchor('https://metamask.io/download.html') | ||
const handleConnectInjected = useCallback(() => { | ||
if (availableProviders.has(ProviderType.INJECTED)) { | ||
handleConnect(ProviderType.INJECTED, chainId) | ||
} else { | ||
handleDownloadMetamaskClick() | ||
} | ||
}, [handleConnect, handleDownloadMetamaskClick, chainId]) | ||
const handleConnectFortmatic = useCallback( | ||
() => handleConnect(ProviderType.FORTMATIC, chainId), | ||
[chainId, handleConnect] | ||
) | ||
const handleConnectWalletConnect = useCallback( | ||
() => | ||
handleConnect( | ||
ff.enabled('dapps-wallet-connect-v2') ? ProviderType.WALLET_CONNECT_V2 : ProviderType.WALLET_CONNECT, | ||
chainId | ||
), | ||
[ff, chainId, handleConnect] | ||
) | ||
const handleConnectWalletLink = useCallback( | ||
() => handleConnect(ProviderType.WALLET_LINK, chainId), | ||
[chainId, handleConnect] | ||
) | ||
|
||
return ( | ||
<Modal open={open} className="dcl login-modal"> | ||
<ModalNavigation | ||
title={t('modal.wallet_selector.title')} | ||
subtitle={t('modal.wallet_selector.subtitle')} | ||
onClose={onClose} | ||
/> | ||
<ModalContent> | ||
<LoginModal.Option type={provider} onClick={handleConnectInjected} /> | ||
{availableProviders.has(ProviderType.FORTMATIC) && ( | ||
<LoginModal.Option type={LoginModalOptionType.FORTMATIC} onClick={handleConnectFortmatic} /> | ||
)} | ||
{availableProviders.has(ProviderType.WALLET_CONNECT) && ( | ||
<LoginModal.Option type={LoginModalOptionType.WALLET_CONNECT} onClick={handleConnectWalletConnect} /> | ||
)} | ||
{availableProviders.has(ProviderType.WALLET_LINK) && ( | ||
<LoginModal.Option type={LoginModalOptionType.WALLET_LINK} onClick={handleConnectWalletLink} /> | ||
)} | ||
<small className="message"> | ||
<Markdown>{t('modal.wallet_selector.trezor')}</Markdown> | ||
</small> | ||
</ModalContent> | ||
{error && <p className="error visible">{error}</p>} | ||
{loading ? ( | ||
<> | ||
<Loader size="big" active /> | ||
<div className="loader-background"></div> | ||
</> | ||
) : null} | ||
</Modal> | ||
) | ||
} |