Skip to content

Commit

Permalink
bring back Max button for ERC20 tokens #5828
Browse files Browse the repository at this point in the history
  • Loading branch information
herbig committed Dec 21, 2023
1 parent 7bf484c commit 1a192ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
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}
/>
)}
</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 @@ import { Amount } from 'components/Amount/Amount'

type MaxButtonProps = {
isDisabled?: boolean
onClick: (args: number) => void
onPercentOptionClick?: (args: number) => void
onMaxClick?: () => Promise<void>
option: number
value?: number | null
Expand All @@ -18,15 +18,19 @@ const percentFormatOptions = {

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])

return (
<Button isDisabled={isDisabled} isActive={option === value} key={option} onClick={handleClick}>
Expand All @@ -41,15 +45,15 @@ export const PercentOptionsButton: React.FC<MaxButtonProps> = ({

type MaxButtonGroupProps = {
isDisabled?: boolean
onClick: (args: number) => void
onPercentOptionClick?: (args: number) => void
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 @@ export const PercentOptionsButtonGroup: React.FC<MaxButtonGroupProps> = ({
value={value}
option={option}
key={option}
onClick={onClick}
onPercentOptionClick={onPercentOptionClick}
onMaxClick={onMaxClick}
/>
))}
Expand Down
4 changes: 2 additions & 2 deletions src/components/MultiHopTrade/components/TradeAmountInput.tsx
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 AssetInputWithAsset: React.FC<AssetInputLoadedProps> = props => {
)
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

0 comments on commit 1a192ac

Please sign in to comment.