Skip to content

Commit

Permalink
Merge pull request #49 from TxnLab/txn-1267-only-reconnect-to-active-…
Browse files Browse the repository at this point in the history
…wallet
  • Loading branch information
drichar authored Mar 11, 2023
2 parents 6b7467b + 0a48316 commit cbbfefb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "https://github.com/txnlab/use-wallet/issues"
},
"homepage": "https://txnlab.github.io/use-wallet",
"version": "1.2.3",
"version": "1.2.4",
"description": "React hooks for using Algorand compatible wallets in dApps.",
"scripts": {
"dev": "yarn storybook",
Expand Down
12 changes: 12 additions & 0 deletions src/utils/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PROVIDER_ID } from "src/constants";
import { useWalletStore } from "src/store";

export const getActiveProviders = () => {
const accounts = useWalletStore.getState().accounts;
return [...new Set(accounts.map((acct) => acct.providerId))];
}

export const isActiveProvider = (id: PROVIDER_ID) => {
const activeProviders = getActiveProviders();
return activeProviders.includes(id);
}
8 changes: 7 additions & 1 deletion src/utils/reconnectProviders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { WalletClient } from "../types";
import { clearAccounts } from "./clearAccounts";
import { isActiveProvider } from "./providers";

type SupportedProviders = { [x: string]: Promise<WalletClient | null> };

Expand All @@ -9,7 +10,12 @@ export const reconnectProviders = async (providers: SupportedProviders) => {

for (const client of clients) {
const c = await client;
c?.reconnect(() => clearAccounts(c?.metadata.id));
const id = c?.metadata.id;

// Only reconnect to active providers
if (id && isActiveProvider(id)) {
c.reconnect(() => clearAccounts(id));
}
}
} catch (e) {
console.error(e);
Expand Down

0 comments on commit cbbfefb

Please sign in to comment.