@rango-dev/widget-embedded 0.30.1-next.2 → 0.30.1-next.21

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 (123) hide show
  1. package/dist/components/ConfirmWalletsModal/ConfirmWallets.styles.d.ts +1 -653
  2. package/dist/components/ConfirmWalletsModal/ConfirmWallets.styles.d.ts.map +1 -1
  3. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
  4. package/dist/components/ConfirmWalletsModal/WalletList.d.ts.map +1 -1
  5. package/dist/components/CustomDestination/CustomDestination.d.ts +4 -0
  6. package/dist/components/CustomDestination/CustomDestination.d.ts.map +1 -0
  7. package/dist/components/CustomDestination/CustomDestination.styles.d.ts +495 -0
  8. package/dist/components/CustomDestination/CustomDestination.styles.d.ts.map +1 -0
  9. package/dist/components/CustomDestination/CustomDestination.types.d.ts +7 -0
  10. package/dist/components/CustomDestination/CustomDestination.types.d.ts.map +1 -0
  11. package/dist/components/FilterSelector/FilterSelector.d.ts +4 -0
  12. package/dist/components/FilterSelector/FilterSelector.d.ts.map +1 -0
  13. package/dist/components/FilterSelector/FilterSelector.style.d.ts +962 -0
  14. package/dist/components/FilterSelector/FilterSelector.style.d.ts.map +1 -0
  15. package/dist/components/FilterSelector/FilterSelector.type.d.ts +14 -0
  16. package/dist/components/FilterSelector/FilterSelector.type.d.ts.map +1 -0
  17. package/dist/components/FilterSelector/FilterSelectorContent.d.ts +4 -0
  18. package/dist/components/FilterSelector/FilterSelectorContent.d.ts.map +1 -0
  19. package/dist/components/FilterSelector/index.d.ts +2 -0
  20. package/dist/components/FilterSelector/index.d.ts.map +1 -0
  21. package/dist/components/Quote/Quote.styles.d.ts +3 -3
  22. package/dist/components/RefreshModal/RefreshModal.styles.d.ts +3 -3
  23. package/dist/components/SearchInput/SearchInput.d.ts.map +1 -1
  24. package/dist/components/SwapDetails/SwapDetails.d.ts.map +1 -1
  25. package/dist/components/SwapDetailsModal/SwapDetailsModal.WalletState.d.ts.map +1 -1
  26. package/dist/components/TokenList/TokenList.d.ts.map +1 -1
  27. package/dist/containers/Inputs/Inputs.d.ts.map +1 -1
  28. package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
  29. package/dist/containers/WidgetInfo/WidgetInfo.d.ts.map +1 -1
  30. package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts +5 -1
  31. package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts.map +1 -1
  32. package/dist/hooks/useObserveBalanceChanges/index.d.ts +2 -0
  33. package/dist/hooks/useObserveBalanceChanges/index.d.ts.map +1 -0
  34. package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts +4 -0
  35. package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts.map +1 -0
  36. package/dist/hooks/usePrepareBlockchainList/usePrepareBlockchainList.helpers.d.ts +2 -2
  37. package/dist/hooks/useUpdateQuoteInputs/index.d.ts +2 -0
  38. package/dist/hooks/useUpdateQuoteInputs/index.d.ts.map +1 -0
  39. package/dist/hooks/useUpdateQuoteInputs/useUpdateQuoteInputs.d.ts +3 -0
  40. package/dist/hooks/useUpdateQuoteInputs/useUpdateQuoteInputs.d.ts.map +1 -0
  41. package/dist/index.d.ts +4 -4
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +2 -2
  44. package/dist/index.js.map +4 -4
  45. package/dist/pages/HistoryPage.d.ts.map +1 -1
  46. package/dist/pages/Home.d.ts.map +1 -1
  47. package/dist/pages/WalletsPage.d.ts.map +1 -1
  48. package/dist/services/cacheService.d.ts +21 -0
  49. package/dist/services/cacheService.d.ts.map +1 -0
  50. package/dist/store/notification.d.ts.map +1 -1
  51. package/dist/store/slices/config.d.ts +2 -1
  52. package/dist/store/slices/config.d.ts.map +1 -1
  53. package/dist/store/slices/data.d.ts +2 -1
  54. package/dist/store/slices/data.d.ts.map +1 -1
  55. package/dist/store/utils/data.d.ts +20 -0
  56. package/dist/store/utils/data.d.ts.map +1 -0
  57. package/dist/store/utils/data.test.d.ts +2 -0
  58. package/dist/store/utils/data.test.d.ts.map +1 -0
  59. package/dist/store/utils/index.d.ts +2 -0
  60. package/dist/store/utils/index.d.ts.map +1 -0
  61. package/dist/test-utils/fixtures.d.ts +1 -0
  62. package/dist/test-utils/fixtures.d.ts.map +1 -1
  63. package/dist/types/config.d.ts +5 -0
  64. package/dist/types/config.d.ts.map +1 -1
  65. package/dist/types/event.d.ts +21 -15
  66. package/dist/types/event.d.ts.map +1 -1
  67. package/dist/types/notification.d.ts +1 -0
  68. package/dist/types/notification.d.ts.map +1 -1
  69. package/dist/types/quote.d.ts +9 -1
  70. package/dist/types/quote.d.ts.map +1 -1
  71. package/dist/types/wallets.d.ts +1 -1
  72. package/dist/types/wallets.d.ts.map +1 -1
  73. package/dist/utils/events.d.ts +8 -0
  74. package/dist/utils/events.d.ts.map +1 -0
  75. package/dist/utils/settings.d.ts +1 -1
  76. package/dist/utils/swap.d.ts +1 -1
  77. package/dist/utils/swap.d.ts.map +1 -1
  78. package/dist/widget-embedded.build.json +1 -1
  79. package/package.json +11 -11
  80. package/src/components/ConfirmWalletsModal/ConfirmWallets.styles.ts +1 -49
  81. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +59 -178
  82. package/src/components/ConfirmWalletsModal/WalletList.tsx +16 -4
  83. package/src/components/CustomDestination/CustomDestination.styles.ts +45 -0
  84. package/src/components/CustomDestination/CustomDestination.tsx +148 -0
  85. package/src/components/CustomDestination/CustomDestination.types.ts +7 -0
  86. package/src/components/FilterSelector/FilterSelector.style.ts +95 -0
  87. package/src/components/FilterSelector/FilterSelector.tsx +42 -0
  88. package/src/components/FilterSelector/FilterSelector.type.ts +15 -0
  89. package/src/components/FilterSelector/FilterSelectorContent.tsx +60 -0
  90. package/src/components/FilterSelector/index.ts +1 -0
  91. package/src/components/SearchInput/SearchInput.tsx +2 -0
  92. package/src/components/SwapDetails/SwapDetails.tsx +1 -7
  93. package/src/components/SwapDetailsModal/SwapDetailsModal.WalletState.tsx +28 -13
  94. package/src/components/TokenList/TokenList.tsx +8 -2
  95. package/src/containers/Inputs/Inputs.tsx +1 -0
  96. package/src/containers/Wallets/Wallets.tsx +7 -2
  97. package/src/containers/WidgetInfo/WidgetInfo.tsx +26 -0
  98. package/src/containers/WidgetInfo/WidgetInfo.types.ts +5 -1
  99. package/src/hooks/useObserveBalanceChanges/index.ts +1 -0
  100. package/src/hooks/useObserveBalanceChanges/useObserveBalanceChanges.ts +68 -0
  101. package/src/hooks/useUpdateQuoteInputs/index.ts +1 -0
  102. package/src/hooks/useUpdateQuoteInputs/useUpdateQuoteInputs.ts +50 -0
  103. package/src/index.ts +4 -0
  104. package/src/pages/HistoryPage.tsx +180 -26
  105. package/src/pages/Home.tsx +6 -3
  106. package/src/pages/WalletsPage.tsx +25 -11
  107. package/src/services/cacheService.ts +35 -0
  108. package/src/store/notification.ts +13 -1
  109. package/src/store/quote.ts +1 -1
  110. package/src/store/slices/config.ts +39 -1
  111. package/src/store/slices/data.test.ts +84 -0
  112. package/src/store/slices/data.ts +44 -24
  113. package/src/store/utils/data.test.ts +175 -0
  114. package/src/store/utils/data.ts +117 -0
  115. package/src/store/utils/index.ts +1 -0
  116. package/src/test-utils/fixtures.ts +14 -7
  117. package/src/types/config.ts +3 -1
  118. package/src/types/event.ts +32 -16
  119. package/src/types/notification.ts +1 -0
  120. package/src/types/quote.ts +11 -0
  121. package/src/types/wallets.ts +1 -1
  122. package/src/utils/events.ts +27 -0
  123. package/src/utils/swap.ts +4 -4
@@ -0,0 +1,95 @@
1
+ import { css, darkTheme, IconButton, styled } from '@rango-dev/ui';
2
+
3
+ export const IconContainer = styled('div', {
4
+ position: 'relative',
5
+ '&::before': {
6
+ position: 'absolute',
7
+ right: '1px',
8
+ top: '-1px',
9
+ width: '$8',
10
+ height: '$8',
11
+ borderRadius: '100%',
12
+ backgroundColor: '$neutral300',
13
+ [`.${darkTheme} &`]: {
14
+ backgroundColor: '$neutral400',
15
+ },
16
+ padding: '$2',
17
+ },
18
+ variants: {
19
+ isSelect: {
20
+ true: {
21
+ '&::before': {
22
+ content: '',
23
+ },
24
+ },
25
+ },
26
+ },
27
+ });
28
+
29
+ export const FilterButton = styled(IconButton, {
30
+ width: '$36',
31
+ height: '$36',
32
+ position: 'relative',
33
+ padding: '0',
34
+ overflow: 'unset',
35
+ backgroundColor: '$neutral300',
36
+ [`.${darkTheme} &`]: {
37
+ backgroundColor: '$neutral400',
38
+ },
39
+ '&:hover': {
40
+ backgroundColor: '$secondary100',
41
+ [`.${darkTheme} &`]: {
42
+ backgroundColor: '$neutral',
43
+ },
44
+ [`& ${IconContainer}::before`]: {
45
+ backgroundColor: '$secondary100',
46
+ [`.${darkTheme} &`]: {
47
+ backgroundColor: '$neutral',
48
+ },
49
+ },
50
+ },
51
+ variants: {
52
+ isSelect: {
53
+ true: {
54
+ border: '1px solid $secondary',
55
+ },
56
+ },
57
+ },
58
+ });
59
+
60
+ export const Badge = styled('div', {
61
+ position: 'absolute',
62
+ width: '$6',
63
+ height: '$6',
64
+ display: 'flex',
65
+ justifyContent: 'center',
66
+ alignItems: 'center',
67
+ borderRadius: '3px',
68
+ top: '$0',
69
+ right: '1px',
70
+ backgroundColor: '$secondary500',
71
+ });
72
+
73
+ export const FilterContainer = styled('div', {
74
+ padding: '$15',
75
+ borderRadius: '$sm',
76
+ width: '248px',
77
+ backgroundColor: '$background',
78
+ zIndex: 10,
79
+ });
80
+
81
+ export const centeredFlexContainer = css({
82
+ display: 'flex',
83
+ justifyContent: 'space-between',
84
+ alignItems: 'center',
85
+ });
86
+
87
+ export const FilterList = styled('ul', {
88
+ margin: 0,
89
+ listStyle: 'none',
90
+ height: '100%',
91
+ padding: 0,
92
+ '.item-start-container': {
93
+ paddingRight: '0 !important',
94
+ },
95
+ });
@@ -0,0 +1,42 @@
1
+ import type { FilterSelectorPropTypes } from './FilterSelector.type';
2
+
3
+ import { FilterIcon, Popover } from '@rango-dev/ui';
4
+ import React from 'react';
5
+
6
+ import { getContainer } from '../../utils/common';
7
+
8
+ import { Badge, FilterButton, IconContainer } from './FilterSelector.style';
9
+ import { FilterSelectorContent } from './FilterSelectorContent';
10
+
11
+ export function FilterSelector(props: FilterSelectorPropTypes) {
12
+ const { onClickItem, onOpenChange, filterBy, list, open } = props;
13
+ return (
14
+ <div>
15
+ <Popover
16
+ open={open}
17
+ align="end"
18
+ onOpenChange={onOpenChange}
19
+ container={getContainer()}
20
+ content={
21
+ <FilterSelectorContent
22
+ list={list}
23
+ filterBy={filterBy}
24
+ onClickItem={(id) => {
25
+ onClickItem(id);
26
+ onOpenChange(false);
27
+ }}
28
+ />
29
+ }>
30
+ <FilterButton
31
+ variant="default"
32
+ isSelect={!!filterBy}
33
+ onClick={() => onOpenChange(!props.open)}>
34
+ <IconContainer isSelect={!!filterBy}>
35
+ <FilterIcon size={16} color="black" />
36
+ {!!filterBy && <Badge />}
37
+ </IconContainer>
38
+ </FilterButton>
39
+ </Popover>
40
+ </div>
41
+ );
42
+ }
@@ -0,0 +1,15 @@
1
+ export type PropTypes = {
2
+ filterBy: string;
3
+ onClickItem: (id: string) => void;
4
+ list: Array<FilterItem>;
5
+ };
6
+
7
+ export type FilterItem = {
8
+ id: string;
9
+ title: string;
10
+ };
11
+
12
+ export type FilterSelectorPropTypes = PropTypes & {
13
+ open: boolean;
14
+ onOpenChange: (open: boolean) => void;
15
+ };
@@ -0,0 +1,60 @@
1
+ import type { PropTypes } from './FilterSelector.type';
2
+
3
+ import { i18n } from '@lingui/core';
4
+ import {
5
+ Button,
6
+ Divider,
7
+ ListItemButton,
8
+ Radio,
9
+ RadioRoot,
10
+ Typography,
11
+ } from '@rango-dev/ui';
12
+ import React from 'react';
13
+
14
+ import {
15
+ centeredFlexContainer,
16
+ FilterContainer,
17
+ FilterList,
18
+ } from './FilterSelector.style';
19
+
20
+ export function FilterSelectorContent(props: PropTypes) {
21
+ const { filterBy: value, onClickItem } = props;
22
+ return (
23
+ <FilterContainer>
24
+ <div className={centeredFlexContainer()}>
25
+ <Typography size="small" variant="body">
26
+ {i18n.t('Status')}
27
+ </Typography>
28
+ <Button variant="ghost" size="xxsmall" onClick={() => onClickItem('')}>
29
+ {i18n.t('Reset')}
30
+ </Button>
31
+ </div>
32
+ <Divider size={10} />
33
+ <RadioRoot value={value}>
34
+ <FilterList>
35
+ {props.list.map((item, index) => {
36
+ return (
37
+ <ListItemButton
38
+ key={item.id}
39
+ style={{ height: '40px', width: '100%' }}
40
+ selected={false}
41
+ hasDivider={props.list.length - 1 != index}
42
+ id={item.id}
43
+ title={
44
+ <>
45
+ <Divider direction="horizontal" size={4} />
46
+ <Typography size="medium" variant="body">
47
+ {item.title}
48
+ </Typography>
49
+ </>
50
+ }
51
+ start={<Radio value={item.id} />}
52
+ onClick={onClickItem}
53
+ />
54
+ );
55
+ })}
56
+ </FilterList>
57
+ </RadioRoot>
58
+ </FilterContainer>
59
+ );
60
+ }
@@ -0,0 +1 @@
1
+ export { FilterSelector } from './FilterSelector';
@@ -13,6 +13,7 @@ export function SearchInput(props: PropTypes) {
13
13
  size,
14
14
  onChange,
15
15
  value,
16
+ style,
16
17
  setValue,
17
18
  ...inputAttributes
18
19
  } = props;
@@ -41,6 +42,7 @@ export function SearchInput(props: PropTypes) {
41
42
  padding: 10,
42
43
  borderRadius: 25,
43
44
  alignItems: 'center',
45
+ ...style,
44
46
  }}
45
47
  size={size}
46
48
  value={value}
@@ -188,13 +188,7 @@ export function SwapDetails(props: SwapDetailsProps) {
188
188
  )?.diagnosisUrl;
189
189
 
190
190
  const outputUsdValue = numberToString(
191
- parseFloat(
192
- numberToString(
193
- outputAmount,
194
- TOKEN_AMOUNT_MIN_DECIMALS,
195
- TOKEN_AMOUNT_MAX_DECIMALS
196
- )
197
- ) * (lastStep.toUsdPrice || 0),
191
+ parseFloat(outputAmount || '0') * (lastStep.toUsdPrice || 0),
198
192
  USD_VALUE_MIN_DECIMALS,
199
193
  USD_VALUE_MAX_DECIMALS
200
194
  );
@@ -68,6 +68,33 @@ export const WalletStateContent = (props: WalletStateContentProps) => {
68
68
  }
69
69
  };
70
70
 
71
+ const handleWalletItemClick = () => {
72
+ if (!!walletType && !!walletInfo) {
73
+ if (!!walletInfo.namespaces?.length) {
74
+ if (walletInfo.namespaces.length > 1) {
75
+ setNamespacesModalState({
76
+ open: true,
77
+ providerType: walletType,
78
+ providerImage: walletInfo.img,
79
+ availableNamespaces: walletInfo.namespaces,
80
+ singleNamespace: walletInfo.singleNamespace,
81
+ });
82
+ } else if (walletInfo.needsDerivationPath) {
83
+ setDerivationPathModalState({
84
+ open: true,
85
+ providerType: walletType,
86
+ providerImage: walletInfo.img,
87
+ namespace: walletInfo.namespaces[0],
88
+ });
89
+ } else {
90
+ connect(walletType).catch((error) => debug(error));
91
+ }
92
+ } else {
93
+ connect(walletType).catch((error) => debug(error));
94
+ }
95
+ }
96
+ };
97
+
71
98
  const handleConfirmNamespaces = (selectedNamespaces: Namespace[]) => {
72
99
  if (
73
100
  !!walletInfo?.needsDerivationPath &&
@@ -117,19 +144,7 @@ export const WalletStateContent = (props: WalletStateContentProps) => {
117
144
  link={walletInfo.installLink}
118
145
  disabled={walletButtonDisabled}
119
146
  // TODO we need to show an error modal when user reject the connection
120
- onClick={async () => {
121
- if (walletInfo.namespaces) {
122
- setNamespacesModalState({
123
- open: true,
124
- providerType: walletType,
125
- providerImage: walletInfo.img,
126
- availableNamespaces: walletInfo.namespaces,
127
- singleNamespace: walletInfo.singleNamespace,
128
- });
129
- } else {
130
- connect(walletType).catch((error) => debug(error));
131
- }
132
- }}
147
+ onClick={handleWalletItemClick}
133
148
  />
134
149
  </WalletContainer>
135
150
  )}
@@ -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
  };
@@ -109,6 +109,7 @@ export function Inputs(props: PropTypes) {
109
109
  onSelectMaxBalance={() => {
110
110
  setInputAmount(tokenBalanceReal.split(',').join(''));
111
111
  }}
112
+ anyWalletConnected={connectedWallets.length > 0}
112
113
  />
113
114
  <SwitchFromAndToButton />
114
115
  </FromContainer>
@@ -31,7 +31,12 @@ export const WidgetContext = createContext<WidgetContextInterface>({
31
31
  });
32
32
 
33
33
  function Main(props: PropsWithChildren<PropTypes>) {
34
- const { updateConfig, updateSettings, fetch: fetchMeta } = useAppStore();
34
+ const {
35
+ updateConfig,
36
+ updateSettings,
37
+ fetch: fetchMeta,
38
+ fetchStatus,
39
+ } = useAppStore();
35
40
  const blockchains = useAppStore().blockchains();
36
41
  const { findToken } = useAppStore();
37
42
  const config = useAppStore().config;
@@ -61,7 +66,7 @@ function Main(props: PropsWithChildren<PropTypes>) {
61
66
  dappConfig: props.config,
62
67
  };
63
68
  }
64
- }, [props.config]);
69
+ }, [props.config, fetchStatus]);
65
70
 
66
71
  const evmBasedChainNames = blockchains
67
72
  .filter(isEvmBlockchain)
@@ -4,6 +4,7 @@ import { useManager } from '@rango-dev/queue-manager-react';
4
4
  import React, { createContext, useContext } from 'react';
5
5
 
6
6
  import { useLanguage } from '../../hooks/useLanguage';
7
+ import { useUpdateQuoteInputs } from '../../hooks/useUpdateQuoteInputs/useUpdateQuoteInputs';
7
8
  import { useAppStore } from '../../store/AppStore';
8
9
  import { useNotificationStore } from '../../store/notification';
9
10
  import { useQuoteStore } from '../../store/quote';
@@ -35,6 +36,9 @@ export function WidgetInfo(props: React.PropsWithChildren) {
35
36
  const resetLanguage = useLanguage().resetLanguage;
36
37
  const notifications = useNotificationStore().getNotifications();
37
38
  const clearNotifications = useNotificationStore().clearNotifications;
39
+ const updateQuoteInputs = useUpdateQuoteInputs();
40
+ const { fromBlockchain, toBlockchain, fromToken, toToken, inputAmount } =
41
+ useQuoteStore();
38
42
 
39
43
  // eslint-disable-next-line react/jsx-no-constructed-context-values
40
44
  const value: WidgetInfoContextInterface = {
@@ -59,6 +63,28 @@ export function WidgetInfo(props: React.PropsWithChildren) {
59
63
  list: notifications,
60
64
  clearAll: clearNotifications,
61
65
  },
66
+ quote: {
67
+ quoteInputs: {
68
+ fromBlockchain: fromBlockchain?.name ?? null,
69
+ fromToken: fromToken
70
+ ? {
71
+ symbol: fromToken.symbol,
72
+ blockchain: fromToken.blockchain,
73
+ address: fromToken.address,
74
+ }
75
+ : null,
76
+ toBlockchain: toBlockchain?.name ?? null,
77
+ toToken: toToken
78
+ ? {
79
+ symbol: toToken.symbol,
80
+ blockchain: toToken.blockchain,
81
+ address: toToken.address,
82
+ }
83
+ : null,
84
+ requestAmount: inputAmount,
85
+ },
86
+ updateQuoteInputs,
87
+ },
62
88
  };
63
89
 
64
90
  return (
@@ -1,7 +1,7 @@
1
1
  import type { WidgetHistory } from './WidgetInfo.helpers';
2
2
  import type { FetchStatus, FindToken } from '../../store/slices/data';
3
3
  import type { ConnectedWallet } from '../../store/wallets';
4
- import type { Wallet } from '../../types';
4
+ import type { QuoteInputs, UpdateQuoteInputs, Wallet } from '../../types';
5
5
  import type { Notification } from '../../types/notification';
6
6
  import type { BlockchainMeta, SwapperMeta, Token } from 'rango-sdk';
7
7
 
@@ -27,4 +27,8 @@ export interface WidgetInfoContextInterface {
27
27
  list: Notification[];
28
28
  clearAll: () => void;
29
29
  };
30
+ quote: {
31
+ quoteInputs: QuoteInputs;
32
+ updateQuoteInputs: UpdateQuoteInputs;
33
+ };
30
34
  }
@@ -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
+ }
@@ -0,0 +1 @@
1
+ export { useUpdateQuoteInputs } from './useUpdateQuoteInputs';
@@ -0,0 +1,50 @@
1
+ import type { UpdateQuoteInputs } from '../../types';
2
+
3
+ import { useAppStore } from '../../store/AppStore';
4
+ import { useQuoteStore } from '../../store/quote';
5
+
6
+ // This hook provides a function to update quote inputs using fewer and simpler parameters.
7
+ export function useUpdateQuoteInputs() {
8
+ const { findToken } = useAppStore();
9
+ const blockchains = useAppStore().blockchains();
10
+ const tokens = useAppStore().tokens();
11
+ const {
12
+ setFromBlockchain,
13
+ setFromToken,
14
+ setToBlockchain,
15
+ setToToken,
16
+ setInputAmount,
17
+ } = useQuoteStore();
18
+
19
+ const updateQuoteInputs: UpdateQuoteInputs = (params) => {
20
+ const { fromBlockchain, fromToken, toBlockchain, toToken, requestAmount } =
21
+ params;
22
+ const meta = { blockchains, tokens };
23
+
24
+ if (fromBlockchain !== undefined) {
25
+ const blockchainMeta =
26
+ blockchains.find((blockchain) => blockchain.name === fromBlockchain) ??
27
+ null;
28
+ setFromBlockchain(blockchainMeta);
29
+ }
30
+ if (fromToken !== undefined) {
31
+ const token = fromToken ? findToken(fromToken) ?? null : null;
32
+ setFromToken({ meta, token });
33
+ }
34
+ if (toBlockchain !== undefined) {
35
+ const blockchainMeta =
36
+ blockchains.find((blockchain) => blockchain.name === toBlockchain) ??
37
+ null;
38
+ setToBlockchain(blockchainMeta);
39
+ }
40
+ if (toToken !== undefined) {
41
+ const token = toToken ? findToken(toToken) ?? null : null;
42
+ setToToken({ meta, token });
43
+ }
44
+ if (requestAmount !== undefined) {
45
+ setInputAmount(requestAmount);
46
+ }
47
+ };
48
+
49
+ return updateQuoteInputs;
50
+ }
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import type {
4
4
  BlockchainAndTokenConfig,
5
5
  QuoteEventData,
6
6
  Tokens,
7
+ UiEventData,
7
8
  WalletEventData,
8
9
  WalletInfoWithExtra,
9
10
  WidgetColors,
@@ -71,6 +72,7 @@ import { widgetEventEmitter } from './services/eventEmitter';
71
72
  import {
72
73
  WidgetEvents as MainEvents,
73
74
  QuoteEventTypes,
75
+ UiEventTypes,
74
76
  WalletEventTypes,
75
77
  WidgetEvents,
76
78
  } from './types';
@@ -108,6 +110,7 @@ export type {
108
110
  WidgetVariant,
109
111
  WalletEventData,
110
112
  QuoteEventData,
113
+ UiEventData,
111
114
  WalletInfoWithExtra,
112
115
  };
113
116
  export {
@@ -133,6 +136,7 @@ export {
133
136
  MainEvents,
134
137
  QuoteEventTypes,
135
138
  WalletEventTypes,
139
+ UiEventTypes,
136
140
  RouteEventType,
137
141
  StepEventType,
138
142
  StepExecutionEventStatus,