-
Notifications
You must be signed in to change notification settings - Fork 122
/
WalletSelectorContext.tsx
187 lines (167 loc) · 5.79 KB
/
WalletSelectorContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { setupCoin98Wallet } from "@near-wallet-selector/coin98-wallet";
import type { AccountState, WalletSelector } from "@near-wallet-selector/core";
import { setupWalletSelector } from "@near-wallet-selector/core";
import { setupHereWallet } from "@near-wallet-selector/here-wallet";
import { setupMathWallet } from "@near-wallet-selector/math-wallet";
import { setupMeteorWallet } from "@near-wallet-selector/meteor-wallet";
import { setupNarwallets } from "@near-wallet-selector/narwallets";
import type { WalletSelectorModal } from "@near-wallet-selector/modal-ui";
import { setupModal } from "@near-wallet-selector/modal-ui";
import { setupNearWallet } from "@near-wallet-selector/near-wallet";
import { setupNearFi } from "@near-wallet-selector/nearfi";
import { setupNightly } from "@near-wallet-selector/nightly";
import { setupNightlyConnect } from "@near-wallet-selector/nightly-connect";
import { setupSender } from "@near-wallet-selector/sender";
import { setupWalletConnect } from "@near-wallet-selector/wallet-connect";
import { setupNearSnap } from "@near-wallet-selector/near-snap";
import { setupWelldoneWallet } from "@near-wallet-selector/welldone-wallet";
import { setupXDEFI } from "@near-wallet-selector/xdefi";
import type { ReactNode } from "react";
import React, {
useCallback,
useContext,
useEffect,
useState,
useMemo,
} from "react";
import { distinctUntilChanged, map } from "rxjs";
import { setupNeth } from "@near-wallet-selector/neth";
import { setupOptoWallet } from "@near-wallet-selector/opto-wallet";
import { setupFinerWallet } from "@near-wallet-selector/finer-wallet";
import { Loading } from "../components/Loading";
import { setupMyNearWallet } from "@near-wallet-selector/my-near-wallet";
import { setupLedger } from "@near-wallet-selector/ledger";
import { CONTRACT_ID } from "../constants";
declare global {
interface Window {
selector: WalletSelector;
modal: WalletSelectorModal;
}
}
interface WalletSelectorContextValue {
selector: WalletSelector;
modal: WalletSelectorModal;
accounts: Array<AccountState>;
accountId: string | null;
}
const WalletSelectorContext =
React.createContext<WalletSelectorContextValue | null>(null);
export const WalletSelectorContextProvider: React.FC<{
children: ReactNode;
}> = ({ children }) => {
const [selector, setSelector] = useState<WalletSelector | null>(null);
const [modal, setModal] = useState<WalletSelectorModal | null>(null);
const [accounts, setAccounts] = useState<Array<AccountState>>([]);
const [loading, setLoading] = useState<boolean>(true);
const init = useCallback(async () => {
const _selector = await setupWalletSelector({
network: "testnet",
debug: true,
modules: [
setupMyNearWallet(),
setupLedger(),
setupNearWallet(),
setupSender(),
setupMathWallet(),
setupNightly(),
setupMeteorWallet(),
setupNearSnap(),
setupNarwallets(),
setupWelldoneWallet(),
setupHereWallet(),
setupCoin98Wallet(),
setupNearFi(),
setupNeth({
gas: "300000000000000",
bundle: false,
}),
setupOptoWallet(),
setupFinerWallet(),
setupXDEFI(),
setupWalletConnect({
projectId: "c4f79cc...",
metadata: {
name: "NEAR Wallet Selector",
description: "Example dApp used by NEAR Wallet Selector",
url: "https://github.com/near/wallet-selector",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
}),
setupNightlyConnect({
url: "wss://relay.nightly.app/app",
appMetadata: {
additionalInfo: "",
application: "NEAR Wallet Selector",
description: "Example dApp used by NEAR Wallet Selector",
icon: "https://near.org/wp-content/uploads/2020/09/cropped-favicon-192x192.png",
},
}),
],
});
const _modal = setupModal(_selector, {
contractId: CONTRACT_ID,
});
const state = _selector.store.getState();
setAccounts(state.accounts);
// this is added for debugging purpose only
// for more information (https://github.com/near/wallet-selector/pull/764#issuecomment-1498073367)
window.selector = _selector;
window.modal = _modal;
setSelector(_selector);
setModal(_modal);
setLoading(false);
}, []);
useEffect(() => {
init().catch((err) => {
console.error(err);
alert("Failed to initialise wallet selector");
});
}, [init]);
useEffect(() => {
if (!selector) {
return;
}
const subscription = selector.store.observable
.pipe(
map((state) => state.accounts),
distinctUntilChanged()
)
.subscribe((nextAccounts) => {
console.log("Accounts Update", nextAccounts);
setAccounts(nextAccounts);
});
const onHideSubscription = modal!.on("onHide", ({ hideReason }) => {
console.log(`The reason for hiding the modal ${hideReason}`);
});
return () => {
subscription.unsubscribe();
onHideSubscription.remove();
};
}, [selector, modal]);
const walletSelectorContextValue = useMemo<WalletSelectorContextValue>(
() => ({
selector: selector!,
modal: modal!,
accounts,
accountId: accounts.find((account) => account.active)?.accountId || null,
}),
[selector, modal, accounts]
);
if (loading) {
return <Loading />;
}
return (
<WalletSelectorContext.Provider value={walletSelectorContextValue}>
{children}
</WalletSelectorContext.Provider>
);
};
export function useWalletSelector() {
const context = useContext(WalletSelectorContext);
if (!context) {
throw new Error(
"useWalletSelector must be used within a WalletSelectorContextProvider"
);
}
return context;
}