@rango-dev/widget-embedded 0.28.3-next.5 → 0.28.3-next.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/widget-embedded",
3
- "version": "0.28.3-next.5",
3
+ "version": "0.28.3-next.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -53,4 +53,4 @@
53
53
  "publishConfig": {
54
54
  "access": "public"
55
55
  }
56
- }
56
+ }
@@ -18,7 +18,7 @@ import {
18
18
  Typography,
19
19
  WalletIcon,
20
20
  } from '@rango-dev/ui';
21
- import React, { useEffect, useRef, useState } from 'react';
21
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
22
22
  import { useNavigate } from 'react-router-dom';
23
23
 
24
24
  import { WIDGET_UI_ID } from '../../constants';
@@ -59,6 +59,7 @@ const NUMBER_OF_WALLETS_TO_DISPLAY = 2;
59
59
  export function ConfirmWalletsModal(props: PropTypes) {
60
60
  //TODO: move component's logics to a custom hook
61
61
  const { open, onClose, onCancel, onCheckBalance, loading } = props;
62
+ const navigate = useNavigate();
62
63
  const config = useAppStore().config;
63
64
 
64
65
  const customDestinationInputRef = useRef<HTMLInputElement | null>(null);
@@ -104,16 +105,34 @@ export function ConfirmWalletsModal(props: PropTypes) {
104
105
  const isWalletRequiredFor = (blockchain: string) =>
105
106
  requiredWallets.includes(blockchain);
106
107
 
107
- const getInitialSelectableWallets = () =>
108
- connectedWallets.filter((connectedWallet) => {
109
- return (
110
- connectedWallet.selected && quoteWallets.includes(connectedWallet.chain)
111
- );
112
- });
108
+ const getInitialSelectableWallets = useCallback(
109
+ () =>
110
+ connectedWallets.filter((connectedWallet) => {
111
+ return (
112
+ connectedWallet.selected &&
113
+ quoteWallets.includes(connectedWallet.chain)
114
+ );
115
+ }),
116
+ [connectedWallets.length, quoteWallets.length]
117
+ );
113
118
 
114
119
  const [selectableWallets, setSelectableWallets] = useState<ConnectedWallet[]>(
115
120
  getInitialSelectableWallets()
116
121
  );
122
+ const [nextSelectedWallets, setNextSelectedWallets] = useState<
123
+ {
124
+ blockchain: string;
125
+ walletType: string;
126
+ }[]
127
+ >([]);
128
+
129
+ const addNextSelectedWallets = (blockchain: string, walletType: string) =>
130
+ setNextSelectedWallets((prevState) =>
131
+ prevState.concat({
132
+ blockchain,
133
+ walletType,
134
+ })
135
+ );
117
136
 
118
137
  const isInsufficientBalanceModalOpen = balanceWarnings.length > 0;
119
138
 
@@ -206,7 +225,11 @@ export function ConfirmWalletsModal(props: PropTypes) {
206
225
  }
207
226
 
208
227
  onCancel();
209
- if (wallet.chain === lastStepToBlockchain?.name && showCustomDestination) {
228
+ if (
229
+ wallet.chain === lastStepToBlockchain?.name &&
230
+ showCustomDestination &&
231
+ !isWalletRequiredFor(lastStepToBlockchain.name)
232
+ ) {
210
233
  setShowCustomDestination(false);
211
234
  setCustomDestination(null);
212
235
  }
@@ -273,10 +296,30 @@ export function ConfirmWalletsModal(props: PropTypes) {
273
296
  };
274
297
 
275
298
  useEffect(() => {
276
- setSelectableWallets((selectableWallets) =>
277
- selectableWallets.concat(
299
+ setSelectableWallets((selectableWallets) => {
300
+ let nextState: typeof selectableWallets = [];
301
+
302
+ //if wallet disconnected we should unselect the wallet from the list
303
+ selectableWallets.forEach((selectableWallet) => {
304
+ const walletDisconnected = !connectedWallets.some(
305
+ (connectedWallet) =>
306
+ connectedWallet.chain === selectableWallet.chain &&
307
+ connectedWallet.walletType === selectableWallet.walletType &&
308
+ connectedWallet.address === selectableWallet.address
309
+ );
310
+ if (!walletDisconnected) {
311
+ nextState.push(selectableWallet);
312
+ }
313
+ });
314
+
315
+ /**
316
+ * We confirm if a newly connected wallet for a blockchain is marked as selected in the store,
317
+ * but there are no selected wallets for that blockchain in our list.
318
+ * If this condition is met, we include the wallet in our list as selected.
319
+ */
320
+ nextState = nextState.concat(
278
321
  connectedWallets.filter((connectedWallet) => {
279
- const anyWalletSelected = !!selectableWallets.find(
322
+ const anyWalletSelected = !!nextState.find(
280
323
  (selectableWallet) =>
281
324
  selectableWallet.chain === connectedWallet.chain
282
325
  );
@@ -287,16 +330,35 @@ export function ConfirmWalletsModal(props: PropTypes) {
287
330
  quoteWallets.includes(connectedWallet.chain)
288
331
  );
289
332
  })
290
- )
291
- );
292
- }, [connectedWallets.length]);
333
+ );
334
+ return nextState;
335
+ });
336
+ }, [connectedWallets.length, quoteWallets.length]);
337
+
338
+ useEffect(() => {
339
+ const nextState: typeof nextSelectedWallets = [];
340
+
341
+ if (nextSelectedWallets.length > 0) {
342
+ nextSelectedWallets.forEach((selectedWallet) => {
343
+ const wallet = connectedWallets.find(
344
+ (wallet) =>
345
+ wallet.chain === selectedWallet.blockchain &&
346
+ wallet.walletType === selectedWallet.walletType
347
+ );
348
+ if (wallet) {
349
+ onChange(wallet);
350
+ } else {
351
+ nextState.push(selectedWallet);
352
+ }
353
+ });
354
+ setNextSelectedWallets(nextState);
355
+ }
356
+ }, [connectedWallets.length, nextSelectedWallets.length]);
293
357
 
294
358
  const modalContainer = document.getElementById(
295
359
  WIDGET_UI_ID.SWAP_BOX_ID
296
360
  ) as HTMLDivElement;
297
361
 
298
- const navigate = useNavigate();
299
-
300
362
  return (
301
363
  <WatermarkedModal
302
364
  open={open}
@@ -381,7 +443,10 @@ export function ConfirmWalletsModal(props: PropTypes) {
381
443
  chain={showMoreWalletFor}
382
444
  isSelected={isSelected}
383
445
  selectWallet={onChange}
384
- onShowMore={setShowMoreWalletFor.bind(null, showMoreWalletFor)}
446
+ onShowMore={() => setShowMoreWalletFor(showMoreWalletFor)}
447
+ onConnect={(walletType) => {
448
+ addNextSelectedWallets(showMoreWalletFor, walletType);
449
+ }}
385
450
  />
386
451
  </div>
387
452
  </WalletsContainer>
@@ -442,6 +507,9 @@ export function ConfirmWalletsModal(props: PropTypes) {
442
507
  onShowMore={() =>
443
508
  setShowMoreWalletFor(blockchain?.name ?? '')
444
509
  }
510
+ onConnect={(walletType) => {
511
+ addNextSelectedWallets(requiredWallet, walletType);
512
+ }}
445
513
  />
446
514
  </ListContainer>
447
515
  {!isLastWallet && <Divider size={32} />}
@@ -501,12 +569,7 @@ export function ConfirmWalletsModal(props: PropTypes) {
501
569
  suffix={renderCustomDestinationSuffix()}
502
570
  onChange={(e) => {
503
571
  const value = e.target.value;
504
- if (!value.length) {
505
- setShowCustomDestination(false);
506
- setCustomDestination(null);
507
- } else {
508
- setCustomDestination(value.length ? value : null);
509
- }
572
+ setCustomDestination(value);
510
573
  }}
511
574
  {...(!customDestination && { autoFocus: true })}
512
575
  />
@@ -50,7 +50,8 @@ interface WalletNamespacesModalState {
50
50
 
51
51
  const ACCOUNT_ADDRESS_MAX_CHARACTERS = 7;
52
52
  export function WalletList(props: PropTypes) {
53
- const { chain, isSelected, selectWallet, limit, onShowMore } = props;
53
+ const { chain, isSelected, selectWallet, limit, onShowMore, onConnect } =
54
+ props;
54
55
  const isActiveTab = useUiStore.use.isActiveTab();
55
56
 
56
57
  const connectedWallets = useWalletsStore.use.connectedWallets();
@@ -80,7 +81,8 @@ export function WalletList(props: PropTypes) {
80
81
  setOpenWalletStateModal(type);
81
82
  }, TIME_TO_IGNORE_MODAL);
82
83
  },
83
- onConnect: () => {
84
+ onConnect: (type) => {
85
+ onConnect?.(type);
84
86
  if (modalTimerId) {
85
87
  clearTimeout(modalTimerId);
86
88
  }
@@ -6,4 +6,5 @@ export type PropTypes = {
6
6
  selectWallet: (wallet: Wallet) => void;
7
7
  limit?: number;
8
8
  onShowMore: () => void;
9
+ onConnect?: (walletType: string) => void;
9
10
  };
@@ -65,10 +65,15 @@ export const useWalletsStore = createSelectors(
65
65
  .filter((wallet) => wallet.walletType !== accounts[0].walletType)
66
66
  .concat(
67
67
  accounts.map((account) => {
68
- const shouldMarkWalletAsSelected = !state.connectedWallets.find(
68
+ const shouldMarkWalletAsSelected = !state.connectedWallets.some(
69
69
  (connectedWallet) =>
70
70
  connectedWallet.chain === account.chain &&
71
- connectedWallet.selected
71
+ connectedWallet.selected &&
72
+ /**
73
+ * Sometimes, the connect function can be called multiple times for a particular wallet type when using the auto-connect feature.
74
+ * This check is there to make sure the chosen wallet doesn't end up unselected.
75
+ */
76
+ connectedWallet.walletType !== account.walletType
72
77
  );
73
78
  return {
74
79
  balances: [],