Skip to content

Commit b5ea6df

Browse files
feat: move init functions to utils directory
1 parent 6bf19d0 commit b5ea6df

8 files changed

Lines changed: 90 additions & 82 deletions

File tree

src/components/Example/Connect.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import useWallet from "../../hooks/useWallet";
2+
import { useWallet } from "../../index";
33

44
export default function ConnectWallet() {
55
const {
@@ -48,16 +48,18 @@ export default function ConnectWallet() {
4848
Set Active
4949
</button>
5050
<div>
51-
{provider.isActive && provider.accounts.length && (
51+
{provider.isActive && provider.accounts.length ? (
5252
<select
5353
value={activeAccount?.address}
5454
onChange={(e) => provider.setActiveAccount(e.target.value)}
5555
>
5656
{provider.accounts.map((account) => (
57-
<option value={account.address}>{account.address}</option>
57+
<option key={account.address} value={account.address}>
58+
{account.address}
59+
</option>
5860
))}
5961
</select>
60-
)}
62+
) : null}
6163
</div>
6264
</div>
6365
</div>

src/components/Example/Example.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React, { useEffect } from "react";
2-
import { reconnectProviders, initializeProviders } from "../../hooks/useWallet";
3-
import WalletProvider from "../../store/state/clientStore";
2+
import {
3+
reconnectProviders,
4+
initializeProviders,
5+
WalletProvider,
6+
} from "../../index";
47
import Account from "./Account";
58
import Connect from "./Connect";
69
import Transact from "./Transact";

src/hooks/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export { initializeProviders, reconnectProviders } from "./useWallet";
21
export { default as useWallet } from "./useWallet";

src/hooks/useWallet.ts

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,10 @@ import { useMemo, useContext } from "react";
22
import type algosdk from "algosdk";
33
import { getAlgosdk } from "../algod";
44
import { useWalletStore, walletStoreSelector } from "../store/index";
5-
import {
6-
PROVIDER_ID,
7-
TransactionsArray,
8-
WalletClient,
9-
Network,
10-
} from "../types";
5+
import { PROVIDER_ID, TransactionsArray, WalletClient } from "../types";
116
import { ClientContext } from "../store/state/clientStore";
127
import allClients from "../clients";
138
import shallow from "zustand/shallow";
14-
import {
15-
DEFAULT_NODE_BASEURL,
16-
DEFAULT_NODE_TOKEN,
17-
DEFAULT_NODE_PORT,
18-
DEFAULT_NETWORK,
19-
} from "../constants";
20-
21-
type SupportedProviders = { [x: string]: Promise<WalletClient | null> };
22-
23-
type NodeConfig = {
24-
network: Network;
25-
nodeServer: string;
26-
nodeToken?: string;
27-
nodePort?: string;
28-
};
29-
30-
export const initializeProviders = (
31-
providers?: PROVIDER_ID[],
32-
nodeConfig?: NodeConfig,
33-
algosdkStatic?: typeof algosdk
34-
) => {
35-
const initializedProviders: SupportedProviders = {};
36-
37-
const {
38-
network = DEFAULT_NETWORK,
39-
nodeServer = DEFAULT_NODE_BASEURL,
40-
nodePort = DEFAULT_NODE_PORT,
41-
nodeToken = DEFAULT_NODE_TOKEN,
42-
} = nodeConfig || {};
43-
44-
if (!providers || providers.length === 0)
45-
for (const [id, client] of Object.entries(allClients)) {
46-
if (id === "kmd") {
47-
continue;
48-
}
49-
50-
initializedProviders[id] = client.init({
51-
network,
52-
algodOptions: [nodeToken, nodeServer, nodePort],
53-
algosdkStatic: algosdkStatic,
54-
});
55-
}
56-
57-
if (providers) {
58-
for (const id of providers) {
59-
initializedProviders[id] = allClients[id].init({
60-
network,
61-
algodOptions: [nodeToken, nodeServer, nodePort],
62-
algosdkStatic: algosdkStatic,
63-
});
64-
}
65-
}
66-
67-
return initializedProviders;
68-
};
69-
70-
export const reconnectProviders = async (providers: SupportedProviders) => {
71-
try {
72-
const clients = Object.values(providers);
73-
74-
for (const client of clients) {
75-
const c = await client;
76-
c?.reconnect(c?.disconnect);
77-
}
78-
} catch (e) {
79-
console.error(e);
80-
}
81-
};
829

8310
export { PROVIDER_ID };
8411

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export { useWallet, initializeProviders, reconnectProviders } from "./hooks";
1+
export { useWallet } from "./hooks";
2+
export { initializeProviders, reconnectProviders } from "./utils";
23
export { WalletProvider } from "./store";
34
export * from "./constants";
45
export * from "./types";

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { initializeProviders } from "./initializeProviders";
2+
export { reconnectProviders } from "./reconnectProviders";

src/utils/initializeProviders.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type algosdk from "algosdk";
2+
import { PROVIDER_ID, WalletClient, Network } from "../types";
3+
import allClients from "../clients";
4+
import {
5+
DEFAULT_NODE_BASEURL,
6+
DEFAULT_NODE_TOKEN,
7+
DEFAULT_NODE_PORT,
8+
DEFAULT_NETWORK,
9+
} from "../constants";
10+
11+
type SupportedProviders = { [x: string]: Promise<WalletClient | null> };
12+
13+
type NodeConfig = {
14+
network: Network;
15+
nodeServer: string;
16+
nodeToken?: string;
17+
nodePort?: string;
18+
};
19+
20+
export const initializeProviders = (
21+
providers?: PROVIDER_ID[],
22+
nodeConfig?: NodeConfig,
23+
algosdkStatic?: typeof algosdk
24+
) => {
25+
const initializedProviders: SupportedProviders = {};
26+
27+
const {
28+
network = DEFAULT_NETWORK,
29+
nodeServer = DEFAULT_NODE_BASEURL,
30+
nodePort = DEFAULT_NODE_PORT,
31+
nodeToken = DEFAULT_NODE_TOKEN,
32+
} = nodeConfig || {};
33+
34+
if (!providers || providers.length === 0)
35+
for (const [id, client] of Object.entries(allClients)) {
36+
if (id === "kmd") {
37+
continue;
38+
}
39+
40+
initializedProviders[id] = client.init({
41+
network,
42+
algodOptions: [nodeToken, nodeServer, nodePort],
43+
algosdkStatic: algosdkStatic,
44+
});
45+
}
46+
47+
if (providers) {
48+
for (const id of providers) {
49+
initializedProviders[id] = allClients[id].init({
50+
network,
51+
algodOptions: [nodeToken, nodeServer, nodePort],
52+
algosdkStatic: algosdkStatic,
53+
});
54+
}
55+
}
56+
57+
return initializedProviders;
58+
};

src/utils/reconnectProviders.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { WalletClient } from "../types";
2+
3+
type SupportedProviders = { [x: string]: Promise<WalletClient | null> };
4+
5+
export const reconnectProviders = async (providers: SupportedProviders) => {
6+
try {
7+
const clients = Object.values(providers);
8+
9+
for (const client of clients) {
10+
const c = await client;
11+
c?.reconnect(c?.disconnect);
12+
}
13+
} catch (e) {
14+
console.error(e);
15+
}
16+
};

0 commit comments

Comments
 (0)