@volr/react-ui 0.1.80 → 0.1.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2084,12 +2084,30 @@ function SiweLoginScreen({
2084
2084
  const [walletConnector, setWalletConnector] = useState(
2085
2085
  void 0
2086
2086
  );
2087
+ const [hasWallet, setHasWallet] = useState(null);
2088
+ const waitForEthereum = (timeout = 2e3, interval = 100) => {
2089
+ return new Promise((resolve) => {
2090
+ if (typeof window !== "undefined" && window.ethereum) {
2091
+ resolve(true);
2092
+ return;
2093
+ }
2094
+ const startTime = Date.now();
2095
+ const checkInterval = setInterval(() => {
2096
+ if (typeof window !== "undefined" && window.ethereum) {
2097
+ clearInterval(checkInterval);
2098
+ resolve(true);
2099
+ } else if (Date.now() - startTime >= timeout) {
2100
+ clearInterval(checkInterval);
2101
+ resolve(false);
2102
+ }
2103
+ }, interval);
2104
+ });
2105
+ };
2087
2106
  const detectWalletConnector = () => {
2088
2107
  if (typeof window === "undefined" || !window.ethereum) {
2089
2108
  return void 0;
2090
2109
  }
2091
2110
  const provider = window.ethereum;
2092
- console.log(provider.info);
2093
2111
  if (provider.info?.rdns) {
2094
2112
  return provider.info.rdns;
2095
2113
  }
@@ -2101,26 +2119,36 @@ function SiweLoginScreen({
2101
2119
  return "unknown";
2102
2120
  };
2103
2121
  useEffect(() => {
2104
- if (typeof window !== "undefined" && window.ethereum) {
2105
- window.ethereum.request({ method: "eth_accounts" }).then((accounts) => {
2106
- if (accounts.length > 0) {
2107
- setAccount(accounts[0]);
2108
- setStep("sign");
2109
- setWalletConnector(detectWalletConnector());
2122
+ const initWallet = async () => {
2123
+ const walletAvailable = await waitForEthereum();
2124
+ setHasWallet(walletAvailable);
2125
+ if (walletAvailable && window.ethereum) {
2126
+ try {
2127
+ const accounts = await window.ethereum.request({
2128
+ method: "eth_accounts"
2129
+ });
2130
+ if (accounts.length > 0) {
2131
+ setAccount(accounts[0]);
2132
+ setStep("sign");
2133
+ setWalletConnector(detectWalletConnector());
2134
+ }
2135
+ } catch {
2110
2136
  }
2111
- }).catch(() => {
2112
- });
2113
- }
2137
+ }
2138
+ };
2139
+ initWallet();
2114
2140
  }, []);
2115
2141
  const handleConnectWallet = async () => {
2116
2142
  setError(null);
2117
2143
  setIsLoading(true);
2118
2144
  try {
2119
- if (typeof window === "undefined" || !window.ethereum) {
2145
+ const walletAvailable = await waitForEthereum(3e3, 100);
2146
+ if (!walletAvailable || !window.ethereum) {
2120
2147
  throw new Error(
2121
2148
  "No Ethereum wallet found. Please install MetaMask or another wallet."
2122
2149
  );
2123
2150
  }
2151
+ setHasWallet(true);
2124
2152
  const accounts = await window.ethereum.request({
2125
2153
  method: "eth_requestAccounts"
2126
2154
  });
@@ -2218,8 +2246,8 @@ Issued At: ${issuedAt}`;
2218
2246
  variant: "primary",
2219
2247
  fullWidth: true,
2220
2248
  onClick: handleConnectWallet,
2221
- disabled: isLoading,
2222
- children: isLoading ? t("common.loading") : t("login.wallet.connect")
2249
+ disabled: isLoading || hasWallet === null,
2250
+ children: isLoading || hasWallet === null ? t("common.loading") : t("login.wallet.connect")
2223
2251
  }
2224
2252
  ) : /* @__PURE__ */ jsx(
2225
2253
  Button,