forked from solana-foundation/connectorkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector-provider.tsx
More file actions
310 lines (272 loc) · 11.2 KB
/
connector-provider.tsx
File metadata and controls
310 lines (272 loc) · 11.2 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
'use client';
import React, { createContext, useContext, useMemo, useRef, useSyncExternalStore } from 'react';
import type { ReactNode } from 'react';
import { ConnectorClient } from '../lib/core/connector-client';
import type { ConnectorConfig } from '../types/connector';
import type { ExtendedConnectorConfig } from '../config/default-config';
import { ConnectorErrorBoundary } from './error-boundary';
import { useWalletConnectUri } from './walletconnect-context';
import { installPolyfills } from '../lib/utils/polyfills';
import { createLogger } from '../lib/utils/secure-logger';
import type { MobileWalletAdapterConfig, RegisterMwaConfig } from '../types/mobile';
import type {
WalletConnectorId,
WalletConnectorMetadata,
ConnectOptions,
WalletStatus,
SessionAccount,
} from '../types/session';
import type { Address } from '@solana/addresses';
// Re-export for backwards compatibility
export type { MobileWalletAdapterConfig };
const logger = createLogger('ConnectorProvider');
installPolyfills();
declare global {
interface Window {
__connectorClient?: ConnectorClient;
}
}
export type ConnectorSnapshot = ReturnType<ConnectorClient['getSnapshot']> & {
// ========================================================================
// Legacy Actions (kept for backwards compatibility)
// ========================================================================
/** @deprecated Use `connectWallet(connectorId)` instead */
select: (walletName: string) => Promise<void>;
/** @deprecated Use `disconnectWallet()` instead */
disconnect: () => Promise<void>;
selectAccount: (address: string) => Promise<void>;
// ========================================================================
// WalletConnect URI
// ========================================================================
/** WalletConnect URI for QR code display (null when not connecting via WalletConnect) */
walletConnectUri: string | null;
/** Clear the WalletConnect URI (call when modal closes or connection completes) */
clearWalletConnectUri: () => void;
// ========================================================================
// vNext Actions
// ========================================================================
/** Connect to a wallet by connector ID (vNext) */
connectWallet: (connectorId: WalletConnectorId, options?: ConnectOptions) => Promise<void>;
/** Disconnect the current wallet session (vNext) */
disconnectWallet: () => Promise<void>;
// ========================================================================
// vNext Derived Fields (from wallet status state machine)
// ========================================================================
/** Full wallet status object (discriminated union) */
walletStatus: WalletStatus;
/** Whether a wallet is connected */
isConnected: boolean;
/** Whether a wallet connection is in progress */
isConnecting: boolean;
/** Whether an error occurred */
isError: boolean;
/** Error object if status is 'error', otherwise null */
walletError: Error | null;
/** Currently selected account address (null if not connected) */
account: Address | null;
/** All available accounts in the session (empty if not connected) */
sessionAccounts: SessionAccount[];
/** Connected connector ID (null if not connected) */
connectorId: WalletConnectorId | null;
/** Resolved connector metadata for the connected wallet (null if not connected) */
connector: WalletConnectorMetadata | null;
};
export const ConnectorContext = createContext<ConnectorClient | null>(null);
ConnectorContext.displayName = 'ConnectorContext';
function ConnectorProviderInternal({
children,
config,
mobile,
}: {
children: ReactNode;
config?: ConnectorConfig;
mobile?: MobileWalletAdapterConfig;
}) {
const clientRef = useRef<ConnectorClient | null>(null);
const getClient = React.useCallback(() => {
if (!clientRef.current) {
try {
clientRef.current = new ConnectorClient(config);
if (typeof window !== 'undefined') {
window.__connectorClient = clientRef.current;
}
if (config?.debug) {
logger.info('Client initialized successfully');
}
} catch (error) {
const err = error as Error;
logger.error('Failed to initialize client', { error: err });
const extendedConfig = config as ExtendedConnectorConfig;
if (extendedConfig?.errorBoundary?.onError) {
extendedConfig.errorBoundary.onError(err, {
componentStack: 'client-initialization',
digest: `constructor-${new Date().toISOString()}`,
});
}
return null;
}
}
return clientRef.current;
}, [config]);
const client = getClient();
React.useEffect(() => {
const currentClient = clientRef.current;
if (currentClient) {
const privateClient = currentClient as unknown as { initialize?: () => void };
if (privateClient.initialize && typeof privateClient.initialize === 'function') {
privateClient.initialize();
}
}
return () => {
if (typeof window !== 'undefined') {
window.__connectorClient = undefined;
}
if (currentClient && typeof currentClient.destroy === 'function') {
currentClient.destroy();
}
};
}, []);
React.useEffect(() => {
if (!mobile) return;
let cancelled = false;
(async () => {
try {
const mod =
(await import('@solana-mobile/wallet-standard-mobile')) as typeof import('@solana-mobile/wallet-standard-mobile');
if (cancelled) return;
const {
registerMwa,
createDefaultAuthorizationCache,
createDefaultChainSelector,
createDefaultWalletNotFoundHandler,
} = mod;
const defaultChains: RegisterMwaConfig['chains'] = [
'solana:mainnet',
'solana:devnet',
'solana:testnet',
];
const mwaConfig: RegisterMwaConfig = {
appIdentity: mobile.appIdentity,
authorizationCache: mobile.authorizationCache ?? createDefaultAuthorizationCache(),
chains: mobile.chains ?? defaultChains,
chainSelector: mobile.chainSelector ?? createDefaultChainSelector(),
remoteHostAuthority: mobile.remoteHostAuthority,
onWalletNotFound: mobile.onWalletNotFound ?? createDefaultWalletNotFoundHandler(),
};
registerMwa(mwaConfig);
} catch (e) {
// Failed to register Mobile Wallet Adapter
}
})();
return () => {
cancelled = true;
};
}, [mobile]);
return <ConnectorContext.Provider value={client}>{children}</ConnectorContext.Provider>;
}
export function ConnectorProvider({
children,
config,
mobile,
}: {
children: ReactNode;
config?: ExtendedConnectorConfig;
mobile?: MobileWalletAdapterConfig;
}) {
const extendedConfig = config as ExtendedConnectorConfig;
const errorBoundaryConfig = extendedConfig?.errorBoundary;
if (!errorBoundaryConfig?.enabled) {
return (
<ConnectorProviderInternal config={config} mobile={mobile}>
{children}
</ConnectorProviderInternal>
);
}
return (
<ConnectorErrorBoundary
maxRetries={errorBoundaryConfig.maxRetries ?? 3}
onError={errorBoundaryConfig.onError}
fallback={errorBoundaryConfig.fallback}
>
<ConnectorProviderInternal config={config} mobile={mobile}>
{children}
</ConnectorProviderInternal>
</ConnectorErrorBoundary>
);
}
export function useConnector(): ConnectorSnapshot {
const client = useContext(ConnectorContext);
if (!client) {
throw new Error(
'useConnector must be used within ConnectorProvider. ' +
'Wrap your app with <ConnectorProvider> or <UnifiedProvider> to use connector hooks.',
);
}
// Get WalletConnect URI from context (gracefully returns null if not in WalletConnect flow)
const { uri: walletConnectUri, clearUri: clearWalletConnectUri } = useWalletConnectUri();
const state = useSyncExternalStore(
React.useCallback(cb => client.subscribe(cb), [client]),
React.useCallback(() => client.getSnapshot(), [client]),
React.useCallback(() => client.getSnapshot(), [client]),
);
// Legacy + vNext actions
const methods = useMemo(
() => ({
// Legacy (kept for backwards compatibility)
select: client.select.bind(client),
disconnect: client.disconnect.bind(client),
selectAccount: client.selectAccount.bind(client),
// vNext
connectWallet: client.connectWallet.bind(client),
disconnectWallet: client.disconnectWallet.bind(client),
}),
[client],
);
// Derive vNext convenience fields from wallet status state machine
const vNextFields = useMemo(() => {
const walletStatus = state.wallet;
const isConnected = walletStatus.status === 'connected';
const isConnecting = walletStatus.status === 'connecting';
const isError = walletStatus.status === 'error';
let connectorId: WalletConnectorId | null = null;
let account: Address | null = null;
let sessionAccounts: SessionAccount[] = [];
let walletError: Error | null = null;
if (walletStatus.status === 'connected') {
connectorId = walletStatus.session.connectorId;
account = walletStatus.session.selectedAccount.address;
sessionAccounts = walletStatus.session.accounts;
} else if (walletStatus.status === 'connecting') {
connectorId = walletStatus.connectorId;
} else if (walletStatus.status === 'error') {
walletError = walletStatus.error;
connectorId = walletStatus.connectorId ?? null;
}
// Resolve connector metadata from connectors array
const connector = connectorId ? (state.connectors.find(c => c.id === connectorId) ?? null) : null;
return {
walletStatus,
isConnected,
isConnecting,
isError,
walletError,
account,
sessionAccounts,
connectorId,
connector,
};
}, [state.wallet, state.connectors]);
return useMemo(
() => ({
...state,
...methods,
...vNextFields,
walletConnectUri,
clearWalletConnectUri,
}),
[state, methods, vNextFields, walletConnectUri, clearWalletConnectUri],
);
}
export function useConnectorClient(): ConnectorClient | null {
return useContext(ConnectorContext);
}