Skip to content

Commit

Permalink
fix: clean SwapProps and standardize event handler logic (#177)
Browse files Browse the repository at this point in the history
* refactor: standardize conditional handler logic

* fix: swap props
  • Loading branch information
zzmp authored Aug 30, 2022
1 parent d6ce6d5 commit 0d74a8c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/components/ConnectWallet/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Trans } from '@lingui/macro'
import { useConditionalHandler } from 'hooks/useConditionalHandler'
import { Wallet as WalletIcon } from 'icons'
import { useAtomValue } from 'jotai/utils'
import { useCallback, useState } from 'react'
Expand All @@ -25,10 +26,9 @@ export default function ConnectWallet({ disabled }: ConnectWalletProps) {
const [open, setOpen] = useState(false)
const onClose = () => setOpen(false)

const onConnectWalletClick = useAtomValue(onConnectWalletClickAtom)
const onConnectWalletClick = useConditionalHandler(useAtomValue(onConnectWalletClickAtom))
const onClick = useCallback(async () => {
const open = await Promise.resolve(onConnectWalletClick?.()).catch(() => false)
setOpen(open ?? true)
setOpen(await onConnectWalletClick())
}, [onConnectWalletClick])

return (
Expand Down
6 changes: 3 additions & 3 deletions src/components/Swap/SwapButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useSwapApprovalOptimizedTrade } from 'hooks/swap/useSwapApproval'
import { useSwapCallback } from 'hooks/swap/useSwapCallback'
import useWrapCallback from 'hooks/swap/useWrapCallback'
import { useAddTransactionInfo } from 'hooks/transactions'
import { useConditionalHandler } from 'hooks/useConditionalHandler'
import { useSetOldestValidBlock } from 'hooks/useIsValidBlock'
import useNativeCurrency from 'hooks/useNativeCurrency'
import useTransactionDeadline from 'hooks/useTransactionDeadline'
Expand Down Expand Up @@ -154,7 +155,7 @@ export default memo(function SwapButton({ disabled }: SwapButtonProps) {
inputCurrencyBalance.lessThan(inputCurrencyAmount),
[disabled, isWrap, chainId, optimizedTrade, inputCurrencyAmount, inputCurrencyBalance]
)
const { onReviewSwapClick } = useAtomValue(swapEventHandlersAtom)
const onReviewSwapClick = useConditionalHandler(useAtomValue(swapEventHandlersAtom).onReviewSwapClick)
const actionProps = useMemo((): Partial<ActionButtonProps> | undefined => {
if (disableSwap) {
return { disabled: true }
Expand All @@ -168,8 +169,7 @@ export default memo(function SwapButton({ disabled }: SwapButtonProps) {
: trade.state === TradeState.VALID
? {
onClick: async () => {
const open = await Promise.resolve(onReviewSwapClick?.())?.catch(() => false)
setOpen(open ?? true)
setOpen(await onReviewSwapClick())
},
}
: { disabled: true }
Expand Down
8 changes: 1 addition & 7 deletions src/components/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ import useValidate from './useValidate'
// since the wallet connection component exists within the Swap component.
// This includes useSyncWidgetEventHandlers.
// TODO(zzmp): refactor WalletConnection outside of Swap component
export interface SwapProps
extends BrandingSettings,
FeeOptions,
SwapController,
SwapEventHandlers,
TokenDefaults,
WidgetEventHandlers {
export interface SwapProps extends BrandingSettings, FeeOptions, SwapEventHandlers, TokenDefaults, WidgetEventHandlers {
hideConnectionUI?: boolean
provider?: Eip1193Provider | JsonRpcProvider
routerUrl?: string
Expand Down
6 changes: 3 additions & 3 deletions src/components/TokenSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { t, Trans } from '@lingui/macro'
import { Currency } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core'
import { useConditionalHandler } from 'hooks/useConditionalHandler'
import { useCurrencyBalances } from 'hooks/useCurrencyBalance'
import useNativeCurrency from 'hooks/useNativeCurrency'
import useTokenList, { useIsTokenListLoaded, useQueryTokens } from 'hooks/useTokenList'
Expand Down Expand Up @@ -131,10 +132,9 @@ export default memo(function TokenSelect({ collapsed, disabled, field, onSelect,
usePrefetchBalances()

const [open, setOpen] = useState(false)
const { onTokenSelectorClick } = useAtomValue(swapEventHandlersAtom)
const onTokenSelectorClick = useConditionalHandler(useAtomValue(swapEventHandlersAtom).onTokenSelectorClick)
const onOpen = useCallback(async () => {
const open = await Promise.resolve(onTokenSelectorClick?.(field)).catch(() => false)
setOpen(open ?? true)
setOpen(await onTokenSelectorClick(field))
}, [field, onTokenSelectorClick])
const selectAndClose = useCallback(
(value: Currency) => {
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useConditionalHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCallback } from 'react'

export function useConditionalHandler<T extends (...args: any[]) => void | boolean | Promise<boolean>>(
handler?: T
): (...params: Parameters<T>) => Promise<boolean> {
return useCallback(
async (...params) => {
if (!handler) return true

try {
const result = await handler(...params)
if (result === false) return false
} catch {
return false
}
return true
},
[handler]
)
}

1 comment on commit 0d74a8c

@vercel
Copy link

@vercel vercel bot commented on 0d74a8c Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

widgets – ./

widgets-git-main-uniswap.vercel.app
widgets-seven-tau.vercel.app
widgets-uniswap.vercel.app

Please sign in to comment.