|
| 1 | +import { SwapOutlined } from '@ant-design/icons'; |
| 2 | +import { isNumber } from 'lodash'; |
| 3 | +import { useCallback, useEffect, useMemo } from 'react'; |
| 4 | +import { Config } from 'wagmi'; |
| 5 | + |
| 6 | +// eslint-disable-next-line @nx/enforce-module-boundaries |
| 7 | +import { COLOR, useScreen } from 'libs/ui-theme/src'; |
| 8 | +import { Button, ButtonProps, ConfigProvider } from 'antd'; |
| 9 | +import { Chain } from 'viem'; |
| 10 | +import { SwitchChainMutateAsync } from 'wagmi/query'; |
| 11 | + |
| 12 | +type YellowButtonProps = ButtonProps & { |
| 13 | + children: React.ReactNode; |
| 14 | +}; |
| 15 | + |
| 16 | +export const YellowButton = ({ children, ...props }: YellowButtonProps) => ( |
| 17 | + <ConfigProvider |
| 18 | + theme={{ |
| 19 | + token: { |
| 20 | + colorPrimary: COLOR.YELLOW_PRIMARY, |
| 21 | + colorBgBase: COLOR.YELLOW_SECONDARY, |
| 22 | + colorTextBase: COLOR.YELLOW_PRIMARY, |
| 23 | + colorBorder: COLOR.YELLOW_PRIMARY, |
| 24 | + }, |
| 25 | + }} |
| 26 | + > |
| 27 | + <Button {...props} size="large"> |
| 28 | + {children} |
| 29 | + </Button> |
| 30 | + </ConfigProvider> |
| 31 | +); |
| 32 | + |
| 33 | +type SwitchNetworkButtonProps = { |
| 34 | + walletConnectedChain: Chain | undefined; |
| 35 | + networkId: number | null; |
| 36 | + switchChainAsync: SwitchChainMutateAsync<Config, unknown>; |
| 37 | + isPending: boolean; |
| 38 | +}; |
| 39 | + |
| 40 | +export const SwitchNetworkButton = ({ |
| 41 | + walletConnectedChain, |
| 42 | + networkId, |
| 43 | + switchChainAsync, |
| 44 | + isPending, |
| 45 | +}: SwitchNetworkButtonProps) => { |
| 46 | + const { isMobile } = useScreen(); |
| 47 | + |
| 48 | + /** |
| 49 | + * @returns {boolean} - true if the wallet is connected to wrong network |
| 50 | + * (ie. chain ID from wallet is different from the provided chain ID e.g. selected in the dropdown) |
| 51 | + */ |
| 52 | + const isConnectedToWrongNetwork = useMemo(() => { |
| 53 | + if (!isNumber(walletConnectedChain?.id) || !isNumber(networkId)) return false; |
| 54 | + |
| 55 | + return walletConnectedChain?.id !== networkId; |
| 56 | + }, [walletConnectedChain, networkId]); |
| 57 | + |
| 58 | + const onSwitchNetwork = useCallback(async () => { |
| 59 | + if (!networkId) return; |
| 60 | + |
| 61 | + try { |
| 62 | + await switchChainAsync({ chainId: networkId }); |
| 63 | + } catch (error) { |
| 64 | + console.error(error); |
| 65 | + } |
| 66 | + }, [networkId, switchChainAsync]); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + if (isConnectedToWrongNetwork) { |
| 70 | + onSwitchNetwork(); |
| 71 | + } |
| 72 | + }, [isConnectedToWrongNetwork, onSwitchNetwork]); |
| 73 | + |
| 74 | + if (!isConnectedToWrongNetwork) return null; |
| 75 | + return ( |
| 76 | + <YellowButton |
| 77 | + loading={isPending} |
| 78 | + type="default" |
| 79 | + onClick={onSwitchNetwork} |
| 80 | + icon={<SwapOutlined />} |
| 81 | + > |
| 82 | + {!isMobile && 'Switch network'} |
| 83 | + </YellowButton> |
| 84 | + ); |
| 85 | +}; |
0 commit comments