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

fix(evm): search params state handling #27

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
80 changes: 44 additions & 36 deletions apps/evm/src/pages/Bridge/Bridge.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ArrowTopRightOnSquare, Flex, H1, P, Tabs, TabsItem, XMark } from '@gobob/ui';
import { Key, useCallback, useEffect, useState } from 'react';
import { Key, useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { ChainId } from '@gobob/chains';
import { ChainId, getChainIdByChainName, getChainName } from '@gobob/chains';
import { useLocalStorage } from '@uidotdev/usehooks';

import { Main } from '../../components';
Expand All @@ -26,11 +26,28 @@ enum BridgeOrigin {

const Bridge = () => {
const location = useLocation();
const [type, setType] = useState<'deposit' | 'withdraw'>('deposit');

const [searchParams] = useSearchParams(new URLSearchParams(window.location.search));

const [type, setType] = useState<'deposit' | 'withdraw'>((searchParams.get('type') as 'deposit') || 'deposit');
const [bridgeOrigin, setBridgeOrigin] = useState<BridgeOrigin>(BridgeOrigin.INTERNAL);
const [chain, setChain] = useState<ChainId | 'BTC'>(L1_CHAIN);

const [searchParams, setSearchParams] = useSearchParams(new URLSearchParams('action=deposit'));
const initialChain = useMemo(() => {
const network = searchParams.get('network');

if (!network) {
return L1_CHAIN;
}

if (network === 'bitcoin') {
return 'BTC';
}

return getChainIdByChainName(network) || L1_CHAIN;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const [chain, setChain] = useState<ChainId | 'BTC'>(initialChain);

const navigate = useNavigate();

Expand All @@ -39,22 +56,11 @@ const Bridge = () => {
);
const isBtcOnRampEnabled = useFeatureFlag(FeatureFlags.BTC_ONRAMP);

const handleChangeTab = useCallback(
(key: any) => {
setType(key as any);
setBridgeOrigin(key === 'deposit' ? BridgeOrigin.INTERNAL : BridgeOrigin.EXTERNAL);
setChain(L1_CHAIN);

setSearchParams(() => {
const newParams = new URLSearchParams();

newParams.set('type', key as string);

return newParams;
});
},
[setSearchParams]
);
const handleChangeTab = useCallback((key: any) => {
setType(key as any);
setBridgeOrigin(key === 'deposit' ? BridgeOrigin.INTERNAL : BridgeOrigin.EXTERNAL);
setChain(L1_CHAIN);
}, []);

const handleChangeNetwork = useCallback(
(network: Key) => {
Expand All @@ -81,28 +87,30 @@ const Bridge = () => {
[]
);

const handlePressOnrampBanner = useCallback(
() => {
setChain('BTC');
setBridgeOrigin(BridgeOrigin.INTERNAL);
},
const handlePressOnrampBanner = useCallback(() => {
setChain('BTC');
setBridgeOrigin(BridgeOrigin.INTERNAL);
}, []);

useEffect(() => {
const searchParams = new URLSearchParams(window.location.search);

searchParams.set('type', type);
navigate({ search: searchParams.toString() }, { replace: true });

// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);
}, [type]);

useEffect(() => {
const paramsType = searchParams.get('type') || undefined;
const searchParams = new URLSearchParams(window.location.search);

const paramsNetwork = searchParams.get('network') || undefined;
const network = chain === 'BTC' ? 'bitcoin' : getChainName(chain);

setType(paramsType as any);
searchParams.set('network', network);
navigate({ search: searchParams.toString() }, { replace: true });

// TODO: build a mapping between possible keywords and networks
if (paramsNetwork === 'bitcoin') {
setChain('BTC');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [chain]);

return (
<Main maxWidth='5xl' padding='md'>
Expand Down