Skip to content

Commit

Permalink
fix: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasackermn committed Nov 7, 2023
1 parent 9a016dd commit 5fc13a2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 49 deletions.
12 changes: 11 additions & 1 deletion widget/embedded/src/components/AppRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WidgetConfig } from '../types';
import type { WalletType } from '@rango-dev/wallets-shared';
import type { WalletType, WalletTypes } from '@rango-dev/wallets-shared';
import type { PropsWithChildren } from 'react';

import { useQueueManager } from '@rango-dev/queue-manager-rango-preset';
Expand All @@ -10,6 +10,8 @@ import { MemoryRouter, useInRouterContext } from 'react-router';
import { useLocation, useNavigate } from 'react-router-dom';

import { navigationRoutes } from '../constants/navigationRoutes';
import { SearchParams } from '../constants/searchParams';
import { useForceAutoConnect } from '../hooks/useForceAutoConnect';
import { Home } from '../pages/Home';
import { useMetaStore } from '../store/meta';

Expand Down Expand Up @@ -50,6 +52,14 @@ export function AppRouter({
const Router = isRouterInContext ? Route : MemoryRouter;
const { blockchains } = useMetaStore.use.meta();
const { canSwitchNetworkTo } = useWallets();
const { loadingStatus } = useMetaStore();
const referrerWalletType = new URLSearchParams(location.search).get(
SearchParams.REFERRER
);
useForceAutoConnect({
walletType: referrerWalletType as WalletTypes,
loadingStatus,
});

const evmChains = blockchains.filter(isEvmBlockchain);

Expand Down
37 changes: 37 additions & 0 deletions widget/embedded/src/hooks/useForceAutoConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { MetaState } from '../store/meta';
import type { WalletTypes } from '@rango-dev/wallets-shared';

import { useWallets } from '@rango-dev/wallets-react';
import { useEffect, useRef } from 'react';

type forceAutoConnectParams = {
loadingStatus: MetaState['loadingStatus'];
walletType: WalletTypes;
};

export function useForceAutoConnect(params: forceAutoConnectParams): void {
const { loadingStatus, walletType } = params;
const { connect, state } = useWallets();
const initiated = useRef(false);

const walletState = state(walletType);

useEffect(() => {
const shouldTryConnect =
loadingStatus === 'success' &&
walletType &&
walletState &&
walletState.installed &&
!walletState.connecting &&
!walletState.connected;

if (shouldTryConnect && !initiated.current) {
initiated.current = true;
connect(walletType)
.then()
.catch((error: any) => {
console.error(error);
});
}
}, [walletState, loadingStatus]);
}
42 changes: 0 additions & 42 deletions widget/embedded/src/hooks/useReferrer.ts

This file was deleted.

6 changes: 0 additions & 6 deletions widget/embedded/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import {
USD_VALUE_MAX_DECIMALS,
USD_VALUE_MIN_DECIMALS,
} from '../constants/routing';
import { SearchParams } from '../constants/searchParams';
import { useReferrer } from '../hooks/useReferrer';
import { useSwapInput } from '../hooks/useSwapInput';
import { useBestRouteStore } from '../store/bestRoute';
import { useMetaStore } from '../store/meta';
Expand Down Expand Up @@ -118,10 +116,6 @@ export function Home() {
meta: { tokens, blockchains },
loadingStatus: loadingMetaStatus,
} = useMetaStore();
const referrer = new URLSearchParams(location.search).get(
SearchParams.REFERRER
);
useReferrer({ referrer, loadingStatus: loadingMetaStatus });

const connectedWallets = useWalletsStore.use.connectedWallets();
const setCurrentPage = useUiStore.use.setCurrentPage();
Expand Down

0 comments on commit 5fc13a2

Please sign in to comment.