From f01f3338445b9964fb1b2f57605a94156ee1b63d Mon Sep 17 00:00:00 2001 From: Eugene Chybisov Date: Wed, 10 Aug 2022 18:28:03 +0100 Subject: [PATCH] feat: parse config from search params --- .../SwapFormProvider/SwapFormProvider.tsx | 5 +- .../WidgetProvider/WidgetProvider.tsx | 55 +++++++++++++++---- packages/widget/src/types/widget.ts | 2 +- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx b/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx index 0219ef851..165261960 100644 --- a/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx +++ b/packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx @@ -18,7 +18,10 @@ export const SwapFormProvider: React.FC> = ({ ...formDefaultValues, fromChain, fromToken, - fromAmount: fromAmount?.toPrecision() ?? formDefaultValues.fromAmount, + fromAmount: + (typeof fromAmount === 'number' + ? fromAmount?.toPrecision() + : fromAmount) || formDefaultValues.fromAmount, toChain, toToken, }, diff --git a/packages/widget/src/providers/WidgetProvider/WidgetProvider.tsx b/packages/widget/src/providers/WidgetProvider/WidgetProvider.tsx index 41092b02b..01eee2124 100644 --- a/packages/widget/src/providers/WidgetProvider/WidgetProvider.tsx +++ b/packages/widget/src/providers/WidgetProvider/WidgetProvider.tsx @@ -26,6 +26,7 @@ export const WidgetProvider: React.FC< fromToken, toChain, toToken, + fromAmount, integrator, ...config } = {}, @@ -33,28 +34,60 @@ export const WidgetProvider: React.FC< const { account } = useWallet(); const value = useMemo((): WidgetContextProps => { try { + const searchParams = Object.fromEntries( + new URLSearchParams(window?.location.search), + ); return { ...config, fromChain: - typeof fromChain === 'number' - ? fromChain - : typeof fromChain === 'string' - ? getChainByKey(fromChain.toLowerCase() as ChainKey).id + (searchParams.fromChain && + isNaN(parseInt(searchParams.fromChain, 10))) || + typeof fromChain === 'string' + ? getChainByKey( + ( + searchParams.fromChain || (fromChain as string) + )?.toLowerCase() as ChainKey, + ).id + : (searchParams.fromChain && + !isNaN(parseInt(searchParams.fromChain, 10))) || + typeof fromChain === 'number' + ? parseInt(searchParams.fromChain, 10) || fromChain : account.chainId ?? ChainId.ETH, toChain: - typeof toChain === 'number' - ? toChain - : typeof toChain === 'string' - ? getChainByKey(toChain.toLowerCase() as ChainKey).id + (searchParams.toChain && isNaN(parseInt(searchParams.toChain, 10))) || + typeof toChain === 'string' + ? getChainByKey( + ( + searchParams.toChain || (toChain as string) + )?.toLowerCase() as ChainKey, + ).id + : (searchParams.toChain && + !isNaN(parseInt(searchParams.toChain, 10))) || + typeof toChain === 'number' + ? parseInt(searchParams.toChain, 10) || toChain : ChainId.ETH, - fromToken: fromToken?.toLowerCase(), - toToken: toToken?.toLowerCase(), + fromToken: + searchParams.fromToken?.toLowerCase() || fromToken?.toLowerCase(), + toToken: searchParams.toToken?.toLowerCase() || toToken?.toLowerCase(), + fromAmount: + typeof searchParams.fromAmount === 'string' && + !isNaN(parseFloat(searchParams.fromAmount)) + ? searchParams.fromAmount + : fromAmount, } as WidgetContextProps; } catch (e) { console.warn(e); return config; } - }, [account.chainId, config, fromChain, fromToken, toChain, toToken]); + }, [ + account.chainId, + config, + fromAmount, + fromChain, + fromToken, + toChain, + toToken, + ]); useEffect(() => { updateLiFiConfig({ diff --git a/packages/widget/src/types/widget.ts b/packages/widget/src/types/widget.ts index 412b71028..52bd4e050 100644 --- a/packages/widget/src/types/widget.ts +++ b/packages/widget/src/types/widget.ts @@ -21,7 +21,7 @@ export interface WidgetWalletManagement { } interface WidgetConfigBase { - fromAmount?: number; + fromAmount?: number | string; containerStyle?: CSSProperties; theme?: ThemeConfig; appearance?: Appearance;