Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CheckIcon from '@mui/icons-material/Check'
import ErrorIcon from '@mui/icons-material/Error'
import { red, green } from '@mui/material/colors'
import classNames from 'classnames'
import { useDebounce, useAsyncFn } from 'react-use'
import { useDebounce, useAsyncFn, useUpdateEffect } from 'react-use'
import { useErrorStyles } from '../../../utils/theme'

const circle = <CircularProgress color="inherit" size={18} />
Expand Down Expand Up @@ -87,7 +87,7 @@ export interface ActionButtonPromiseProps extends ButtonProps {
completeOnClick?: 'use executor' | (() => void)
waiting: React.ReactChild
waitingOnClick?: () => ActionButtonPromiseState
failed: React.ReactChild
failed?: React.ReactChild
failedOnClick?: 'use executor' | (() => void)
completeIcon?: React.ReactNode
failIcon?: React.ReactNode
Expand Down Expand Up @@ -131,6 +131,11 @@ export function ActionButtonPromise(props: ActionButtonPromiseProps) {
}
const completeClick = completeOnClick === 'use executor' ? run : completeOnClick
const failClick = failedOnClick === 'use executor' ? run : failedOnClick

useUpdateEffect(() => {
setState((prev) => (prev === 'init' ? prev : 'init'))
}, [executor])

if (state === 'wait')
return <Button {...b} startIcon={circle} disabled={!waitingOnClick} children={waiting} onClick={cancel} />
if (state === 'complete')
Expand All @@ -144,7 +149,7 @@ export function ActionButtonPromise(props: ActionButtonPromiseProps) {
onClick={completeClick}
/>
)
if (state === 'fail')
if (state === 'fail' && failed)
return (
<Button
{...b}
Expand All @@ -167,6 +172,7 @@ const useStyles = makeStyles()({
},
failed: {
backgroundColor: red[500],
color: '#fff',
'&:hover': {
backgroundColor: red[700],
},
Expand Down
6 changes: 4 additions & 2 deletions packages/mask/src/web3/UI/EthereumChainBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export function EthereumChainBoundary(props: EthereumChainBoundaryProps) {
? Services.Ethereum.switchEthereumChain(expectedChainId, overrides)
: Services.Ethereum.addEthereumChain(chainDetailedCAIP, account, overrides),
])

// recheck
const chainIdHex = await Services.Ethereum.getChainId(overrides)
if (Number.parseInt(chainIdHex, 16) !== expectedChainId) throw new Error('Failed to switch chain.')
} catch {
throw new Error(`Make sure your wallet is on the ${resolveNetworkName(networkType)} network.`)
}
Expand Down Expand Up @@ -194,7 +198,6 @@ export function EthereumChainBoundary(props: EthereumChainBoundaryProps) {
complete={t('plugin_wallet_switch_network', {
network: expectedNetwork,
})}
failed={t('retry')}
executor={onSwitchChain}
completeOnClick={onSwitchChain}
failedOnClick="use executor"
Expand Down Expand Up @@ -235,7 +238,6 @@ export function EthereumChainBoundary(props: EthereumChainBoundaryProps) {
complete={t('plugin_wallet_switch_network', {
network: expectedNetwork,
})}
failed={t('retry')}
executor={onSwitchChain}
completeOnClick={onSwitchChain}
failedOnClick="use executor"
Expand Down