Skip to content

Commit

Permalink
#151 - add logic to disable button while wallets are in detection pro…
Browse files Browse the repository at this point in the history
…cess (#230)
  • Loading branch information
sebastianscatularo authored Jul 7, 2023
1 parent 7627816 commit 6edcc35
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
33 changes: 16 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@
"@solana/wallet-adapter-react-ui": "^0.9.5",
"@solana/wallet-adapter-wallets": "^0.19.5",
"@solana/web3.js": "^1.35.1",
"@terra-money/terra.js": "^3.1.9",
"@terra-money/wallet-provider": "^3.9.4",
"@walletconnect/web3-provider": "^1.7.8",
"@xlabs-libs/wallet-aggregator-algorand": "0.0.1-alpha.16",
"@xlabs-libs/wallet-aggregator-aptos": "^0.0.1-alpha.9",
"@xlabs-libs/wallet-aggregator-core": "^0.0.1-alpha.13",
"@xlabs-libs/wallet-aggregator-core": "^0.0.1-alpha.15",
"@xlabs-libs/wallet-aggregator-evm": "0.0.1-alpha.27",
"@xlabs-libs/wallet-aggregator-injective": "^0.0.1-alpha.12",
"@xlabs-libs/wallet-aggregator-near": "^0.0.1-alpha.5",
"@xlabs-libs/wallet-aggregator-react": "0.0.1-alpha.11",
"@xlabs-libs/wallet-aggregator-react": "0.0.1-alpha.12",
"@xlabs-libs/wallet-aggregator-solana": "^0.0.1-alpha.10",
"@xlabs-libs/wallet-aggregator-sui": "^0.0.1-alpha.4",
"@xlabs-libs/wallet-aggregator-terra": "^0.0.1-alpha.5",
Expand Down
37 changes: 33 additions & 4 deletions src/components/ConnectWalletButton.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import { ChainId } from "@certusone/wormhole-sdk";
import { useCallback, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import { Wallet } from "@xlabs-libs/wallet-aggregator-core";
import {
useChangeWallet,
useUnsetWalletFromChain,
useWallet,
useWalletsForChain,
useWalletsForChainWithStatus,
} from "@xlabs-libs/wallet-aggregator-react";
import ConnectWalletDialog from "./ConnectWalletDialog";
import ToggleConnectedButton from "./ToggleConnectedButton";
import { Typography } from "@material-ui/core";
import { CLUSTER } from "../utils/consts";
import { getIsSanctioned } from "../utils/sanctions";
import { useSelector } from "react-redux";
import { RootState } from "../store";

const ConnectWalletButton = ({ chainId }: { chainId: ChainId }) => {
const wallet = useWallet(chainId);
const changeWallet = useChangeWallet();
const unsetWalletFromChain = useUnsetWalletFromChain();
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [error, setError] = useState<Error | undefined>();
const availableWallets = useWalletsForChain(chainId);

const { wallets: availableWallets, isDetectingWallets } =
useWalletsForChainWithStatus(chainId);
const sourceChain = useSelector(
(state: RootState) => state.transfer.sourceChain
);
const [walletsNotAvailable, setWalletsNotAvailable] = useState(false);
const pk = wallet?.getAddress();

useEffect(() => {
setWalletsNotAvailable(availableWallets.length === 0);
}, [sourceChain, availableWallets]);

const connect = useCallback(
async (w: Wallet) => {
try {
Expand Down Expand Up @@ -79,11 +89,30 @@ const ConnectWalletButton = ({ chainId }: { chainId: ChainId }) => {

return (
<>
{isDetectingWallets && (
<Typography
style={{ textAlign: "center" }}
variant="body2"
color="textPrimary"
>
Detecting wallets ...
</Typography>
)}
{!isDetectingWallets && walletsNotAvailable && (
<Typography
style={{ textAlign: "center" }}
variant="body2"
color="textPrimary"
>
Wallets not detected for the selected chain
</Typography>
)}
<ToggleConnectedButton
connect={handleConnect}
disconnect={disconnect}
connected={!!pk}
pk={pk || ""}
disabled={isDetectingWallets || walletsNotAvailable}
/>
<ConnectWalletDialog
isOpen={isDialogOpen}
Expand Down
3 changes: 3 additions & 0 deletions src/components/ToggleConnectedButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ const ToggleConnectedButton = ({
connected,
pk,
walletIcon,
disabled = false,
}: {
connect(): any;
disconnect(): any;
connected: boolean;
pk: string;
walletIcon?: string;
disabled?: boolean;
}) => {
const classes = useStyles();
const is0x = pk.startsWith("0x");
Expand Down Expand Up @@ -56,6 +58,7 @@ const ToggleConnectedButton = ({
size="small"
onClick={connect}
className={classes.button}
disabled={disabled}
>
Connect
</Button>
Expand Down

0 comments on commit 6edcc35

Please sign in to comment.