@rango-dev/widget-embedded 0.30.1-next.19 → 0.30.1-next.20
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/dist/components/TokenList/TokenList.d.ts.map +1 -1
- package/dist/hooks/useObserveBalanceChanges/index.d.ts +2 -0
- package/dist/hooks/useObserveBalanceChanges/index.d.ts.map +1 -0
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts +4 -0
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts.map +1 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/dist/widget-embedded.build.json +1 -1
- package/package.json +1 -1
- package/src/components/TokenList/TokenList.tsx +8 -2
- package/src/hooks/useObserveBalanceChanges/index.ts +1 -0
- package/src/hooks/useObserveBalanceChanges/useObserveBalanceChanges.ts +68 -0
package/package.json
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
} from '@rango-dev/ui';
|
|
18
18
|
import React, { useEffect, useState } from 'react';
|
|
19
19
|
|
|
20
|
+
import { useObserveBalanceChanges } from '../../hooks/useObserveBalanceChanges';
|
|
20
21
|
import { useAppStore } from '../../store/AppStore';
|
|
21
22
|
import { useWalletsStore } from '../../store/wallets';
|
|
22
23
|
import { createTintsAndShades } from '../../utils/colors';
|
|
@@ -99,6 +100,11 @@ export function TokenList(props: PropTypes) {
|
|
|
99
100
|
const [hasNextPage, setHasNextPage] = useState<boolean>(true);
|
|
100
101
|
const { getBalanceFor, loading: loadingWallet } = useWalletsStore();
|
|
101
102
|
const { isTokenPinned } = useAppStore();
|
|
103
|
+
/**
|
|
104
|
+
* We can create the key by hashing the list of tokens,
|
|
105
|
+
* but if the list is large, the memory usage and cost of comparisons may be high.
|
|
106
|
+
*/
|
|
107
|
+
const { balanceKey } = useObserveBalanceChanges(selectedBlockchain);
|
|
102
108
|
|
|
103
109
|
const loadNextPage = () => {
|
|
104
110
|
setTokens(list.slice(0, tokens.length + PAGE_SIZE));
|
|
@@ -110,7 +116,7 @@ export function TokenList(props: PropTypes) {
|
|
|
110
116
|
|
|
111
117
|
useEffect(() => {
|
|
112
118
|
setTokens(list.slice(0, PAGE_SIZE));
|
|
113
|
-
}, [list.length, selectedBlockchain]);
|
|
119
|
+
}, [list.length, selectedBlockchain, balanceKey]);
|
|
114
120
|
|
|
115
121
|
const renderList = () => {
|
|
116
122
|
if (!tokens.length && !!searchedFor) {
|
|
@@ -244,7 +250,7 @@ export function TokenList(props: PropTypes) {
|
|
|
244
250
|
);
|
|
245
251
|
}}
|
|
246
252
|
totalCount={tokens.length}
|
|
247
|
-
key={`${selectedBlockchain}-${searchedFor}`}
|
|
253
|
+
key={`${selectedBlockchain}-${searchedFor}-${balanceKey}`}
|
|
248
254
|
/>
|
|
249
255
|
);
|
|
250
256
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useObserveBalanceChanges } from './useObserveBalanceChanges';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { WalletEventData } from '../../types';
|
|
2
|
+
|
|
3
|
+
import { useWallets } from '@rango-dev/wallets-react';
|
|
4
|
+
import { useEffect, useRef, useState } from 'react';
|
|
5
|
+
|
|
6
|
+
import { widgetEventEmitter } from '../../services/eventEmitter';
|
|
7
|
+
import { useWalletsStore } from '../../store/wallets';
|
|
8
|
+
import { WalletEventTypes, WidgetEvents } from '../../types';
|
|
9
|
+
|
|
10
|
+
// A hook to listen for and detect changes in balances on a specific blockchain.
|
|
11
|
+
export function useObserveBalanceChanges(selectedBlockchain?: string) {
|
|
12
|
+
const { connectedWallets } = useWalletsStore();
|
|
13
|
+
const { getWalletInfo } = useWallets();
|
|
14
|
+
const prevFetchingBalanceWallets = useRef<string[]>([]);
|
|
15
|
+
// The "balanceKey" will be updated and incremented after each change in the balance for a blockchain.
|
|
16
|
+
const [balanceKey, setBalanceKey] = useState(0);
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const handleWalletEvent = (event: WalletEventData) => {
|
|
20
|
+
if (event.type === WalletEventTypes.DISCONNECT) {
|
|
21
|
+
const walletInfo = getWalletInfo(event.payload.walletType);
|
|
22
|
+
const blockchainSupported = walletInfo.supportedChains.some(
|
|
23
|
+
(chain) => chain.name === selectedBlockchain
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
if (blockchainSupported) {
|
|
27
|
+
setBalanceKey((prev) => prev + 1);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
widgetEventEmitter.on(WidgetEvents.WalletEvent, handleWalletEvent);
|
|
33
|
+
|
|
34
|
+
if (!selectedBlockchain) {
|
|
35
|
+
return setBalanceKey((prev) => prev + 1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const supportedWallets = connectedWallets.filter(
|
|
39
|
+
(wallet) => wallet.chain === selectedBlockchain
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
supportedWallets.forEach((wallet) => {
|
|
43
|
+
const { walletType } = wallet;
|
|
44
|
+
const walletIsAlreadyFetching =
|
|
45
|
+
prevFetchingBalanceWallets.current.includes(walletType);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Watching for changes in "wallet.balances.length" is not accurate.
|
|
49
|
+
* Additionally, checking the equality of previous and current balances for a specific blockchain is resource-intensive, so we avoid these methods.
|
|
50
|
+
*/
|
|
51
|
+
if (wallet.loading && !walletIsAlreadyFetching) {
|
|
52
|
+
prevFetchingBalanceWallets.current =
|
|
53
|
+
prevFetchingBalanceWallets.current.concat(walletType);
|
|
54
|
+
} else if (!wallet.loading && walletIsAlreadyFetching) {
|
|
55
|
+
prevFetchingBalanceWallets.current =
|
|
56
|
+
prevFetchingBalanceWallets.current.filter(
|
|
57
|
+
(prevWallet) => prevWallet !== walletType
|
|
58
|
+
);
|
|
59
|
+
setBalanceKey((prev) => prev + 1);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return () =>
|
|
64
|
+
widgetEventEmitter.off(WidgetEvents.WalletEvent, handleWalletEvent);
|
|
65
|
+
}, [connectedWallets, selectedBlockchain, getWalletInfo]);
|
|
66
|
+
|
|
67
|
+
return { balanceKey };
|
|
68
|
+
}
|