@rango-dev/widget-embedded 0.38.1-next.0 → 0.38.1-next.2

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.38.1-next.0",
3
+ "version": "0.38.1-next.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -54,4 +54,4 @@
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  }
57
- }
57
+ }
@@ -14,10 +14,7 @@ import React, { useEffect, useState } from 'react';
14
14
 
15
15
  import { useWallets } from '../..';
16
16
  import { WIDGET_UI_ID } from '../../constants';
17
- import {
18
- ResultStatus,
19
- useStatefulConnect,
20
- } from '../../hooks/useStatefulConnect';
17
+ import { useStatefulConnect } from '../../hooks/useStatefulConnect';
21
18
  import { useWalletList } from '../../hooks/useWalletList';
22
19
  import { useAppStore } from '../../store/AppStore';
23
20
  import { useUiStore } from '../../store/ui';
@@ -227,8 +224,8 @@ export function WalletList(props: PropTypes) {
227
224
  onClose={() => {
228
225
  setSelectedWalletToConnect(undefined);
229
226
  }}
230
- onConnect={(result) => {
231
- if (props.onConnect && result.status === ResultStatus.Connected) {
227
+ onConnect={() => {
228
+ if (props.onConnect) {
232
229
  if (selectedWalletToConnect?.type) {
233
230
  props.onConnect(selectedWalletToConnect.type);
234
231
  } else {
@@ -26,8 +26,8 @@ const DELAY_SHOWING_MODAL_FOR = 300;
26
26
  interface PropTypes {
27
27
  wallet: WalletInfoWithExtra | undefined;
28
28
  onClose: () => void;
29
- // When `handleConnect` runs **successfully**, this will be called afterwards.
30
- onConnect?: (result: Result) => void;
29
+ // When connecting wallet is executed **successfully**, this will be called afterwards.
30
+ onConnect?: () => void;
31
31
  }
32
32
 
33
33
  export function StatefulConnectModal(props: PropTypes) {
@@ -48,9 +48,9 @@ export function StatefulConnectModal(props: PropTypes) {
48
48
  } = useStatefulConnect();
49
49
 
50
50
  const handleConfirmNamespaces = (selectedNamespaces: Namespace[]) => {
51
- handleNamespace(props.wallet!, selectedNamespaces).catch(
52
- catchErrorOnHandle
53
- );
51
+ handleNamespace(props.wallet!, selectedNamespaces)
52
+ .then(afterConnected)
53
+ .catch(catchErrorOnHandle);
54
54
  };
55
55
 
56
56
  const handleDerivationPathConfirm = (derivationPath: string) => {
@@ -60,7 +60,9 @@ export function StatefulConnectModal(props: PropTypes) {
60
60
  );
61
61
  }
62
62
 
63
- handleDerivationPath(derivationPath).catch(catchErrorOnHandle);
63
+ handleDerivationPath(derivationPath)
64
+ .then(afterConnected)
65
+ .catch(catchErrorOnHandle);
64
66
  };
65
67
 
66
68
  const handleClosingModal = () => {
@@ -77,6 +79,26 @@ export function StatefulConnectModal(props: PropTypes) {
77
79
  }
78
80
  };
79
81
 
82
+ const afterConnected = (result: Result, isImmediatelyConnected?: boolean) => {
83
+ const resultIsConnected = result.status === ResultStatus.Connected;
84
+ const resultIsDisconnected = [
85
+ ResultStatus.Disconnected,
86
+ ResultStatus.DisconnectedUnhandled,
87
+ ].includes(result.status);
88
+
89
+ if (resultIsConnected) {
90
+ props.onConnect?.();
91
+ if (!isImmediatelyConnected) {
92
+ successModalTimerId.current = setTimeout(
93
+ handleClosingModal,
94
+ KEEP_SUCCESS_MODAL_FOR
95
+ );
96
+ }
97
+ } else if (resultIsDisconnected) {
98
+ handleClosingModal();
99
+ }
100
+ };
101
+
80
102
  useEffect(() => {
81
103
  if (props.wallet) {
82
104
  resetModalState();
@@ -97,40 +119,21 @@ export function StatefulConnectModal(props: PropTypes) {
97
119
  }, DELAY_SHOWING_MODAL_FOR);
98
120
  };
99
121
 
100
- const afterConnected = (result: Result) => {
101
- const resultIsConnected = result.status === ResultStatus.Connected;
102
- const resultIsDisconnected = [
103
- ResultStatus.Disconnected,
104
- ResultStatus.DisconnectedUnhandled,
105
- ].includes(result.status);
106
- const resultIsNeedMoreStepsToConnect = [
107
- ResultStatus.Namespace,
108
- ResultStatus.DerivationPath,
109
- ].includes(result.status);
110
-
111
- if (!resultIsNeedMoreStepsToConnect) {
112
- isConnected = true;
113
- }
114
-
115
- if (resultIsConnected && !isImmediatelyConnected) {
116
- successModalTimerId.current = setTimeout(
117
- handleClosingModal,
118
- KEEP_SUCCESS_MODAL_FOR
119
- );
120
- } else if (resultIsDisconnected) {
121
- handleClosingModal();
122
- }
123
-
124
- if (props.onConnect) {
125
- props.onConnect(result);
126
- }
127
- };
128
-
129
122
  beforeConnecting();
130
123
  handleConnect(props.wallet, {
131
124
  disconnectIfConnected: true,
132
125
  })
133
- .then(afterConnected)
126
+ .then((result) => {
127
+ const resultIsNeedMoreStepsToConnect = [
128
+ ResultStatus.Namespace,
129
+ ResultStatus.DerivationPath,
130
+ ].includes(result.status);
131
+
132
+ if (!resultIsNeedMoreStepsToConnect) {
133
+ isConnected = true;
134
+ }
135
+ afterConnected(result, isImmediatelyConnected);
136
+ })
134
137
  .catch(catchErrorOnHandle);
135
138
  }
136
139
  }, [props.wallet]);
@@ -280,10 +280,10 @@ export function resetConnectedWalletState(
280
280
 
281
281
  export const calculateWalletUsdValue = (balances: BalanceState) => {
282
282
  const total = Object.values(balances).reduce((prev, balance) => {
283
- const formattedBalance = formatBalance(balance);
284
- return formattedBalance?.usdValue
285
- ? prev.plus(formattedBalance.usdValue)
286
- : prev;
283
+ const usdBalance = balance.usdValue
284
+ ? representAmountInNumber(balance.usdValue, balance.decimals)
285
+ : ZERO.toFixed();
286
+ return prev.plus(usdBalance);
287
287
  }, new BigNumber(ZERO));
288
288
 
289
289
  return numberWithThousandSeparator(total.toString());
@@ -324,16 +324,18 @@ export const getKeplrCompatibleConnectedWallets = (
324
324
  );
325
325
  };
326
326
 
327
+ function representAmountInNumber(amount: string, decimals: number): string {
328
+ return new BigNumber(amount).shiftedBy(-decimals).toFixed();
329
+ }
330
+
327
331
  export function formatBalance(balance: Balance | null): Balance | null {
328
332
  if (!balance) {
329
333
  return null;
330
334
  }
331
335
 
332
- const amount = new BigNumber(balance.amount)
333
- .shiftedBy(-balance.decimals)
334
- .toFixed();
336
+ const amount = representAmountInNumber(balance.amount, balance.decimals);
335
337
  const usdValue = balance.usdValue
336
- ? new BigNumber(balance.usdValue).shiftedBy(-balance.decimals).toFixed()
338
+ ? representAmountInNumber(balance.usdValue, balance.decimals)
337
339
  : null;
338
340
  const formattedAmount = numberToString(
339
341
  amount,