Skip to content

Commit

Permalink
feat: parse config from search params
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Aug 10, 2022
1 parent 9f8f032 commit f01f333
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export const SwapFormProvider: React.FC<React.PropsWithChildren<{}>> = ({
...formDefaultValues,
fromChain,
fromToken,
fromAmount: fromAmount?.toPrecision() ?? formDefaultValues.fromAmount,
fromAmount:
(typeof fromAmount === 'number'
? fromAmount?.toPrecision()
: fromAmount) || formDefaultValues.fromAmount,
toChain,
toToken,
},
Expand Down
55 changes: 44 additions & 11 deletions packages/widget/src/providers/WidgetProvider/WidgetProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,68 @@ export const WidgetProvider: React.FC<
fromToken,
toChain,
toToken,
fromAmount,
integrator,
...config
} = {},
}) => {
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({
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface WidgetWalletManagement {
}

interface WidgetConfigBase {
fromAmount?: number;
fromAmount?: number | string;
containerStyle?: CSSProperties;
theme?: ThemeConfig;
appearance?: Appearance;
Expand Down

0 comments on commit f01f333

Please sign in to comment.