@rango-dev/widget-embedded 0.32.0 → 0.32.2-next.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 (130) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/components/ConfirmWalletsModal/WalletList.d.ts.map +1 -1
  3. package/dist/components/Slippage/Slippage.d.ts.map +1 -1
  4. package/dist/components/StatefulConnectModal/StatefulConnectModal.d.ts +11 -0
  5. package/dist/components/StatefulConnectModal/StatefulConnectModal.d.ts.map +1 -0
  6. package/dist/components/StatefulConnectModal/helpers.d.ts +10 -0
  7. package/dist/components/StatefulConnectModal/helpers.d.ts.map +1 -0
  8. package/dist/components/StatefulConnectModal/index.d.ts +2 -0
  9. package/dist/components/StatefulConnectModal/index.d.ts.map +1 -0
  10. package/dist/components/SwapDetailsModal/SwapDetailsModal.WalletState.d.ts.map +1 -1
  11. package/dist/components/WalletStatefulConnect/ConnectStatus.d.ts +4 -0
  12. package/dist/components/WalletStatefulConnect/ConnectStatus.d.ts.map +1 -0
  13. package/dist/components/{WalletModal/WalletModalContent.styles.d.ts → WalletStatefulConnect/ConnectStatus.styles.d.ts} +1 -1
  14. package/dist/components/{WalletModal/WalletModalContent.styles.d.ts.map → WalletStatefulConnect/ConnectStatus.styles.d.ts.map} +1 -1
  15. package/dist/components/WalletStatefulConnect/ConnectStatus.types.d.ts +8 -0
  16. package/dist/components/WalletStatefulConnect/ConnectStatus.types.d.ts.map +1 -0
  17. package/dist/components/WalletStatefulConnect/DerivationPath.d.ts +4 -0
  18. package/dist/components/WalletStatefulConnect/DerivationPath.d.ts.map +1 -0
  19. package/dist/components/WalletStatefulConnect/DerivationPath.helpers.d.ts +4 -0
  20. package/dist/components/WalletStatefulConnect/DerivationPath.helpers.d.ts.map +1 -0
  21. package/dist/components/{WalletDerivationPathModal/WalletDerivationPathModal.styles.d.ts → WalletStatefulConnect/DerivationPath.styles.d.ts} +1 -1
  22. package/dist/components/WalletStatefulConnect/DerivationPath.styles.d.ts.map +1 -0
  23. package/dist/components/WalletStatefulConnect/DerivationPath.types.d.ts +6 -0
  24. package/dist/components/WalletStatefulConnect/DerivationPath.types.d.ts.map +1 -0
  25. package/dist/components/WalletStatefulConnect/ExperimentalChain.d.ts +8 -0
  26. package/dist/components/WalletStatefulConnect/ExperimentalChain.d.ts.map +1 -0
  27. package/dist/components/WalletStatefulConnect/ExperimentalChainStatus.d.ts +4 -0
  28. package/dist/components/WalletStatefulConnect/ExperimentalChainStatus.d.ts.map +1 -0
  29. package/dist/components/WalletStatefulConnect/ExperimentalChainStatus.helpers.d.ts +8 -0
  30. package/dist/components/WalletStatefulConnect/ExperimentalChainStatus.helpers.d.ts.map +1 -0
  31. package/dist/components/WalletStatefulConnect/ExperimentalChainStatus.types.d.ts +6 -0
  32. package/dist/components/WalletStatefulConnect/ExperimentalChainStatus.types.d.ts.map +1 -0
  33. package/dist/components/WalletStatefulConnect/Namespaces.d.ts +4 -0
  34. package/dist/components/WalletStatefulConnect/Namespaces.d.ts.map +1 -0
  35. package/dist/components/WalletStatefulConnect/Namespaces.helpers.d.ts +3 -0
  36. package/dist/components/WalletStatefulConnect/Namespaces.helpers.d.ts.map +1 -0
  37. package/dist/components/{WalletNamespacesModal/WalletNamespacesModal.styles.d.ts → WalletStatefulConnect/Namespaces.styles.d.ts} +1 -1
  38. package/dist/components/WalletStatefulConnect/Namespaces.styles.d.ts.map +1 -0
  39. package/dist/components/WalletStatefulConnect/Namespaces.types.d.ts +7 -0
  40. package/dist/components/WalletStatefulConnect/Namespaces.types.d.ts.map +1 -0
  41. package/dist/components/WalletStatefulConnect/index.d.ts +5 -0
  42. package/dist/components/WalletStatefulConnect/index.d.ts.map +1 -0
  43. package/dist/containers/Wallets/Wallets.types.d.ts +1 -1
  44. package/dist/containers/Wallets/Wallets.types.d.ts.map +1 -1
  45. package/dist/containers/WidgetProvider/WidgetProvider.types.d.ts +1 -1
  46. package/dist/containers/WidgetProvider/WidgetProvider.types.d.ts.map +1 -1
  47. package/dist/hooks/useStatefulConnect/index.d.ts +5 -0
  48. package/dist/hooks/useStatefulConnect/index.d.ts.map +1 -0
  49. package/dist/hooks/useStatefulConnect/useStatefulConnect.d.ts +25 -0
  50. package/dist/hooks/useStatefulConnect/useStatefulConnect.d.ts.map +1 -0
  51. package/dist/hooks/useStatefulConnect/useStatefulConnect.helpers.d.ts +8 -0
  52. package/dist/hooks/useStatefulConnect/useStatefulConnect.helpers.d.ts.map +1 -0
  53. package/dist/hooks/useStatefulConnect/useStatefulConnect.state.d.ts +9 -0
  54. package/dist/hooks/useStatefulConnect/useStatefulConnect.state.d.ts.map +1 -0
  55. package/dist/hooks/useStatefulConnect/useStatefulConnect.types.d.ts +43 -0
  56. package/dist/hooks/useStatefulConnect/useStatefulConnect.types.d.ts.map +1 -0
  57. package/dist/hooks/useWalletList.d.ts +11 -14
  58. package/dist/hooks/useWalletList.d.ts.map +1 -1
  59. package/dist/index.d.ts +5 -3
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +2 -2
  62. package/dist/index.js.map +4 -4
  63. package/dist/pages/WalletsPage.d.ts +0 -2
  64. package/dist/pages/WalletsPage.d.ts.map +1 -1
  65. package/dist/widget-embedded.build.json +1 -1
  66. package/package.json +8 -7
  67. package/src/components/ConfirmWalletsModal/WalletList.tsx +52 -243
  68. package/src/components/HeaderButtons/WalletButton.tsx +1 -1
  69. package/src/components/Slippage/Slippage.tsx +3 -0
  70. package/src/components/StatefulConnectModal/StatefulConnectModal.tsx +168 -0
  71. package/src/components/StatefulConnectModal/helpers.ts +23 -0
  72. package/src/components/StatefulConnectModal/index.ts +1 -0
  73. package/src/components/SwapDetailsModal/SwapDetailsModal.WalletState.tsx +23 -113
  74. package/src/components/{WalletModal/WalletModalContent.tsx → WalletStatefulConnect/ConnectStatus.tsx} +13 -5
  75. package/src/components/WalletStatefulConnect/ConnectStatus.types.ts +11 -0
  76. package/src/components/WalletStatefulConnect/DerivationPath.helpers.ts +23 -0
  77. package/src/components/{WalletDerivationPathModal/WalletDerivationPathModal.tsx → WalletStatefulConnect/DerivationPath.tsx} +18 -36
  78. package/src/components/WalletStatefulConnect/DerivationPath.types.ts +6 -0
  79. package/src/components/WalletStatefulConnect/ExperimentalChain.tsx +36 -0
  80. package/src/components/WalletStatefulConnect/ExperimentalChainStatus.helpers.ts +53 -0
  81. package/src/components/WalletStatefulConnect/ExperimentalChainStatus.tsx +35 -0
  82. package/src/components/WalletStatefulConnect/ExperimentalChainStatus.types.ts +5 -0
  83. package/src/components/WalletStatefulConnect/Namespaces.helpers.ts +9 -0
  84. package/src/components/{WalletNamespacesModal/WalletNamespacesModal.tsx → WalletStatefulConnect/Namespaces.tsx} +9 -27
  85. package/src/components/{WalletNamespacesModal/WalletNamespacesModal.types.tsx → WalletStatefulConnect/Namespaces.types.tsx} +2 -5
  86. package/src/components/WalletStatefulConnect/index.ts +4 -0
  87. package/src/containers/Wallets/Wallets.tsx +1 -1
  88. package/src/containers/Wallets/Wallets.types.ts +1 -1
  89. package/src/containers/WidgetProvider/WidgetProvider.types.ts +1 -1
  90. package/src/hooks/useStatefulConnect/index.ts +9 -0
  91. package/src/hooks/useStatefulConnect/useStatefulConnect.helpers.ts +13 -0
  92. package/src/hooks/useStatefulConnect/useStatefulConnect.state.ts +45 -0
  93. package/src/hooks/useStatefulConnect/useStatefulConnect.ts +232 -0
  94. package/src/hooks/useStatefulConnect/useStatefulConnect.types.ts +56 -0
  95. package/src/hooks/useWalletList.ts +22 -53
  96. package/src/index.ts +7 -8
  97. package/src/pages/WalletsPage.tsx +10 -162
  98. package/src/store/wallets.ts +1 -1
  99. package/src/utils/wallets.ts +1 -1
  100. package/dist/components/WalletDerivationPathModal/WalletDerivationPathModal.d.ts +0 -4
  101. package/dist/components/WalletDerivationPathModal/WalletDerivationPathModal.d.ts.map +0 -1
  102. package/dist/components/WalletDerivationPathModal/WalletDerivationPathModal.styles.d.ts.map +0 -1
  103. package/dist/components/WalletDerivationPathModal/WalletDerivationPathModal.types.d.ts +0 -10
  104. package/dist/components/WalletDerivationPathModal/WalletDerivationPathModal.types.d.ts.map +0 -1
  105. package/dist/components/WalletDerivationPathModal/index.d.ts +0 -2
  106. package/dist/components/WalletDerivationPathModal/index.d.ts.map +0 -1
  107. package/dist/components/WalletModal/WalletModal.d.ts +0 -4
  108. package/dist/components/WalletModal/WalletModal.d.ts.map +0 -1
  109. package/dist/components/WalletModal/WalletModal.types.d.ts +0 -10
  110. package/dist/components/WalletModal/WalletModal.types.d.ts.map +0 -1
  111. package/dist/components/WalletModal/WalletModalContent.d.ts +0 -4
  112. package/dist/components/WalletModal/WalletModalContent.d.ts.map +0 -1
  113. package/dist/components/WalletModal/index.d.ts +0 -2
  114. package/dist/components/WalletModal/index.d.ts.map +0 -1
  115. package/dist/components/WalletNamespacesModal/WalletNamespacesModal.d.ts +0 -4
  116. package/dist/components/WalletNamespacesModal/WalletNamespacesModal.d.ts.map +0 -1
  117. package/dist/components/WalletNamespacesModal/WalletNamespacesModal.styles.d.ts.map +0 -1
  118. package/dist/components/WalletNamespacesModal/WalletNamespacesModal.types.d.ts +0 -10
  119. package/dist/components/WalletNamespacesModal/WalletNamespacesModal.types.d.ts.map +0 -1
  120. package/dist/components/WalletNamespacesModal/index.d.ts +0 -2
  121. package/dist/components/WalletNamespacesModal/index.d.ts.map +0 -1
  122. package/src/components/WalletDerivationPathModal/WalletDerivationPathModal.types.tsx +0 -10
  123. package/src/components/WalletDerivationPathModal/index.ts +0 -1
  124. package/src/components/WalletModal/WalletModal.tsx +0 -25
  125. package/src/components/WalletModal/WalletModal.types.ts +0 -11
  126. package/src/components/WalletModal/index.ts +0 -1
  127. package/src/components/WalletNamespacesModal/index.ts +0 -1
  128. /package/src/components/{WalletModal/WalletModalContent.styles.ts → WalletStatefulConnect/ConnectStatus.styles.ts} +0 -0
  129. /package/src/components/{WalletDerivationPathModal/WalletDerivationPathModal.styles.ts → WalletStatefulConnect/DerivationPath.styles.ts} +0 -0
  130. /package/src/components/{WalletNamespacesModal/WalletNamespacesModal.styles.ts → WalletStatefulConnect/Namespaces.styles.ts} +0 -0
@@ -0,0 +1,45 @@
1
+ import type {
2
+ Actions,
3
+ NeedsDerivationPathState,
4
+ NeedsNamespacesState,
5
+ } from './useStatefulConnect.types';
6
+
7
+ export interface State {
8
+ status: 'init' | 'namespace' | 'derivationPath';
9
+ namespace: NeedsNamespacesState | null;
10
+ derivationPath: NeedsDerivationPathState | null;
11
+ }
12
+
13
+ export const initState: State = {
14
+ status: 'init',
15
+ namespace: null,
16
+ derivationPath: null,
17
+ };
18
+
19
+ export function reducer(state: State, action: Actions): State {
20
+ switch (action.type) {
21
+ case 'needsNamespace':
22
+ return {
23
+ ...state,
24
+ status: 'namespace',
25
+ namespace: action.payload,
26
+ derivationPath: null,
27
+ };
28
+ case 'needsDerivationPath':
29
+ return {
30
+ ...state,
31
+ status: 'derivationPath',
32
+ derivationPath: action.payload,
33
+ };
34
+ case 'reset':
35
+ return initState;
36
+ case 'resetDerivation':
37
+ return {
38
+ ...state,
39
+ derivationPath: null,
40
+ status: 'namespace',
41
+ };
42
+ default:
43
+ throw new Error(`Action hasn't been defined.`);
44
+ }
45
+ }
@@ -0,0 +1,232 @@
1
+ import type { HandleConnectOptions, Result } from './useStatefulConnect.types';
2
+ import type { WalletInfoWithExtra } from '../../types';
3
+ import type {
4
+ Namespace,
5
+ NamespaceData,
6
+ WalletType,
7
+ } from '@rango-dev/wallets-shared';
8
+
9
+ import { WalletState } from '@rango-dev/ui';
10
+ import { useWallets } from '@rango-dev/wallets-react';
11
+ import { useReducer } from 'react';
12
+
13
+ import {
14
+ isStateOnDerivationPathStep,
15
+ isStateOnNamespace,
16
+ } from './useStatefulConnect.helpers';
17
+ import { initState, reducer, type State } from './useStatefulConnect.state';
18
+ import { ResultStatus } from './useStatefulConnect.types';
19
+
20
+ interface UseStatefulConnect {
21
+ handleConnect: (
22
+ wallet: WalletInfoWithExtra,
23
+ options?: HandleConnectOptions
24
+ ) => Promise<Result>;
25
+ handleNamespace: (
26
+ wallet: WalletInfoWithExtra,
27
+ selectedNamespaces: Namespace[]
28
+ ) => Promise<Result>;
29
+ handleDerivationPath: (path: string) => Promise<Result>;
30
+ handleDisconnect: (type: WalletType) => Promise<Result>;
31
+ getState(): State;
32
+ resetState(section?: 'derivation'): void;
33
+ }
34
+
35
+ /**
36
+ *
37
+ * This hook maintains state of wallet and checks if additional information
38
+ * is required to call the `connect` method of `core` package properly.
39
+ *
40
+ * The final goal is running `runConnect` which calls `wallets/core`'s `connect` method.
41
+ * But sometimes it needs to get more information like what namespace it should be connected to,
42
+ * or what derivation path should be used for.
43
+ *
44
+ */
45
+ export function useStatefulConnect(): UseStatefulConnect {
46
+ const { state, disconnect, connect } = useWallets();
47
+
48
+ const [connectState, dispatch] = useReducer(reducer, initState);
49
+
50
+ const runConnect = async (
51
+ type: WalletType,
52
+ namespaces?: NamespaceData[],
53
+ _options?: HandleConnectOptions
54
+ ): Promise<{ status: ResultStatus }> => {
55
+ /*
56
+ * When running this function, it means all optional steps (like namespace and derivation path) has passed, or wallet doesn't need them at all.
57
+ * By resetting state, we will make sure when user tries to connect other wallets, it doesn't have unexpected states.
58
+ */
59
+ dispatch({
60
+ type: 'reset',
61
+ });
62
+
63
+ try {
64
+ await connect(type, undefined, namespaces);
65
+
66
+ return { status: ResultStatus.Connected };
67
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
68
+ } catch (e: any) {
69
+ const message = e?.message
70
+ ? `Error: ${e.message}`
71
+ : 'An unknown error happened during connecting wallet.';
72
+
73
+ throw new Error(message, { cause: e });
74
+ }
75
+ };
76
+
77
+ const handleConnect = async (
78
+ wallet: WalletInfoWithExtra,
79
+ options?: HandleConnectOptions
80
+ ): Promise<{
81
+ status: ResultStatus;
82
+ }> => {
83
+ const isDisconnected = wallet.state === WalletState.DISCONNECTED;
84
+
85
+ if (isDisconnected) {
86
+ if (!wallet.namespaces) {
87
+ return await runConnect(wallet.type, undefined, options);
88
+ }
89
+
90
+ const needsNamespace = wallet.namespaces.length > 1;
91
+ const needsDerivationPath = wallet.needsDerivationPath;
92
+
93
+ if (needsNamespace) {
94
+ dispatch({
95
+ type: 'needsNamespace',
96
+ payload: {
97
+ providerType: wallet.type,
98
+ providerImage: wallet.image,
99
+ availableNamespaces: wallet.namespaces,
100
+ singleNamespace: wallet.singleNamespace,
101
+ },
102
+ });
103
+ return { status: ResultStatus.Namespace };
104
+ } else if (needsDerivationPath) {
105
+ dispatch({
106
+ type: 'needsDerivationPath',
107
+ payload: {
108
+ providerType: wallet.type,
109
+ providerImage: wallet.image,
110
+ namespace: wallet.namespaces[0],
111
+ },
112
+ });
113
+ return { status: ResultStatus.DerivationPath };
114
+ }
115
+
116
+ return await runConnect(
117
+ wallet.type,
118
+ wallet.namespaces.map((namespace) => ({ namespace })),
119
+ options
120
+ );
121
+ }
122
+
123
+ if (options?.disconnectIfConnected) {
124
+ await handleDisconnect(wallet.type);
125
+ return { status: ResultStatus.Disconnected };
126
+ }
127
+
128
+ return { status: ResultStatus.DisconnectedUnhandled };
129
+ };
130
+
131
+ const handleNamespace = async (
132
+ wallet: WalletInfoWithExtra,
133
+ selectedNamespaces: Namespace[]
134
+ ): Promise<Result> => {
135
+ const isSingleNamespace = wallet.singleNamespace;
136
+ const needsDerivationPath = wallet.needsDerivationPath;
137
+ const firstSelectedNamespace = selectedNamespaces[0];
138
+
139
+ if (!firstSelectedNamespace) {
140
+ throw new Error(
141
+ 'To confirm a namespace, you should select at least one namespace.'
142
+ );
143
+ }
144
+
145
+ /*
146
+ * Currently we support derivation path only for single namespace wallets.
147
+ *
148
+ * Basically Single namespace is a list with radio buttons to let the user select one value within the list.
149
+ */
150
+ if (isSingleNamespace && needsDerivationPath) {
151
+ dispatch({
152
+ type: 'needsDerivationPath',
153
+ payload: {
154
+ providerType: wallet.type,
155
+ providerImage: wallet.image,
156
+ namespace: firstSelectedNamespace,
157
+ },
158
+ });
159
+
160
+ return { status: ResultStatus.DerivationPath };
161
+ }
162
+
163
+ /**
164
+ * If execution reaches here, means we don't have any more optional step (like namespace and derivation)
165
+ * So we should start connecting wallet.
166
+ */
167
+
168
+ if (!isStateOnNamespace(connectState)) {
169
+ // Unreachable code.
170
+ throw new Error(
171
+ 'Something went wrong on handling namespace. Please retry.'
172
+ );
173
+ }
174
+
175
+ const type = connectState.namespace.providerType;
176
+ const namespaces = selectedNamespaces.map((namespace) => ({
177
+ namespace,
178
+ }));
179
+
180
+ return await runConnect(type, namespaces);
181
+ };
182
+
183
+ const handleDerivationPath = async (
184
+ derivationPath: string
185
+ ): Promise<Result> => {
186
+ if (!derivationPath) {
187
+ throw new Error(
188
+ "Derivation path is empty. Please make sure you've filled the field correctly."
189
+ );
190
+ }
191
+
192
+ if (!isStateOnDerivationPathStep(connectState)) {
193
+ throw new Error(
194
+ 'It seems you are filling derivation path without setting namespace before doing that. Please retry to connect.'
195
+ );
196
+ }
197
+ const type = connectState.derivationPath.providerType;
198
+ const selectedNamespace = connectState.derivationPath.namespace;
199
+ const namespaces = [{ namespace: selectedNamespace, derivationPath }];
200
+
201
+ return await runConnect(type, namespaces);
202
+ };
203
+
204
+ const handleDisconnect = async (type: WalletType): Promise<Result> => {
205
+ const wallet = state(type);
206
+ if (wallet.connected) {
207
+ await disconnect(type);
208
+ return { status: ResultStatus.Disconnected };
209
+ }
210
+
211
+ return { status: ResultStatus.Noop };
212
+ };
213
+
214
+ return {
215
+ handleConnect,
216
+ handleDisconnect,
217
+ handleNamespace,
218
+ handleDerivationPath,
219
+ getState: () => connectState,
220
+ resetState: (section?: 'derivation') => {
221
+ if (section === 'derivation') {
222
+ dispatch({
223
+ type: 'resetDerivation',
224
+ });
225
+ } else {
226
+ dispatch({
227
+ type: 'reset',
228
+ });
229
+ }
230
+ },
231
+ };
232
+ }
@@ -0,0 +1,56 @@
1
+ import type { Namespace } from '@rango-dev/wallets-shared';
2
+
3
+ export interface HandleConnectOptions {
4
+ // To have a switch between connect and disconnect when user is clicking on a button, this option can be helpful.
5
+ disconnectIfConnected?: boolean;
6
+ }
7
+
8
+ export interface NeedsNamespacesState {
9
+ providerType: string;
10
+ providerImage: string;
11
+ availableNamespaces?: Namespace[];
12
+ singleNamespace?: boolean;
13
+ }
14
+
15
+ export interface NeedsDerivationPathState {
16
+ providerType: string;
17
+ providerImage: string;
18
+ namespace: Namespace;
19
+ }
20
+
21
+ export enum ResultStatus {
22
+ Connected = 'connected',
23
+ Namespace = 'namespace',
24
+ DerivationPath = 'derivation-path',
25
+ Disconnected = 'disconnected',
26
+ DisconnectedUnhandled = 'disconnected-unhandled',
27
+ Noop = 'noop',
28
+ }
29
+
30
+ export type Result = { status: ResultStatus };
31
+
32
+ // State types
33
+
34
+ interface UpdateNamespaceAction {
35
+ type: 'needsNamespace';
36
+ payload: NeedsNamespacesState;
37
+ }
38
+
39
+ interface UpdateDerivationPathAction {
40
+ type: 'needsDerivationPath';
41
+ payload: NeedsDerivationPathState;
42
+ }
43
+
44
+ interface ResetAction {
45
+ type: 'reset';
46
+ }
47
+
48
+ interface ResetDerivationAction {
49
+ type: 'resetDerivation';
50
+ }
51
+
52
+ export type Actions =
53
+ | ResetAction
54
+ | ResetDerivationAction
55
+ | UpdateDerivationPathAction
56
+ | UpdateNamespaceAction;
@@ -1,5 +1,5 @@
1
+ import type { WalletInfoWithExtra } from '../types';
1
2
  import type { WalletInfo } from '@rango-dev/ui';
2
- import type { NamespaceData, WalletType } from '@rango-dev/wallets-shared';
3
3
  import type { BlockchainMeta } from 'rango-sdk';
4
4
 
5
5
  import { WalletState } from '@rango-dev/ui';
@@ -9,11 +9,10 @@ import {
9
9
  KEPLR_COMPATIBLE_WALLETS,
10
10
  WalletTypes,
11
11
  } from '@rango-dev/wallets-shared';
12
- import { useCallback, useEffect, useState } from 'react';
12
+ import { useCallback, useEffect } from 'react';
13
13
 
14
14
  import { useAppStore } from '../store/AppStore';
15
15
  import { useWalletsStore } from '../store/wallets';
16
- import { isSingleWalletActive } from '../utils/common';
17
16
  import { configWalletsToWalletName } from '../utils/providers';
18
17
  import {
19
18
  hashWalletsState,
@@ -22,26 +21,33 @@ import {
22
21
  sortWalletsBasedOnConnectionState,
23
22
  } from '../utils/wallets';
24
23
 
24
+ import { useStatefulConnect } from './useStatefulConnect/useStatefulConnect';
25
+
25
26
  const ALL_SUPPORTED_WALLETS = Object.values(WalletTypes);
26
27
 
27
28
  interface Params {
28
29
  chain?: string;
29
- onBeforeConnect?: (walletType: string) => void;
30
- onConnect?: (walletType: string) => void;
31
- onError?: (error?: string) => void;
30
+ }
31
+
32
+ interface API {
33
+ list: WalletInfoWithExtra[];
34
+ terminateConnectingWallets: () => void;
32
35
  }
33
36
 
34
37
  /**
35
- * gets list of wallets with their information and an action for handling click callback fo UI
36
- * we need to share the logic of rendering list of wallets and handle clicking on them in different places
37
- * you can use this list whenever you need to show the list of wallets and needed callbacks
38
+ *
39
+ * Returning list of wallets which has a applied sorting and filtering (some of wallet can be excluded).
40
+ * It can have some functionality on list itself,
41
+ * now it only has a method to disconnect the wallets that has `connecting` status. This is useful for exiting page and terminating wallet connection.
42
+ *
38
43
  */
39
- export function useWalletList(params: Params) {
40
- const { chain, onBeforeConnect, onConnect, onError } = params;
44
+ export function useWalletList(params?: Params): API {
45
+ const { chain } = params || {};
41
46
  const { config } = useAppStore();
42
- const { state, disconnect, getWalletInfo, connect } = useWallets();
47
+ const { state, getWalletInfo } = useWallets();
43
48
  const { connectedWallets } = useWalletsStore();
44
49
  const blockchains = useAppStore().blockchains();
50
+ const { handleDisconnect } = useStatefulConnect();
45
51
 
46
52
  /** It can be what has been set by widget config or as a fallback we use all the supported wallets by our library */
47
53
  const listAvailableWalletTypes =
@@ -67,7 +73,6 @@ export function useWalletList(params: Params) {
67
73
  : wallets;
68
74
 
69
75
  const sortedWallets = sortWalletsBasedOnConnectionState(wallets);
70
- const [error, setError] = useState('');
71
76
 
72
77
  const isExperimentalChainNotAdded = (walletType: string) =>
73
78
  !connectedWallets.find(
@@ -76,51 +81,18 @@ export function useWalletList(params: Params) {
76
81
  connectedWallet.chain === chain
77
82
  );
78
83
 
79
- const handleClick = async (
80
- type: WalletType,
81
- namespaces?: NamespaceData[]
82
- ) => {
83
- const wallet = state(type);
84
- try {
85
- if (error) {
86
- setError('');
87
- }
88
- if (wallet.connected) {
89
- await disconnect(type);
90
- } else {
91
- if (isSingleWalletActive(wallets, config.multiWallets)) {
92
- return;
93
- }
94
- onBeforeConnect?.(type);
95
- await connect(type, undefined, namespaces);
96
- onConnect?.(type);
97
- }
98
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
- } catch (e: any) {
100
- onError?.(e?.message);
101
- setError('Error: ' + e?.message);
102
- }
103
- };
104
-
105
- const disconnectConnectingWallets = useCallback(() => {
84
+ const terminateConnectingWallets = useCallback(() => {
106
85
  const connectingWallets =
107
86
  wallets?.filter((wallet) => wallet.state === WalletState.CONNECTING) ||
108
87
  [];
109
88
  for (const wallet of connectingWallets) {
110
- void disconnect(wallet.type);
89
+ void handleDisconnect(wallet.type);
111
90
  }
112
91
  }, [hashWalletsState(wallets)]);
113
92
 
114
- const disconnectWallet = async (type: WalletType) => {
115
- const wallet = state(type);
116
- if (wallet.connected) {
117
- await disconnect(type);
118
- }
119
- };
120
-
121
93
  useEffect(() => {
122
94
  return () => {
123
- disconnectConnectingWallets();
95
+ terminateConnectingWallets();
124
96
  };
125
97
  }, []);
126
98
 
@@ -175,9 +147,6 @@ export function useWalletList(params: Params) {
175
147
  list: sortedWallets.filter(
176
148
  (wallet) => !shouldExcludeWallet(wallet.type, chain ?? '', blockchains)
177
149
  ),
178
- error,
179
- handleClick,
180
- disconnectConnectingWallets,
181
- disconnectWallet,
150
+ terminateConnectingWallets,
182
151
  };
183
152
  }
package/src/index.ts CHANGED
@@ -35,9 +35,9 @@ import type {
35
35
  StepTxExecutionUpdatedEvent,
36
36
  } from '@rango-dev/queue-manager-rango-preset';
37
37
  import type {
38
- EventHandler as HandleWalletsUpdate,
39
- ProviderInterface,
40
- } from '@rango-dev/wallets-react';
38
+ LegacyEventHandler as HandleWalletsUpdate,
39
+ LegacyProviderInterface as ProviderInterface,
40
+ } from '@rango-dev/wallets-core/legacy';
41
41
  import type {
42
42
  WalletInfo,
43
43
  WalletState,
@@ -52,11 +52,8 @@ import {
52
52
  StepExecutionBlockedEventStatus,
53
53
  StepExecutionEventStatus,
54
54
  } from '@rango-dev/queue-manager-rango-preset';
55
- import {
56
- readAccountAddress,
57
- useWallets,
58
- Events as WalletEvents,
59
- } from '@rango-dev/wallets-react';
55
+ import { legacyReadAccountAddress as readAccountAddress } from '@rango-dev/wallets-core/legacy';
56
+ import { useWallets, Events as WalletEvents } from '@rango-dev/wallets-react';
60
57
  import { Networks, WalletTypes } from '@rango-dev/wallets-shared';
61
58
  import { PendingSwapNetworkStatus } from 'rango-types';
62
59
 
@@ -66,6 +63,7 @@ import { WidgetWallets } from './containers/Wallets';
66
63
  import { Widget } from './containers/Widget';
67
64
  import { useWidget } from './containers/WidgetInfo';
68
65
  import { WidgetProvider } from './containers/WidgetProvider';
66
+ import { useStatefulConnect } from './hooks/useStatefulConnect';
69
67
  import { useWalletList } from './hooks/useWalletList';
70
68
  import { useWidgetEvents } from './hooks/useWidgetEvents';
71
69
  import { widgetEventEmitter } from './services/eventEmitter';
@@ -122,6 +120,7 @@ export {
122
120
  WidgetProvider,
123
121
  useWidget,
124
122
  useWallets,
123
+ useStatefulConnect,
125
124
  /**
126
125
  * @deprecated Use `widgetEventEmitter` instead. This hook will be removed in future versions.
127
126
  */