Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(widget): handle wallet referrer in widget #431

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions widget/embedded/src/components/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MemoryRouter, useInRouterContext } from 'react-router';
import { useLocation, useNavigate } from 'react-router-dom';

import { navigationRoutes } from '../constants/navigationRoutes';
import { useForceAutoConnect } from '../hooks/useForceAutoConnect';
import { Home } from '../pages/Home';
import { useAppStore } from '../store/AppStore';

Expand Down Expand Up @@ -49,6 +50,8 @@ export function AppRouter({
const blockchains = useAppStore().blockchains();
const { canSwitchNetworkTo } = useWallets();

useForceAutoConnect();

const evmChains = blockchains.filter(isEvmBlockchain);

useQueueManager({
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 @@ -57,6 +57,7 @@ export function UpdateUrl() {
toChainString = '',
toTokenString = '',
fromAmount = '';
const autoConnect = searchParamsRef.current[SearchParams.AUTO_CONNECT];
if (fetchMetaStatus !== 'success') {
fromChainString = searchParamsRef.current[SearchParams.FROM_CHAIN];
fromTokenString = searchParamsRef.current[SearchParams.FROM_TOKEN];
Expand Down Expand Up @@ -91,6 +92,9 @@ export function UpdateUrl() {
...(fromAmount && {
[SearchParams.FROM_AMOUNT]: fromAmount.toString(),
}),
...(autoConnect && {
[SearchParams.AUTO_CONNECT]: autoConnect,
}),
},
{ 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',
'AUTO_CONNECT' = 'autoConnect',
}
27 changes: 27 additions & 0 deletions widget/embedded/src/hooks/useForceAutoConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useWallets } from '@rango-dev/wallets-react';
import { useEffect, useRef } from 'react';

import { SearchParams } from '../constants/searchParams';
import { useAppStore } from '../store/AppStore';

export function useForceAutoConnect(): void {
const { connect, state } = useWallets();
const initiated = useRef<{ [key: string]: boolean }>({});
const { fetchStatus } = useAppStore();
const walletType =
new URLSearchParams(location.search).get(SearchParams.AUTO_CONNECT) || '';
const walletState = state(walletType);

useEffect(() => {
const shouldTryConnect =
fetchStatus === 'success' &&
walletType &&
walletState.installed &&
!walletState.connecting &&
!walletState.connected;
if (shouldTryConnect && !initiated.current[walletType]) {
initiated.current[walletType] = true;
void connect(walletType);
}
}, [walletState, fetchStatus]);
}