-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update behavior to switch chains
- Loading branch information
1 parent
84a83d4
commit 72ccf22
Showing
9 changed files
with
149 additions
and
185 deletions.
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
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,41 @@ | ||
import { chainMetadata } from '@venusprotocol/chains'; | ||
|
||
import { Button } from 'components/Button'; | ||
import { useTranslation } from 'libs/translations'; | ||
import { useChainId, useSwitchChain } from 'libs/wallet'; | ||
import type { ChainId } from 'types'; | ||
import { useAccount } from 'wagmi'; | ||
|
||
// TODO: add tests | ||
|
||
export interface SwitchChainProps extends React.HTMLAttributes<HTMLDivElement> { | ||
chainId?: ChainId; | ||
} | ||
|
||
export const SwitchChain: React.FC<SwitchChainProps> = ({ children, chainId, ...otherProps }) => { | ||
const { chainId: currentChainId } = useChainId(); | ||
const targetChainId = chainId || currentChainId; | ||
|
||
const { chainId: walletChainId } = useAccount(); | ||
const isOnWrongChain = walletChainId !== targetChainId; | ||
const targetChain = chainMetadata[targetChainId]; | ||
|
||
const { switchChain } = useSwitchChain(); | ||
const { t } = useTranslation(); | ||
|
||
const handleSwitchChain = () => switchChain({ chainId: targetChainId }); | ||
|
||
return ( | ||
<div {...otherProps}> | ||
{isOnWrongChain ? ( | ||
<Button className="w-full" onClick={handleSwitchChain}> | ||
{t('connectWallet.switchChain', { | ||
chainName: targetChain.name, | ||
})} | ||
</Button> | ||
) : ( | ||
children | ||
)} | ||
</div> | ||
); | ||
}; |
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
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
Oops, something went wrong.