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: swap max for non native assets #5923

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/components/DeFi/components/AssetInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const AssetInput: React.FC<AssetInputProps> = ({
options={percentOptions}
isDisabled={isReadOnly || isSendMaxDisabled}
onMaxClick={onMaxClick}
onClick={onPercentOptionClick}
onPercentOptionClick={onPercentOptionClick}
herbig marked this conversation as resolved.
Show resolved Hide resolved
/>
)}
</Stack>
Expand Down
18 changes: 11 additions & 7 deletions src/components/DeFi/components/PercentOptionsButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

type MaxButtonProps = {
isDisabled?: boolean
onClick: (args: number) => void
onPercentOptionClick?: (args: number) => void
herbig marked this conversation as resolved.
Show resolved Hide resolved
onMaxClick?: () => Promise<void>
option: number
value?: number | null
Expand All @@ -18,15 +18,19 @@

export const PercentOptionsButton: React.FC<MaxButtonProps> = ({
isDisabled,
onClick,
onPercentOptionClick,
onMaxClick,
option,
value,
}) => {
const translate = useTranslate()
const handleClick = useCallback(async () => {
onMaxClick && option === 1 ? await onMaxClick() : onClick(option)
}, [onClick, onMaxClick, option])
if (onMaxClick && option === 1) {
await onMaxClick()
} else if (onPercentOptionClick) {

Check failure on line 30 in src/components/DeFi/components/PercentOptionsButtonGroup.tsx

View workflow job for this annotation

GitHub Actions / Call / Static

Delete `·`
onPercentOptionClick(option)
}
}, [onPercentOptionClick, onMaxClick, option])
Copy link
Author

Choose a reason for hiding this comment

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

changed the logic slightly, since onPercentOptionClick is now optional

Copy link
Contributor

Choose a reason for hiding this comment

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

Since both are now optional, we could end up in a situation where we forget to pass onMaxClick, and the regular onClick handler gets fired, which could have very different logic as the max one


return (
<Button isDisabled={isDisabled} isActive={option === value} key={option} onClick={handleClick}>
Expand All @@ -41,15 +45,15 @@

type MaxButtonGroupProps = {
isDisabled?: boolean
onClick: (args: number) => void
onPercentOptionClick?: (args: number) => void
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is a shared component, we absolutely want a discriminated union here - one or the other should always be present: if we ever get to rendering this component, we should have an event handler for it.

onMaxClick?: () => Promise<void>
options: number[]
value?: number | null
}

export const PercentOptionsButtonGroup: React.FC<MaxButtonGroupProps> = ({
isDisabled,
onClick,
onPercentOptionClick,
onMaxClick,
options,
value,
Expand All @@ -62,7 +66,7 @@
value={value}
option={option}
key={option}
onClick={onClick}
onPercentOptionClick={onPercentOptionClick}
onMaxClick={onMaxClick}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ export const TradeAmountInput: React.FC<TradeAmountInputProps> = memo(
<Skeleton isLoaded={!showFiatSkeleton}>{oppositeCurrency}</Skeleton>
</Button>
)}
{onPercentOptionClick && (
{(onMaxClick || onPercentOptionClick) && (
<PercentOptionsButtonGroup
options={percentOptions}
isDisabled={isReadOnly || isSendMaxDisabled}
onMaxClick={onMaxClick}
onClick={onPercentOptionClick}
onPercentOptionClick={onPercentOptionClick}
/>
)}
</Flex>
Expand Down
19 changes: 16 additions & 3 deletions src/components/MultiHopTrade/components/TradeAssetInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Skeleton, SkeletonCircle, Stack, useColorModeValue } from '@chakra-ui/react'

Check failure on line 1 in src/components/MultiHopTrade/components/TradeAssetInput.tsx

View workflow job for this annotation

GitHub Actions / Call / Static

Run autofix to sort these imports!
import type { AssetId } from '@shapeshiftoss/caip'
import React, { memo, useMemo } from 'react'
import { fromAssetId, type AssetId } from '@shapeshiftoss/caip'
import React, { memo, useCallback, useMemo } from 'react'
import type { TradeAmountInputProps } from 'components/MultiHopTrade/components/TradeAmountInput'
import { TradeAmountInput } from 'components/MultiHopTrade/components/TradeAmountInput'
import { bnOrZero } from 'lib/bignumber/bignumber'
Expand Down Expand Up @@ -46,7 +46,20 @@
)
const fiatBalance = bnOrZero(balance).times(marketData.price).toString()

return <TradeAmountInput balance={balance} fiatBalance={fiatBalance} {...props} />
const onMaxClick = useCallback(async () => {

Check failure on line 49 in src/components/MultiHopTrade/components/TradeAssetInput.tsx

View workflow job for this annotation

GitHub Actions / Call / Static

Async arrow function has no 'await' expression
if (props.onChange) props.onChange(balance, false)
}, [balance, props.onChange])

Check failure on line 51 in src/components/MultiHopTrade/components/TradeAssetInput.tsx

View workflow job for this annotation

GitHub Actions / Call / Static

React Hook useCallback has a missing dependency: 'props'. Either include it or remove the dependency array. However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the useCallback call and refer to those specific props inside useCallback

const { assetNamespace } = fromAssetId(assetId)

return (
<TradeAmountInput
balance={balance}
fiatBalance={fiatBalance}
onMaxClick={assetNamespace === 'erc20' ? onMaxClick : undefined}
{...props}
/>
)
}

export type TradeAssetInputProps = {
Expand Down
Loading