Skip to content

Commit addc657

Browse files
Merge pull request #155 from valory-xyz/tanya/select-chain
(govern): switch chain to mainnet, move yellow button to libs
2 parents 8f2ee00 + b3609ad commit addc657

File tree

9 files changed

+117
-95
lines changed

9 files changed

+117
-95
lines changed

apps/autonolas-registry/common-util/Login/LoginV2.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { isAddressProhibited } from 'libs/util-prohibited-data/src/index';
1313

1414
import { setUserBalance } from 'store/setup';
1515

16-
import { YellowButton } from '../YellowButton';
16+
import { YellowButton } from 'libs/ui-components/src';
1717
import { useHelpers } from '../hooks';
1818
import { SolanaWallet } from './SolanaWallet';
1919

apps/autonolas-registry/common-util/YellowButton.jsx

-25
This file was deleted.

apps/govern/components/Layout/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LogoSvg } from './Logos';
1111
import { NavigationMenu } from './Menu';
1212
import { CustomLayout, Logo, OlasHeader, RightMenu } from './styles';
1313
import { NavDropdown } from 'libs/ui-components/src';
14+
import { SwitchNetworkButton } from 'components/Login/SwitchNetworkButton';
1415

1516
const { Content } = AntdLayout;
1617

@@ -33,6 +34,7 @@ export const Layout: FC<LayoutProps> = ({ children }) => {
3334
<NavigationMenu />
3435
<RightMenu>
3536
<Balance />
37+
<SwitchNetworkButton />
3638
<LoginV2 />
3739
</RightMenu>
3840
</OlasHeader>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useAccount, useSwitchChain } from 'wagmi';
2+
3+
import { SwitchNetworkButton as SwitchNetworkButtonComponent } from 'libs/ui-components/src';
4+
import { mainnet } from 'viem/chains';
5+
6+
export const SwitchNetworkButton = () => {
7+
const { chain: walletConnectedChain } = useAccount();
8+
const { switchChainAsync, isPending } = useSwitchChain();
9+
10+
return (
11+
<SwitchNetworkButtonComponent
12+
walletConnectedChain={walletConnectedChain}
13+
networkId={mainnet.id}
14+
switchChainAsync={switchChainAsync}
15+
isPending={isPending}
16+
/>
17+
);
18+
};
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,20 @@
1-
import { SwapOutlined } from '@ant-design/icons';
2-
import { isNumber } from 'lodash';
3-
import { useCallback, useEffect, useMemo } from 'react';
41
import { useAccount, useSwitchChain } from 'wagmi';
52

6-
import { useScreen } from 'libs/ui-theme/src';
7-
83
import { useAppSelector } from 'store/index';
94

10-
import { YellowButton } from './YellowButton';
5+
import { SwitchNetworkButton as SwitchNetworkButtonComponent } from 'libs/ui-components/src';
116

127
export const SwitchNetworkButton = () => {
13-
const { isMobile } = useScreen();
148
const { chain: walletConnectedChain } = useAccount();
159
const { networkId } = useAppSelector((state) => state.network);
1610
const { switchChainAsync, isPending } = useSwitchChain();
1711

18-
/**
19-
* @returns {boolean} - true if the wallet is connected to wrong network
20-
* (ie. chain ID from wallet is different from the chain ID selected in the dropdown)
21-
*/
22-
const isConnectedToWrongNetwork = useMemo(() => {
23-
if (!isNumber(walletConnectedChain?.id) || !isNumber(networkId)) return false;
24-
25-
return walletConnectedChain?.id !== networkId;
26-
}, [walletConnectedChain, networkId]);
27-
28-
const onSwitchNetwork = useCallback(async () => {
29-
if (!networkId) return;
30-
31-
try {
32-
await switchChainAsync({ chainId: networkId });
33-
} catch (error) {
34-
console.error(error);
35-
}
36-
}, [networkId, switchChainAsync]);
37-
38-
useEffect(() => {
39-
if (isConnectedToWrongNetwork) {
40-
onSwitchNetwork();
41-
}
42-
}, [isConnectedToWrongNetwork, onSwitchNetwork]);
43-
44-
if (!isConnectedToWrongNetwork) return null;
4512
return (
46-
<YellowButton
47-
loading={isPending}
48-
type="default"
49-
onClick={onSwitchNetwork}
50-
icon={<SwapOutlined />}
51-
>
52-
{!isMobile && 'Switch network'}
53-
</YellowButton>
13+
<SwitchNetworkButtonComponent
14+
walletConnectedChain={walletConnectedChain}
15+
networkId={networkId}
16+
switchChainAsync={switchChainAsync}
17+
isPending={isPending}
18+
/>
5419
);
5520
};

apps/launch/components/Login/YellowButton.tsx

-24
This file was deleted.

libs/ui-components/src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './lib/Footer';
2-
export * from './lib/Caption';
3-
export * from './lib/TextWithTooltip';
42
export * from './lib/AddressLink';
3+
export * from './lib/Caption';
54
export * from './lib/NavDropdown';
5+
export * from './lib/SwitchNetworkButton';
6+
export * from './lib/TextWithTooltip';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
};

apps/autonolas-registry/tests/common-util/YellowButton.test.tsx libs/ui-components/src/lib/YellowButton.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '@testing-library/jest-dom';
22
import { render } from '@testing-library/react';
33

4-
import { YellowButton } from '../../common-util/YellowButton';
4+
import { YellowButton } from './SwitchNetworkButton';
55

66
describe('YellowButton', () => {
77
it('should display a yellow button', () => {

0 commit comments

Comments
 (0)