Skip to content

Commit

Permalink
feat: handle wallet referrer in widget
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasackermn committed Nov 11, 2023
1 parent 5db6a46 commit ca9cb65
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions widget/embedded/src/components/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
loadingStatus,
});

const evmChains = blockchains.filter(isEvmBlockchain);

Expand Down
4 changes: 4 additions & 0 deletions widget/embedded/src/components/UpdateUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function UpdateUrl(props: Props) {
toChainString = '',
toTokenString = '',
fromAmount = '';
const referrer = searchParamsRef.current[SearchParams.REFERRER];
if (loadingStatus !== 'success') {
fromChainString = searchParamsRef.current[SearchParams.FROM_CHAIN];
fromTokenString = searchParamsRef.current[SearchParams.FROM_TOKEN];
Expand Down Expand Up @@ -94,6 +95,9 @@ export function UpdateUrl(props: Props) {
...(fromAmount && {
[SearchParams.FROM_AMOUNT]: fromAmount.toString(),
}),
...(referrer && {
[SearchParams.REFERRER]: referrer,
}),
},
{ replace: true }
);
Expand Down
1 change: 1 addition & 0 deletions widget/embedded/src/constants/searchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export enum SearchParams {
'TO_CHAIN' = 'toBlockchain',
'TO_TOKEN' = 'toToken',
'FROM_AMOUNT' = 'fromAmount',
'REFERRER' = 'referrer',
}
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 { WalletType } from '@rango-dev/wallets-shared';

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

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

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]);
}

0 comments on commit ca9cb65

Please sign in to comment.