@rango-dev/widget-embedded 0.23.1-next.24 → 0.23.1-next.25

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.23.1-next.24",
3
+ "version": "0.23.1-next.25",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -1,39 +1,11 @@
1
- import type { WalletType } from '@rango-dev/wallets-shared';
2
1
  import type { PropsWithChildren } from 'react';
3
2
 
4
- import { useQueueManager } from '@rango-dev/queue-manager-rango-preset';
5
- import { useWallets } from '@rango-dev/wallets-react';
6
- import { isEvmBlockchain } from 'rango-types';
7
3
  import React, { Fragment } from 'react';
8
4
  import { MemoryRouter, useInRouterContext } from 'react-router';
9
5
 
10
- import { useForceAutoConnect } from '../hooks/useForceAutoConnect';
11
- import { useAppStore } from '../store/AppStore';
12
-
13
- export function AppRouter({
14
- children,
15
- ...props
16
- }: PropsWithChildren & {
17
- lastConnectedWallet: string;
18
- disconnectedWallet: WalletType | undefined;
19
- clearDisconnectedWallet: () => void;
20
- }) {
6
+ export function AppRouter({ children }: PropsWithChildren) {
21
7
  const isRouterInContext = useInRouterContext();
22
8
  const Router = isRouterInContext ? Fragment : MemoryRouter;
23
- const blockchains = useAppStore().blockchains();
24
- const { canSwitchNetworkTo } = useWallets();
25
-
26
- useForceAutoConnect();
27
-
28
- const evmChains = blockchains.filter(isEvmBlockchain);
29
-
30
- useQueueManager({
31
- lastConnectedWallet: props.lastConnectedWallet,
32
- clearDisconnectedWallet: props.clearDisconnectedWallet,
33
- disconnectedWallet: props.disconnectedWallet,
34
- evmChains,
35
- canSwitchNetworkTo,
36
- });
37
9
 
38
10
  return <Router>{children}</Router>;
39
11
  }
@@ -1,48 +1,27 @@
1
- import type { WalletType } from '@rango-dev/wallets-shared';
2
-
3
1
  import { I18nManager } from '@rango-dev/ui';
4
- import React, { useContext, useEffect, useState } from 'react';
2
+ import React from 'react';
5
3
 
6
4
  import { AppRouter } from '../../components/AppRouter';
7
5
  import { AppRoutes } from '../../components/AppRoutes';
8
6
  import { WidgetEvents } from '../../components/WidgetEvents';
9
- import { globalFont } from '../../globalStyles';
10
- import { useFetchApiConfig } from '../../hooks/useFetchApiConfig';
7
+ import { useBootstrap } from '../../hooks/useBootstrap';
11
8
  import { useLanguage } from '../../hooks/useLanguage';
12
9
  import { useTheme } from '../../hooks/useTheme';
13
10
  import { useAppStore } from '../../store/AppStore';
14
- import { useNotificationStore } from '../../store/notification';
15
- import { WidgetContext } from '../Wallets';
16
11
 
17
12
  import { MainContainer } from './App.styles';
18
13
 
19
14
  export function Main() {
20
- globalFont();
21
-
15
+ useBootstrap();
22
16
  const { config } = useAppStore();
23
17
  const { activeTheme } = useTheme(config?.theme || {});
24
18
  const { activeLanguage } = useLanguage();
25
- const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
26
- useState<string>('');
27
- const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
28
- const widgetContext = useContext(WidgetContext);
29
- const { fetchApiConfig } = useFetchApiConfig();
30
- useEffect(() => {
31
- void useNotificationStore.persist.rehydrate();
32
- widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork);
33
- void fetchApiConfig().catch(console.log);
34
- }, []);
35
19
 
36
20
  return (
37
21
  <I18nManager language={activeLanguage}>
38
22
  <MainContainer id="swap-container" className={activeTheme()}>
39
23
  <WidgetEvents />
40
- <AppRouter
41
- lastConnectedWallet={lastConnectedWalletWithNetwork}
42
- disconnectedWallet={disconnectedWallet}
43
- clearDisconnectedWallet={() => {
44
- setDisconnectedWallet(undefined);
45
- }}>
24
+ <AppRouter>
46
25
  <AppRoutes />
47
26
  </AppRouter>
48
27
  </MainContainer>
@@ -1,5 +1,5 @@
1
1
  import type {
2
- OnConnectHandler,
2
+ OnWalletConnectionChange,
3
3
  PropTypes,
4
4
  WidgetContextInterface,
5
5
  } from './Wallets.types';
@@ -10,7 +10,7 @@ import type { PropsWithChildren } from 'react';
10
10
  import { Events, Provider } from '@rango-dev/wallets-react';
11
11
  import { type Network } from '@rango-dev/wallets-shared';
12
12
  import { isEvmBlockchain } from 'rango-sdk';
13
- import React, { createContext, useEffect, useRef } from 'react';
13
+ import React, { createContext, useEffect, useMemo, useRef } from 'react';
14
14
 
15
15
  import { useWalletProviders } from '../../hooks/useWalletProviders';
16
16
  import { AppStoreProvider, useAppStore } from '../../store/AppStore';
@@ -25,6 +25,9 @@ export const WidgetContext = createContext<WidgetContextInterface>({
25
25
  onConnectWallet: () => {
26
26
  return;
27
27
  },
28
+ onDisconnectWallet: () => {
29
+ return;
30
+ },
28
31
  });
29
32
 
30
33
  function Main(props: PropsWithChildren<PropTypes>) {
@@ -36,7 +39,8 @@ function Main(props: PropsWithChildren<PropTypes>) {
36
39
  };
37
40
  const { providers } = useWalletProviders(props.config.wallets, walletOptions);
38
41
  const { connectWallet, disconnectWallet } = useWalletsStore();
39
- const onConnectWalletHandler = useRef<OnConnectHandler>();
42
+ const onConnectWalletHandler = useRef<OnWalletConnectionChange>();
43
+ const onDisconnectWalletHandler = useRef<OnWalletConnectionChange>();
40
44
 
41
45
  useEffect(() => {
42
46
  void fetchMeta().catch(console.log);
@@ -70,6 +74,13 @@ function Main(props: PropsWithChildren<PropTypes>) {
70
74
  }
71
75
  } else {
72
76
  disconnectWallet(type);
77
+ if (!!onDisconnectWalletHandler.current) {
78
+ onDisconnectWalletHandler.current(type);
79
+ } else {
80
+ console.warn(
81
+ `onDisconnectWallet handler hasn't been set. Are you sure?`
82
+ );
83
+ }
73
84
  }
74
85
  }
75
86
  if (event === Events.ACCOUNTS && state.connected) {
@@ -102,14 +113,20 @@ function Main(props: PropsWithChildren<PropTypes>) {
102
113
  };
103
114
  const isActiveTab = useUiStore.use.isActiveTab();
104
115
 
116
+ const handlers = useMemo(
117
+ () => ({
118
+ onConnectWallet: (handler: OnWalletConnectionChange) => {
119
+ onConnectWalletHandler.current = handler;
120
+ },
121
+ onDisconnectWallet: (handler: OnWalletConnectionChange) => {
122
+ onDisconnectWalletHandler.current = handler;
123
+ },
124
+ }),
125
+ []
126
+ );
127
+
105
128
  return (
106
- <WidgetContext.Provider
107
- // eslint-disable-next-line react/jsx-no-constructed-context-values
108
- value={{
109
- onConnectWallet: (handler) => {
110
- onConnectWalletHandler.current = handler;
111
- },
112
- }}>
129
+ <WidgetContext.Provider value={handlers}>
113
130
  <Provider
114
131
  allBlockChains={blockchains}
115
132
  providers={providers}
@@ -1,9 +1,20 @@
1
1
  import type { WidgetConfig } from '../../types';
2
2
  import type { EventHandler } from '@rango-dev/wallets-react';
3
3
 
4
- export type OnConnectHandler = (key: string) => void;
4
+ export type OnWalletConnectionChange = (key: string) => void;
5
5
  export interface WidgetContextInterface {
6
- onConnectWallet(handler: OnConnectHandler): void;
6
+ /**
7
+ * A wallet connection handler, utilized within the wallet provider,
8
+ * is linked to the useBootstrap hook for synchronizing the state of the last connected wallet.
9
+ * It's important not to override this handler in other locations.
10
+ */
11
+ onConnectWallet(handler: OnWalletConnectionChange): void;
12
+ /**
13
+ * A wallet disconnection handler, utilized within the wallet provider,
14
+ * is linked to the useBootstrap hook for synchronizing the state of the last disconnected wallet.
15
+ * It's important not to override this handler in other locations.
16
+ */
17
+ onDisconnectWallet(handler: OnWalletConnectionChange): void;
7
18
  }
8
19
 
9
20
  export interface PropTypes {
@@ -1,19 +1,13 @@
1
1
  import type { WidgetProps } from './Widget.types';
2
2
  import type { PropsWithChildren } from 'react';
3
3
 
4
- import React, { useEffect } from 'react';
4
+ import React from 'react';
5
5
 
6
6
  import { DEFAULT_CONFIG } from '../../store/slices/config';
7
- import { tabManager } from '../../store/ui';
8
7
  import { Main } from '../App';
9
8
  import { WidgetProvider } from '../WidgetProvider';
10
9
 
11
10
  export function Widget(props: PropsWithChildren<WidgetProps>) {
12
- useEffect(() => {
13
- tabManager.init();
14
- return tabManager.destroy;
15
- }, []);
16
-
17
11
  if (!props.config?.externalWallets) {
18
12
  return (
19
13
  <WidgetProvider config={props.config ?? DEFAULT_CONFIG}>
@@ -0,0 +1 @@
1
+ export { useBootstrap } from './useBootstrap';
@@ -0,0 +1,72 @@
1
+ import type { WalletType } from '../..';
2
+
3
+ import { useQueueManager } from '@rango-dev/queue-manager-rango-preset';
4
+ import { splitWalletNetwork } from '@rango-dev/wallets-shared';
5
+ import { isEvmBlockchain } from 'rango-sdk';
6
+ import { useContext, useEffect, useState } from 'react';
7
+
8
+ import { useWallets } from '../..';
9
+ import { WidgetContext } from '../../containers/Wallets';
10
+ import { globalFont } from '../../globalStyles';
11
+ import { useAppStore } from '../../store/AppStore';
12
+ import { useNotificationStore } from '../../store/notification';
13
+ import { tabManager } from '../../store/ui';
14
+ import { useFetchApiConfig } from '../useFetchApiConfig';
15
+ import { useForceAutoConnect } from '../useForceAutoConnect';
16
+
17
+ export function useBootstrap() {
18
+ globalFont();
19
+ useForceAutoConnect();
20
+ const blockchains = useAppStore().blockchains();
21
+ const { canSwitchNetworkTo } = useWallets();
22
+ const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
23
+ useState('');
24
+ const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
25
+ const widgetContext = useContext(WidgetContext);
26
+
27
+ const evmChains = blockchains.filter(isEvmBlockchain);
28
+
29
+ const { fetchApiConfig } = useFetchApiConfig();
30
+
31
+ // At the moment, we only detect the disconnection of EVM wallets.
32
+ useQueueManager({
33
+ lastConnectedWallet: lastConnectedWalletWithNetwork,
34
+ clearDisconnectedWallet: () => {
35
+ setDisconnectedWallet(undefined);
36
+ },
37
+ disconnectedWallet: disconnectedWallet,
38
+ evmChains,
39
+ canSwitchNetworkTo,
40
+ });
41
+
42
+ useEffect(() => {
43
+ tabManager.init();
44
+
45
+ if (!useNotificationStore.persist.hasHydrated()) {
46
+ void useNotificationStore.persist.rehydrate();
47
+ }
48
+
49
+ /**
50
+ * Add handlers to the widget context for connecting and disconnecting wallets.
51
+ * We pass these handlers to the wallet provider to keep track of the last connected and last disconnected wallets.
52
+ * The 'queue-manager' uses these details to spot any changes in the connection status of the required wallet during the swap process.
53
+ *
54
+ * with this approch anyone who use the widget context can override these handlers and causes some bugs happens.
55
+ * it the future we should add a safer soloution like considering array of handlers .
56
+ * https://github.com/rango-exchange/rango-client/pull/630/files#r1518846728
57
+ */
58
+ widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork);
59
+ widgetContext.onDisconnectWallet((walletType) => {
60
+ setDisconnectedWallet(walletType);
61
+ if (
62
+ walletType === splitWalletNetwork(lastConnectedWalletWithNetwork)?.[0]
63
+ ) {
64
+ setLastConnectedWalletWithNetwork('');
65
+ }
66
+ });
67
+
68
+ void fetchApiConfig().catch(console.log);
69
+
70
+ return tabManager.destroy;
71
+ }, []);
72
+ }