Skip to content

Commit

Permalink
feat: add network on ConnectedButton
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Sep 20, 2023
1 parent 7727fa5 commit 4109695
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
11 changes: 2 additions & 9 deletions libs/oeth/redeem/src/views/RedeemView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { alpha, Box, CircularProgress, Stack, Typography } from '@mui/material';
import { GasPopover } from '@origin/oeth/shared';
import { Card, TokenInput } from '@origin/shared/components';
import { tokens } from '@origin/shared/contracts';
import {
ConnectedButton,
EnsureNetworkProvider,
usePrices,
} from '@origin/shared/providers';
import { ConnectedButton, usePrices } from '@origin/shared/providers';
import { composeContexts } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { useAccount, useBalance } from 'wagmi';
Expand Down Expand Up @@ -47,10 +43,7 @@ const tokenInputStyles = {
};

export const RedeemView = () =>
composeContexts(
[[RedeemProvider], [EnsureNetworkProvider]],
<RedeemViewWrapped />,
);
composeContexts([[RedeemProvider]], <RedeemViewWrapped />);

function RedeemViewWrapped() {
const intl = useIntl();
Expand Down
11 changes: 2 additions & 9 deletions libs/oeth/swap/src/views/SwapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import {
} from '@mui/material';
import { ApyHeader, GasPopover } from '@origin/oeth/shared';
import { Card, TokenInput } from '@origin/shared/components';
import {
ConnectedButton,
EnsureNetworkProvider,
usePrices,
} from '@origin/shared/providers';
import { ConnectedButton, usePrices } from '@origin/shared/providers';
import { composeContexts, isNilOrEmpty } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { useAccount, useBalance } from 'wagmi';
Expand Down Expand Up @@ -76,10 +72,7 @@ const tokenInputStyles = {
};

export const SwapView = () =>
composeContexts(
[[SwapProvider], [EnsureNetworkProvider]],
<SwapViewWrapped />,
);
composeContexts([[SwapProvider]], <SwapViewWrapped />);

function SwapViewWrapped() {
const intl = useIntl();
Expand Down
48 changes: 36 additions & 12 deletions libs/shared/providers/src/wagmi/components/ConnectedButton.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import { Button, CircularProgress } from '@mui/material';
import { isNilOrEmpty } from '@origin/shared/utils';
import { useConnectModal } from '@rainbow-me/rainbowkit';
import { useIntl } from 'react-intl';
import { useAccount } from 'wagmi';
import { useAccount, useNetwork, useSwitchNetwork } from 'wagmi';

import type { ButtonProps } from '@mui/material';

export type ConnectedButtonProps = ButtonProps &
Omit<
HTMLButtonElement,
'form' | 'translate' | 'contentEditable' | 'inputMode'
>;
export type ConnectedButtonProps = {
targetChainId?: number;
disableNetworkCheck?: boolean;
} & ButtonProps;

export const ConnectedButton = (props: ButtonProps) => {
export const ConnectedButton = ({
children,
onClick,
disabled,
targetChainId,
disableNetworkCheck,
...rest
}: ConnectedButtonProps) => {
const intl = useIntl();
const { chain, chains } = useNetwork();
const { isConnected, isConnecting } = useAccount();
const { openConnectModal } = useConnectModal();
const { switchNetwork } = useSwitchNetwork();

if (!isConnected) {
const { children, onClick, disabled, ...rest } = props;
const handleSwitchToDefaultNetwork = () => {
switchNetwork(targetChainId ?? chains[0].id);
};

if (!isConnected) {
return (
<Button
{...rest}
Expand All @@ -32,14 +43,27 @@ export const ConnectedButton = (props: ButtonProps) => {
}

if (isConnecting) {
const { children, onClick, disabled, ...rest } = props;

return (
<Button {...rest} disabled>
<CircularProgress sx={{ color: 'primary.contrastText' }} />
</Button>
);
}

return <Button {...props} />;
if (
!disableNetworkCheck &&
isNilOrEmpty(chains.find((c) => c.id === chain?.id))
) {
return (
<Button onClick={handleSwitchToDefaultNetwork} {...rest}>
{intl.formatMessage({ defaultMessage: 'Switch network' })}
</Button>
);
}

return (
<Button onClick={onClick} disabled={disabled} {...rest}>
{children}
</Button>
);
};

0 comments on commit 4109695

Please sign in to comment.