forked from MettaChain/PropChain-FrontEnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWalletConnector.ts
More file actions
130 lines (109 loc) · 3.91 KB
/
Copy pathuseWalletConnector.ts
File metadata and controls
130 lines (109 loc) · 3.91 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
import { useCallback, useState } from 'react';
import { logger } from '@/utils/logger';
import { getFriendlyWeb3ErrorMessage } from '@/utils/errorHandling';
export type SupportedWalletId = 'metamask' | 'walletconnect' | 'coinbase';
export interface ConnectorResult {
address: string;
chainId: number;
}
/**
* Hook for lazy-loading wallet connectors on demand
* Dynamically imports connector modules only when a user initiates connection
*/
export const useWalletConnector = () => {
const [isLoadingConnector, setIsLoadingConnector] = useState(false);
const [connectorError, setConnectorError] = useState<string | null>(null);
/**
* Dynamically loads and executes the MetaMask connector
*/
const connectMetaMask = useCallback(async (): Promise<ConnectorResult> => {
try {
setIsLoadingConnector(true);
setConnectorError(null);
logger.debug('Lazy-loading MetaMask connector...');
// Dynamic import - loaded only when this function is called
const { connectMetaMaskWallet } = await import('@/lib/walletConnectors/metamask');
const result = await connectMetaMaskWallet();
logger.debug('MetaMask connector loaded and executed successfully');
return result;
} catch (error) {
const message = getFriendlyWeb3ErrorMessage(error);
setConnectorError(message);
logger.error('MetaMask connector error:', error);
throw error;
} finally {
setIsLoadingConnector(false);
}
}, []);
/**
* Dynamically loads and executes the Coinbase Wallet connector
*/
const connectCoinbase = useCallback(async (): Promise<ConnectorResult> => {
try {
setIsLoadingConnector(true);
setConnectorError(null);
logger.debug('Lazy-loading Coinbase connector...');
// Dynamic import - loaded only when this function is called
const { connectCoinbaseWallet } = await import('@/lib/walletConnectors/coinbase');
const result = await connectCoinbaseWallet();
logger.debug('Coinbase connector loaded and executed successfully');
return result;
} catch (error) {
const message = getFriendlyWeb3ErrorMessage(error);
setConnectorError(message);
logger.error('Coinbase connector error:', error);
throw error;
} finally {
setIsLoadingConnector(false);
}
}, []);
/**
* Dynamically loads and executes the WalletConnect connector
*/
const connectWalletConnect = useCallback(async (): Promise<ConnectorResult> => {
try {
setIsLoadingConnector(true);
setConnectorError(null);
logger.debug('Lazy-loading WalletConnect connector...');
// Dynamic import - loaded only when this function is called
const { connectWalletConnectWallet } = await import('@/lib/walletConnectors/walletconnect');
const result = await connectWalletConnectWallet();
logger.debug('WalletConnect connector loaded and executed successfully');
return result;
} catch (error) {
const message = getFriendlyWeb3ErrorMessage(error);
setConnectorError(message);
logger.error('WalletConnect connector error:', error);
throw error;
} finally {
setIsLoadingConnector(false);
}
}, []);
/**
* Unifies connector selection and lazy-loading
*/
const connectWallet = useCallback(async (walletId: SupportedWalletId): Promise<ConnectorResult> => {
switch (walletId) {
case 'metamask':
return connectMetaMask();
case 'coinbase':
return connectCoinbase();
case 'walletconnect':
return connectWalletConnect();
default:
throw new Error(`Unsupported wallet: ${walletId}`);
}
}, [connectMetaMask, connectCoinbase, connectWalletConnect]);
const clearError = useCallback(() => {
setConnectorError(null);
}, []);
return {
connectWallet,
connectMetaMask,
connectCoinbase,
connectWalletConnect,
isLoadingConnector,
connectorError,
clearError,
};
};