Skip to content

Commit

Permalink
fix(evm): search params state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao committed Jun 18, 2024
1 parent 8a4ef7f commit 9fca792
Showing 1 changed file with 44 additions and 36 deletions.
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

0 comments on commit 9fca792

Please sign in to comment.