@dodoex/wallet-web3-react 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/babel.config.js +9 -9
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.js +1 -1
  4. package/lingui.config.ts +13 -13
  5. package/package.json +90 -77
  6. package/rollup.config.mjs +100 -100
  7. package/src/ClientProvider.tsx +17 -15
  8. package/src/LangProvider.tsx +36 -34
  9. package/src/WalletConnect/AccountPage.tsx +496 -494
  10. package/src/WalletConnect/ActivityList.tsx +606 -604
  11. package/src/WalletConnect/ConnectAlchemy/index.tsx +248 -246
  12. package/src/WalletConnect/ConnectAlchemy/useConnectAlchemy.ts +105 -105
  13. package/src/WalletConnect/ConnectDialog.tsx +35 -33
  14. package/src/WalletConnect/ConnectLedger/ErrorDialog.tsx +61 -61
  15. package/src/WalletConnect/ConnectLedger/LockedDialog.tsx +54 -54
  16. package/src/WalletConnect/ConnectLedger/helper.ts +14 -14
  17. package/src/WalletConnect/ConnectLedger/index.tsx +2 -0
  18. package/src/WalletConnect/ConnectPage.tsx +508 -506
  19. package/src/WalletConnect/HasBalanceTokenList.tsx +202 -200
  20. package/src/WalletConnect/ReceiveTokenPage.tsx +145 -143
  21. package/src/WalletConnect/SendTokenPage.tsx +251 -249
  22. package/src/WalletConnect/WalletDialog.tsx +80 -78
  23. package/src/WalletConnectProvider.tsx +57 -55
  24. package/src/components/AddressWithLinkAndCopy.tsx +202 -200
  25. package/src/components/Dialog.tsx +158 -156
  26. package/src/components/TokenLogo.tsx +167 -165
  27. package/src/components/WalletTag.tsx +117 -115
  28. package/src/constants/localstorage.ts +24 -22
  29. package/src/hooks/useConnectWallet.ts +150 -148
  30. package/src/hooks/useFetchFiatPrice.ts +53 -51
  31. package/src/hooks/useFetchTokensBalance.ts +53 -51
  32. package/src/hooks/useHasBalanceTokenList.ts +95 -93
  33. package/src/hooks/useTransactionList.ts +89 -87
  34. package/src/index.tsx +7 -7
  35. package/src/locales/en.po +51 -51
  36. package/src/locales/zh.po +51 -51
  37. package/src/utils/formatter.ts +102 -102
  38. package/src/utils/time.ts +21 -21
  39. package/src/utils/utils.ts +8 -8
  40. package/tsconfig.json +23 -23
@@ -1,506 +1,508 @@
1
- import React from 'react';
2
- import WalletWeb3, { useWalletStore, WalletType } from '@dodoex/wallet-web3';
3
- import { useConnectWallet, WalletItem } from '../hooks/useConnectWallet';
4
- import { alpha, Box, ButtonBase, Checkbox, useTheme } from '@dodoex/components';
5
- import { useWalletConnectContext } from '../WalletConnectProvider';
6
- import WalletTag, { WalletTagProps } from '../components/WalletTag';
7
- import { ArrowRight, More } from '@dodoex/icons';
8
- import { Trans } from '@lingui/macro';
9
- import ConnectLedger from './ConnectLedger';
10
- import { checkAlchemyCache } from './ConnectAlchemy/useConnectAlchemy';
11
- import ConnectAlchemy from './ConnectAlchemy';
12
- import { DialogTitle } from '../components/Dialog';
13
-
14
- const walletGroupStyle = {
15
- display: 'grid',
16
- gap: 12,
17
- gridTemplateColumns: 'repeat(1, 1fr)',
18
- alignItems: 'start',
19
- px: 8,
20
- py: 6,
21
- };
22
-
23
- export type ConnectDialogWalletTag = React.FC<
24
- WalletTagProps & {
25
- children?: React.ReactNode;
26
- }
27
- >;
28
-
29
- function WalletGroupItem({
30
- chainId,
31
- wallet,
32
- loggedIn,
33
- matchChain,
34
- selectedChainId,
35
- disabled,
36
- activeWalletType,
37
- installed,
38
- disabledClick,
39
- onShowLedgerConnect,
40
- onShowAlchemyConnect,
41
- onConnectWallet,
42
- WalletTag: WalletTagProps,
43
- }: {
44
- chainId?: number;
45
- wallet: WalletItem;
46
- loggedIn: boolean;
47
- matchChain: boolean;
48
- selectedChainId: number | undefined;
49
- disabled?: boolean;
50
- activeWalletType?: WalletType;
51
- installed?: boolean;
52
- disabledClick: () => void;
53
- onShowLedgerConnect: () => void;
54
- onShowAlchemyConnect: () => void;
55
- onConnectWallet?: (wallet: WalletItem) => void | Promise<void>;
56
- WalletTag?: ConnectDialogWalletTag;
57
- }) {
58
- const theme = useTheme();
59
- const isChecked =
60
- loggedIn &&
61
- activeWalletType === wallet.currentType &&
62
- (matchChain || activeWalletType.toLocaleLowerCase() === 'walletconnect');
63
-
64
- const [loading, setLoading] = React.useState(false);
65
- const { connectTimeout, onConnectTimeout, switchNetwork, onConnected } =
66
- useWalletConnectContext();
67
- return (
68
- <Box
69
- component={ButtonBase}
70
- sx={{
71
- display: 'flex',
72
- justifyContent: 'space-between',
73
- alignItems: 'center',
74
- p: theme.spacing(10, 12),
75
- width: '100%',
76
- color: 'text.primary',
77
- borderRadius: 8,
78
- opacity: disabled ? 0.3 : 1,
79
- '&:hover': {
80
- backgroundColor: theme.palette.background.tag,
81
- },
82
- }}
83
- onClick={async () => {
84
- if (disabled) {
85
- disabledClick();
86
- return;
87
- }
88
- if (isChecked) {
89
- return;
90
- }
91
- if (wallet.currentType === WalletType.LedgerUSB) {
92
- if (wallet.disabled) return;
93
- onShowLedgerConnect();
94
- return;
95
- }
96
- if (wallet.currentType === WalletType.Alchemy) {
97
- if (wallet.disabled) return;
98
- const hasCache = await checkAlchemyCache(chainId ?? 1);
99
- if (!hasCache) {
100
- onShowAlchemyConnect();
101
- return;
102
- }
103
- }
104
- setLoading(true);
105
- let timeout: NodeJS.Timeout | undefined = undefined;
106
- try {
107
- await Promise.race([
108
- (async () => {
109
- if (onConnectWallet) {
110
- try {
111
- await onConnectWallet(wallet);
112
- const connectedChainId = useWalletStore.getState().chainId;
113
- if (onConnected) {
114
- onConnected(connectedChainId, wallet);
115
- }
116
- } finally {
117
- setLoading(false);
118
- }
119
- return;
120
- }
121
-
122
- if (
123
- switchNetwork &&
124
- wallet.currentType === activeWalletType &&
125
- (!matchChain || !loggedIn) &&
126
- selectedChainId
127
- ) {
128
- const res = await switchNetwork(selectedChainId);
129
- if (res !== undefined) {
130
- setLoading(false);
131
- return;
132
- }
133
- }
134
- if (wallet.onClick) {
135
- try {
136
- await wallet.onClick();
137
- const connectedChainId = useWalletStore.getState().chainId;
138
- if (onConnected) {
139
- onConnected(connectedChainId, wallet);
140
- }
141
- } finally {
142
- setLoading(false);
143
- }
144
- }
145
- })(),
146
- onConnectTimeout
147
- ? new Promise(
148
- (_, reject) =>
149
- (timeout = setTimeout(() => {
150
- onConnectTimeout();
151
- reject();
152
- }, connectTimeout ?? 15000)),
153
- )
154
- : undefined,
155
- ]);
156
- } catch (error) {
157
- setLoading(false);
158
- throw error;
159
- }
160
- if (timeout) {
161
- clearTimeout(timeout);
162
- }
163
- }}
164
- >
165
- <Box
166
- sx={{
167
- position: 'relative',
168
- display: 'flex',
169
- alignItems: 'center',
170
- gap: 12,
171
- justifyContent: 'center',
172
- typography: 'body1',
173
- fontWeight: 600,
174
- }}
175
- >
176
- <Box
177
- component="img"
178
- src={wallet.icon}
179
- sx={{
180
- width: 40,
181
- height: 40,
182
- borderRadius: 12,
183
- border: 'solid 1px',
184
- borderColor: 'border.main',
185
- }}
186
- />
187
- {wallet.title}
188
- </Box>
189
- {WalletTagProps ? (
190
- <WalletTagProps
191
- loading={loading}
192
- wallet={wallet}
193
- isChecked={isChecked}
194
- installed={!!installed}
195
- >
196
- <WalletTag
197
- loading={loading}
198
- wallet={wallet}
199
- isChecked={isChecked}
200
- installed={!!installed}
201
- />
202
- </WalletTagProps>
203
- ) : (
204
- <WalletTag
205
- loading={loading}
206
- wallet={wallet}
207
- isChecked={isChecked}
208
- installed={!!installed}
209
- />
210
- )}
211
- </Box>
212
- );
213
- }
214
-
215
- export interface ConnectPageProps {
216
- chainId: number;
217
- account?: string;
218
- walletWeb3?: WalletWeb3;
219
- showOtherInjectWallet?: boolean;
220
- onClose: () => void;
221
- onConnectWallet?: (wallet: WalletItem) => void | Promise<void>;
222
- WalletTag?: ConnectDialogWalletTag;
223
- }
224
- function ConnectPage({
225
- chainId,
226
- account,
227
- walletWeb3,
228
- showOtherInjectWallet,
229
- onClose,
230
- onConnectWallet,
231
- WalletTag,
232
- }: ConnectPageProps) {
233
- const theme = useTheme();
234
- const highlightBackgroundColor =
235
- theme.palette.mode === 'dark' ? alpha('#F1F902', 0.1) : '#F1F902';
236
- const { termsOfServiceLink, chainId: needConnectChainId } =
237
- useWalletConnectContext();
238
-
239
- const selectedChainId = needConnectChainId ?? chainId;
240
-
241
- const [isDisabledClick, setIsDisabledClick] = React.useState(false);
242
- const [showLedgerConnect, setShowLedgerConnect] = React.useState(false);
243
- const [showAlchemyConnect, setShowAlchemyConnect] = React.useState(false);
244
- const disabledClick = React.useCallback(() => {
245
- setIsDisabledClick(true);
246
- const time = setTimeout(() => {
247
- setIsDisabledClick(false);
248
- }, 1800);
249
-
250
- return () => {
251
- clearTimeout(time);
252
- };
253
- }, []);
254
-
255
- const {
256
- walletList,
257
- userReadAndChecked,
258
- handleChangeUserReadAndChecked,
259
- activeWalletType,
260
- } = useConnectWallet({
261
- walletWeb3,
262
- matchTestChain: true,
263
- showOtherInjectWallet,
264
- selectedChainId,
265
- hasTermsOfServiceLink: !!termsOfServiceLink,
266
- });
267
- const [showAllWallet, setShowAllWallet] = React.useState(false);
268
- const showWalletList = React.useMemo(() => {
269
- if (showAllWallet) return walletList;
270
- return walletList.slice(0, 6);
271
- }, [walletList, showAllWallet]);
272
- const morePriorityWalletList = React.useMemo(() => {
273
- return walletList.slice(6, 9);
274
- }, [walletList]);
275
-
276
- return (
277
- <>
278
- <DialogTitle onClose={onClose}>
279
- <Trans>Connect Wallet</Trans>
280
- </DialogTitle>
281
- <Box
282
- sx={{
283
- height: '100%',
284
- display: 'flex',
285
- flexDirection: 'column',
286
- overflow: 'hidden',
287
- pb: termsOfServiceLink ? 0 : 20,
288
- }}
289
- >
290
- <Box
291
- sx={{
292
- overflowY: 'auto',
293
- }}
294
- >
295
- <Box
296
- sx={{
297
- ...walletGroupStyle,
298
- }}
299
- >
300
- {showWalletList.map((wallet) => {
301
- return (
302
- <WalletGroupItem
303
- key={wallet.title}
304
- wallet={wallet}
305
- disabled={!userReadAndChecked}
306
- loggedIn={!!account}
307
- selectedChainId={selectedChainId}
308
- matchChain={selectedChainId === chainId}
309
- activeWalletType={activeWalletType}
310
- installed={wallet.isInstalled}
311
- disabledClick={disabledClick}
312
- onShowLedgerConnect={() => setShowLedgerConnect(true)}
313
- onShowAlchemyConnect={() => setShowAlchemyConnect(true)}
314
- onConnectWallet={onConnectWallet}
315
- WalletTag={WalletTag}
316
- />
317
- );
318
- })}
319
- </Box>
320
- {morePriorityWalletList.length ? (
321
- <Box
322
- sx={{
323
- mx: 20,
324
- display: 'flex',
325
- justifyContent: 'center',
326
- position: 'relative',
327
- opacity: userReadAndChecked ? 1 : 0.5,
328
- }}
329
- >
330
- <Box
331
- sx={{
332
- position: 'absolute',
333
- top: '50%',
334
- right: 0,
335
- left: 0,
336
- height: '1px',
337
- backgroundColor: 'border.main',
338
- transform: 'translateY(-50%)',
339
- }}
340
- />
341
- {showAllWallet ? (
342
- <Box
343
- component={ButtonBase}
344
- disabled={!userReadAndChecked}
345
- sx={{
346
- position: 'relative',
347
- padding: theme.spacing(0, 12),
348
- height: 34,
349
- borderRadius: 20,
350
- backgroundColor: 'background.paper',
351
- border: 'solid 1px',
352
- borderColor: 'border.main',
353
- '&:hover': {
354
- backgroundColor: 'background.paperContrast',
355
- },
356
- }}
357
- onClick={() => setShowAllWallet(false)}
358
- >
359
- <Box
360
- component={ArrowRight}
361
- sx={{
362
- color: 'text.secondary',
363
- transform: 'rotate(-90deg)',
364
- }}
365
- />
366
- </Box>
367
- ) : (
368
- <Box
369
- component={ButtonBase}
370
- disabled={!userReadAndChecked}
371
- sx={{
372
- position: 'relative',
373
- display: 'flex',
374
- alignItems: 'center',
375
- padding: theme.spacing(0, 12),
376
- height: 34,
377
- borderRadius: 20,
378
- border: 'solid 1px',
379
- borderColor: 'border.main',
380
- backgroundColor: 'background.paper',
381
- typography: 'h6',
382
- color: 'text.secondary',
383
- '&:hover': {
384
- backgroundColor: 'background.paperContrast',
385
- },
386
- }}
387
- onClick={() => setShowAllWallet(true)}
388
- >
389
- <Trans>More Wallets</Trans>
390
- {morePriorityWalletList.map((item, index) => {
391
- return (
392
- <Box
393
- key={item.title}
394
- component="img"
395
- src={item.icon}
396
- alt="icon"
397
- sx={{
398
- width: 18,
399
- height: 18,
400
- ...(index !== 0
401
- ? {
402
- position: 'relative',
403
- left: -2,
404
- }
405
- : {
406
- ml: 12,
407
- }),
408
- }}
409
- />
410
- );
411
- })}
412
- <Box
413
- sx={{
414
- width: 18,
415
- height: 18,
416
- display: 'flex',
417
- justifyContent: 'center',
418
- alignItems: 'center',
419
- backgroundColor: 'background.paperContrast',
420
- borderRadius: '50%',
421
- }}
422
- >
423
- <Box
424
- component={More}
425
- sx={{
426
- width: 18,
427
- height: 18,
428
- }}
429
- />
430
- </Box>
431
- <Box
432
- component={ArrowRight}
433
- sx={{
434
- transform: 'rotate(90deg)',
435
- }}
436
- />
437
- </Box>
438
- )}
439
- </Box>
440
- ) : (
441
- ''
442
- )}
443
- <ConnectLedger
444
- on={showLedgerConnect}
445
- chainId={selectedChainId}
446
- onClose={() => setShowLedgerConnect(false)}
447
- onConnect={(path, account) => {
448
- walletList
449
- .find((wallet) => wallet.currentType === WalletType.LedgerUSB)
450
- ?.onClick({
451
- ledgerParams: {
452
- path,
453
- account,
454
- },
455
- });
456
- }}
457
- />
458
- {!!walletWeb3 && (
459
- <ConnectAlchemy
460
- open={showAlchemyConnect}
461
- setOpen={setShowAlchemyConnect}
462
- walletWeb3={walletWeb3}
463
- />
464
- )}
465
- </Box>
466
- {!!termsOfServiceLink && (
467
- <Checkbox
468
- checked={userReadAndChecked}
469
- onChange={(evt: any) =>
470
- handleChangeUserReadAndChecked(evt.target.checked)
471
- }
472
- label={
473
- <Box
474
- sx={{
475
- typography: 'body2',
476
- '& a': {
477
- fontWeight: 600,
478
- color: 'primary.main',
479
- // textDecoration: 'underline',
480
- },
481
- }}
482
- >
483
- {termsOfServiceLink}
484
- </Box>
485
- }
486
- sx={{
487
- m: 0,
488
- p: 20,
489
- alignItems: 'flex-start',
490
- display: 'flex',
491
- backgroundColor: isDisabledClick
492
- ? highlightBackgroundColor
493
- : 'transparent',
494
- '& > span': {
495
- p: 0,
496
- mr: 8,
497
- },
498
- }}
499
- />
500
- )}
501
- </Box>
502
- </>
503
- );
504
- }
505
-
506
- export default ConnectPage;
1
+ 'use client';
2
+
3
+ import React from 'react';
4
+ import WalletWeb3, { useWalletStore, WalletType } from '@dodoex/wallet-web3';
5
+ import { useConnectWallet, WalletItem } from '../hooks/useConnectWallet';
6
+ import { alpha, Box, ButtonBase, Checkbox, useTheme } from '@dodoex/components';
7
+ import { useWalletConnectContext } from '../WalletConnectProvider';
8
+ import WalletTag, { WalletTagProps } from '../components/WalletTag';
9
+ import { ArrowRight, More } from '@dodoex/icons';
10
+ import { Trans } from '@lingui/macro';
11
+ import ConnectLedger from './ConnectLedger';
12
+ import { checkAlchemyCache } from './ConnectAlchemy/useConnectAlchemy';
13
+ import ConnectAlchemy from './ConnectAlchemy';
14
+ import { DialogTitle } from '../components/Dialog';
15
+
16
+ const walletGroupStyle = {
17
+ display: 'grid',
18
+ gap: 12,
19
+ gridTemplateColumns: 'repeat(1, 1fr)',
20
+ alignItems: 'start',
21
+ px: 8,
22
+ py: 6,
23
+ };
24
+
25
+ export type ConnectDialogWalletTag = React.FC<
26
+ WalletTagProps & {
27
+ children?: React.ReactNode;
28
+ }
29
+ >;
30
+
31
+ function WalletGroupItem({
32
+ chainId,
33
+ wallet,
34
+ loggedIn,
35
+ matchChain,
36
+ selectedChainId,
37
+ disabled,
38
+ activeWalletType,
39
+ installed,
40
+ disabledClick,
41
+ onShowLedgerConnect,
42
+ onShowAlchemyConnect,
43
+ onConnectWallet,
44
+ WalletTag: WalletTagProps,
45
+ }: {
46
+ chainId?: number;
47
+ wallet: WalletItem;
48
+ loggedIn: boolean;
49
+ matchChain: boolean;
50
+ selectedChainId: number | undefined;
51
+ disabled?: boolean;
52
+ activeWalletType?: WalletType;
53
+ installed?: boolean;
54
+ disabledClick: () => void;
55
+ onShowLedgerConnect: () => void;
56
+ onShowAlchemyConnect: () => void;
57
+ onConnectWallet?: (wallet: WalletItem) => void | Promise<void>;
58
+ WalletTag?: ConnectDialogWalletTag;
59
+ }) {
60
+ const theme = useTheme();
61
+ const isChecked =
62
+ loggedIn &&
63
+ activeWalletType === wallet.currentType &&
64
+ (matchChain || activeWalletType.toLocaleLowerCase() === 'walletconnect');
65
+
66
+ const [loading, setLoading] = React.useState(false);
67
+ const { connectTimeout, onConnectTimeout, switchNetwork, onConnected } =
68
+ useWalletConnectContext();
69
+ return (
70
+ <Box
71
+ component={ButtonBase}
72
+ sx={{
73
+ display: 'flex',
74
+ justifyContent: 'space-between',
75
+ alignItems: 'center',
76
+ p: theme.spacing(10, 12),
77
+ width: '100%',
78
+ color: 'text.primary',
79
+ borderRadius: 8,
80
+ opacity: disabled ? 0.3 : 1,
81
+ '&:hover': {
82
+ backgroundColor: theme.palette.background.tag,
83
+ },
84
+ }}
85
+ onClick={async () => {
86
+ if (disabled) {
87
+ disabledClick();
88
+ return;
89
+ }
90
+ if (isChecked) {
91
+ return;
92
+ }
93
+ if (wallet.currentType === WalletType.LedgerUSB) {
94
+ if (wallet.disabled) return;
95
+ onShowLedgerConnect();
96
+ return;
97
+ }
98
+ if (wallet.currentType === WalletType.Alchemy) {
99
+ if (wallet.disabled) return;
100
+ const hasCache = await checkAlchemyCache(chainId ?? 1);
101
+ if (!hasCache) {
102
+ onShowAlchemyConnect();
103
+ return;
104
+ }
105
+ }
106
+ setLoading(true);
107
+ let timeout: NodeJS.Timeout | undefined = undefined;
108
+ try {
109
+ await Promise.race([
110
+ (async () => {
111
+ if (onConnectWallet) {
112
+ try {
113
+ await onConnectWallet(wallet);
114
+ const connectedChainId = useWalletStore.getState().chainId;
115
+ if (onConnected) {
116
+ onConnected(connectedChainId, wallet);
117
+ }
118
+ } finally {
119
+ setLoading(false);
120
+ }
121
+ return;
122
+ }
123
+
124
+ if (
125
+ switchNetwork &&
126
+ wallet.currentType === activeWalletType &&
127
+ (!matchChain || !loggedIn) &&
128
+ selectedChainId
129
+ ) {
130
+ const res = await switchNetwork(selectedChainId);
131
+ if (res !== undefined) {
132
+ setLoading(false);
133
+ return;
134
+ }
135
+ }
136
+ if (wallet.onClick) {
137
+ try {
138
+ await wallet.onClick();
139
+ const connectedChainId = useWalletStore.getState().chainId;
140
+ if (onConnected) {
141
+ onConnected(connectedChainId, wallet);
142
+ }
143
+ } finally {
144
+ setLoading(false);
145
+ }
146
+ }
147
+ })(),
148
+ onConnectTimeout
149
+ ? new Promise(
150
+ (_, reject) =>
151
+ (timeout = setTimeout(() => {
152
+ onConnectTimeout();
153
+ reject();
154
+ }, connectTimeout ?? 15000)),
155
+ )
156
+ : undefined,
157
+ ]);
158
+ } catch (error) {
159
+ setLoading(false);
160
+ throw error;
161
+ }
162
+ if (timeout) {
163
+ clearTimeout(timeout);
164
+ }
165
+ }}
166
+ >
167
+ <Box
168
+ sx={{
169
+ position: 'relative',
170
+ display: 'flex',
171
+ alignItems: 'center',
172
+ gap: 12,
173
+ justifyContent: 'center',
174
+ typography: 'body1',
175
+ fontWeight: 600,
176
+ }}
177
+ >
178
+ <Box
179
+ component="img"
180
+ src={wallet.icon}
181
+ sx={{
182
+ width: 40,
183
+ height: 40,
184
+ borderRadius: 12,
185
+ border: 'solid 1px',
186
+ borderColor: 'border.main',
187
+ }}
188
+ />
189
+ {wallet.title}
190
+ </Box>
191
+ {WalletTagProps ? (
192
+ <WalletTagProps
193
+ loading={loading}
194
+ wallet={wallet}
195
+ isChecked={isChecked}
196
+ installed={!!installed}
197
+ >
198
+ <WalletTag
199
+ loading={loading}
200
+ wallet={wallet}
201
+ isChecked={isChecked}
202
+ installed={!!installed}
203
+ />
204
+ </WalletTagProps>
205
+ ) : (
206
+ <WalletTag
207
+ loading={loading}
208
+ wallet={wallet}
209
+ isChecked={isChecked}
210
+ installed={!!installed}
211
+ />
212
+ )}
213
+ </Box>
214
+ );
215
+ }
216
+
217
+ export interface ConnectPageProps {
218
+ chainId: number;
219
+ account?: string;
220
+ walletWeb3?: WalletWeb3;
221
+ showOtherInjectWallet?: boolean;
222
+ onClose: () => void;
223
+ onConnectWallet?: (wallet: WalletItem) => void | Promise<void>;
224
+ WalletTag?: ConnectDialogWalletTag;
225
+ }
226
+ function ConnectPage({
227
+ chainId,
228
+ account,
229
+ walletWeb3,
230
+ showOtherInjectWallet,
231
+ onClose,
232
+ onConnectWallet,
233
+ WalletTag,
234
+ }: ConnectPageProps) {
235
+ const theme = useTheme();
236
+ const highlightBackgroundColor =
237
+ theme.palette.mode === 'dark' ? alpha('#F1F902', 0.1) : '#F1F902';
238
+ const { termsOfServiceLink, chainId: needConnectChainId } =
239
+ useWalletConnectContext();
240
+
241
+ const selectedChainId = needConnectChainId ?? chainId;
242
+
243
+ const [isDisabledClick, setIsDisabledClick] = React.useState(false);
244
+ const [showLedgerConnect, setShowLedgerConnect] = React.useState(false);
245
+ const [showAlchemyConnect, setShowAlchemyConnect] = React.useState(false);
246
+ const disabledClick = React.useCallback(() => {
247
+ setIsDisabledClick(true);
248
+ const time = setTimeout(() => {
249
+ setIsDisabledClick(false);
250
+ }, 1800);
251
+
252
+ return () => {
253
+ clearTimeout(time);
254
+ };
255
+ }, []);
256
+
257
+ const {
258
+ walletList,
259
+ userReadAndChecked,
260
+ handleChangeUserReadAndChecked,
261
+ activeWalletType,
262
+ } = useConnectWallet({
263
+ walletWeb3,
264
+ matchTestChain: true,
265
+ showOtherInjectWallet,
266
+ selectedChainId,
267
+ hasTermsOfServiceLink: !!termsOfServiceLink,
268
+ });
269
+ const [showAllWallet, setShowAllWallet] = React.useState(false);
270
+ const showWalletList = React.useMemo(() => {
271
+ if (showAllWallet) return walletList;
272
+ return walletList.slice(0, 6);
273
+ }, [walletList, showAllWallet]);
274
+ const morePriorityWalletList = React.useMemo(() => {
275
+ return walletList.slice(6, 9);
276
+ }, [walletList]);
277
+
278
+ return (
279
+ <>
280
+ <DialogTitle onClose={onClose}>
281
+ <Trans>Connect Wallet</Trans>
282
+ </DialogTitle>
283
+ <Box
284
+ sx={{
285
+ height: '100%',
286
+ display: 'flex',
287
+ flexDirection: 'column',
288
+ overflow: 'hidden',
289
+ pb: termsOfServiceLink ? 0 : 20,
290
+ }}
291
+ >
292
+ <Box
293
+ sx={{
294
+ overflowY: 'auto',
295
+ }}
296
+ >
297
+ <Box
298
+ sx={{
299
+ ...walletGroupStyle,
300
+ }}
301
+ >
302
+ {showWalletList.map((wallet) => {
303
+ return (
304
+ <WalletGroupItem
305
+ key={wallet.title}
306
+ wallet={wallet}
307
+ disabled={!userReadAndChecked}
308
+ loggedIn={!!account}
309
+ selectedChainId={selectedChainId}
310
+ matchChain={selectedChainId === chainId}
311
+ activeWalletType={activeWalletType}
312
+ installed={wallet.isInstalled}
313
+ disabledClick={disabledClick}
314
+ onShowLedgerConnect={() => setShowLedgerConnect(true)}
315
+ onShowAlchemyConnect={() => setShowAlchemyConnect(true)}
316
+ onConnectWallet={onConnectWallet}
317
+ WalletTag={WalletTag}
318
+ />
319
+ );
320
+ })}
321
+ </Box>
322
+ {morePriorityWalletList.length ? (
323
+ <Box
324
+ sx={{
325
+ mx: 20,
326
+ display: 'flex',
327
+ justifyContent: 'center',
328
+ position: 'relative',
329
+ opacity: userReadAndChecked ? 1 : 0.5,
330
+ }}
331
+ >
332
+ <Box
333
+ sx={{
334
+ position: 'absolute',
335
+ top: '50%',
336
+ right: 0,
337
+ left: 0,
338
+ height: '1px',
339
+ backgroundColor: 'border.main',
340
+ transform: 'translateY(-50%)',
341
+ }}
342
+ />
343
+ {showAllWallet ? (
344
+ <Box
345
+ component={ButtonBase}
346
+ disabled={!userReadAndChecked}
347
+ sx={{
348
+ position: 'relative',
349
+ padding: theme.spacing(0, 12),
350
+ height: 34,
351
+ borderRadius: 20,
352
+ backgroundColor: 'background.paper',
353
+ border: 'solid 1px',
354
+ borderColor: 'border.main',
355
+ '&:hover': {
356
+ backgroundColor: 'background.paperContrast',
357
+ },
358
+ }}
359
+ onClick={() => setShowAllWallet(false)}
360
+ >
361
+ <Box
362
+ component={ArrowRight}
363
+ sx={{
364
+ color: 'text.secondary',
365
+ transform: 'rotate(-90deg)',
366
+ }}
367
+ />
368
+ </Box>
369
+ ) : (
370
+ <Box
371
+ component={ButtonBase}
372
+ disabled={!userReadAndChecked}
373
+ sx={{
374
+ position: 'relative',
375
+ display: 'flex',
376
+ alignItems: 'center',
377
+ padding: theme.spacing(0, 12),
378
+ height: 34,
379
+ borderRadius: 20,
380
+ border: 'solid 1px',
381
+ borderColor: 'border.main',
382
+ backgroundColor: 'background.paper',
383
+ typography: 'h6',
384
+ color: 'text.secondary',
385
+ '&:hover': {
386
+ backgroundColor: 'background.paperContrast',
387
+ },
388
+ }}
389
+ onClick={() => setShowAllWallet(true)}
390
+ >
391
+ <Trans>More Wallets</Trans>
392
+ {morePriorityWalletList.map((item, index) => {
393
+ return (
394
+ <Box
395
+ key={item.title}
396
+ component="img"
397
+ src={item.icon}
398
+ alt="icon"
399
+ sx={{
400
+ width: 18,
401
+ height: 18,
402
+ ...(index !== 0
403
+ ? {
404
+ position: 'relative',
405
+ left: -2,
406
+ }
407
+ : {
408
+ ml: 12,
409
+ }),
410
+ }}
411
+ />
412
+ );
413
+ })}
414
+ <Box
415
+ sx={{
416
+ width: 18,
417
+ height: 18,
418
+ display: 'flex',
419
+ justifyContent: 'center',
420
+ alignItems: 'center',
421
+ backgroundColor: 'background.paperContrast',
422
+ borderRadius: '50%',
423
+ }}
424
+ >
425
+ <Box
426
+ component={More}
427
+ sx={{
428
+ width: 18,
429
+ height: 18,
430
+ }}
431
+ />
432
+ </Box>
433
+ <Box
434
+ component={ArrowRight}
435
+ sx={{
436
+ transform: 'rotate(90deg)',
437
+ }}
438
+ />
439
+ </Box>
440
+ )}
441
+ </Box>
442
+ ) : (
443
+ ''
444
+ )}
445
+ <ConnectLedger
446
+ on={showLedgerConnect}
447
+ chainId={selectedChainId}
448
+ onClose={() => setShowLedgerConnect(false)}
449
+ onConnect={(path, account) => {
450
+ walletList
451
+ .find((wallet) => wallet.currentType === WalletType.LedgerUSB)
452
+ ?.onClick({
453
+ ledgerParams: {
454
+ path,
455
+ account,
456
+ },
457
+ });
458
+ }}
459
+ />
460
+ {!!walletWeb3 && (
461
+ <ConnectAlchemy
462
+ open={showAlchemyConnect}
463
+ setOpen={setShowAlchemyConnect}
464
+ walletWeb3={walletWeb3}
465
+ />
466
+ )}
467
+ </Box>
468
+ {!!termsOfServiceLink && (
469
+ <Checkbox
470
+ checked={userReadAndChecked}
471
+ onChange={(evt: any) =>
472
+ handleChangeUserReadAndChecked(evt.target.checked)
473
+ }
474
+ label={
475
+ <Box
476
+ sx={{
477
+ typography: 'body2',
478
+ '& a': {
479
+ fontWeight: 600,
480
+ color: 'primary.main',
481
+ // textDecoration: 'underline',
482
+ },
483
+ }}
484
+ >
485
+ {termsOfServiceLink}
486
+ </Box>
487
+ }
488
+ sx={{
489
+ m: 0,
490
+ p: 20,
491
+ alignItems: 'flex-start',
492
+ display: 'flex',
493
+ backgroundColor: isDisabledClick
494
+ ? highlightBackgroundColor
495
+ : 'transparent',
496
+ '& > span': {
497
+ p: 0,
498
+ mr: 8,
499
+ },
500
+ }}
501
+ />
502
+ )}
503
+ </Box>
504
+ </>
505
+ );
506
+ }
507
+
508
+ export default ConnectPage;