Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 134
Fix buy-sell modal error handling and UI improvements#12728
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { FixedDecimal } from '@audius/fixed-decimal' | ||
| import { z } from 'zod' | ||
| import { buySellMessages as messages } from '../../../messages' | ||
| @@ -9,7 +10,8 @@ export const createSwapFormSchema = ( | ||
| min: number = 0, | ||
| max: number = Number.MAX_SAFE_INTEGER, | ||
| balance: number | undefined = undefined, | ||
| tokenSymbol: string = '' | ||
| tokenSymbol: string = '', | ||
| decimals: number = 8 | ||
| ) => | ||
| z.object({ | ||
| inputAmount: z | ||
| @@ -18,7 +20,13 @@ export const createSwapFormSchema = ( | ||
| (val) => { | ||
| // Allow empty string during typing, but require it for final validation | ||
| if (val === '') return false | ||
| return !isNaN(parseFloat(val)) | ||
| try { | ||
| // eslint-disable-next-line no-new | ||
| new FixedDecimal(val, decimals) | ||
| return true | ||
| } catch { | ||
| return false | ||
| } | ||
| }, | ||
| { | ||
| message: messages.emptyAmount | ||
| @@ -27,7 +35,13 @@ export const createSwapFormSchema = ( | ||
| .refine( | ||
| (val) => { | ||
| if (val === '') return false | ||
| return parseFloat(val) >= min | ||
| try { | ||
| const amount = new FixedDecimal(val, decimals) | ||
| const minAmount = new FixedDecimal(min, decimals) | ||
| return amount.value >= minAmount.value | ||
| } catch { | ||
| return false | ||
| } | ||
| }, | ||
| { | ||
| message: messages.minAmount(min, tokenSymbol) | ||
| @@ -36,7 +50,13 @@ export const createSwapFormSchema = ( | ||
| .refine( | ||
| (val) => { | ||
| if (val === '') return false | ||
| return parseFloat(val) <= max | ||
| try { | ||
| const amount = new FixedDecimal(val, decimals) | ||
| const maxAmount = new FixedDecimal(max, decimals) | ||
| return amount.value <= maxAmount.value | ||
| } catch { | ||
| return false | ||
| } | ||
| }, | ||
| { | ||
| message: messages.maxAmount(max, tokenSymbol) | ||
| @@ -45,7 +65,15 @@ export const createSwapFormSchema = ( | ||
| .refine( | ||
| (val) => { | ||
| if (val === '') return false | ||
| return !balance || parseFloat(val) <= balance | ||
| // Treat only null/undefined as "unknown balance"; do not bypass check for numeric 0 | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ty for comment | ||
| if (balance == null || balance === undefined) return true | ||
| try { | ||
| const amount = new FixedDecimal(val, decimals) | ||
| const balanceAmount = new FixedDecimal(balance, decimals) | ||
| return amount.value <= balanceAmount.value | ||
| } catch { | ||
| return false | ||
| } | ||
| }, | ||
| { | ||
| message: messages.insufficientBalance(tokenSymbol) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -39,40 +39,43 @@ type CustomSingleValueProps = { | ||
| const CustomSingleValue = ( | ||
| props: SingleValueProps<TokenOption> & CustomSingleValueProps | ||
| ) => ( | ||
| <components.SingleValue {...props}> | ||
| <Flex gap='s' alignItems='center' justifyContent='space-between' w='100%'> | ||
| <Flex gap='s' alignItems='center'> | ||
| <TokenIcon tokenInfo={props.data.tokenInfo} size='2xl' hex /> | ||
| <Flex direction='column'> | ||
| {props.shouldShowLargeTicker ? ( | ||
| <Flex alignSelf='flex-start'> | ||
| <Text variant='heading' size='s' color='subdued'> | ||
| {messages.tokenTicker(props.symbol, !!props.isStablecoin)} | ||
| </Text> | ||
| </Flex> | ||
| ) : null} | ||
| {!props.hasReceiveAmount ? ( | ||
| <Text variant='title' size='s' color='default'> | ||
| {messages.stackedBalance(props.formattedAvailableBalance!)} | ||
| </Text> | ||
| ) : null} | ||
| {props.hasReceiveAmount && | ||
| props.formattedReceiveAmount !== DEFAULT_TOKEN_AMOUNT_PLACEHOLDER ? ( | ||
| <Flex direction='column'> | ||
| <Text variant='heading' size='s'> | ||
| {props.formattedReceiveAmount} | ||
| </Text> | ||
| <Text variant='title' size='s' color='subdued'> | ||
| {messages.tokenTicker(props.symbol, !!props.isStablecoin)} | ||
| ) => { | ||
| return ( | ||
| <components.SingleValue {...props}> | ||
| <Flex gap='s' alignItems='center' justifyContent='space-between' w='100%'> | ||
| <Flex gap='s' alignItems='center'> | ||
| <TokenIcon tokenInfo={props.data.tokenInfo} size='2xl' hex /> | ||
| <Flex direction='column'> | ||
| {props.shouldShowLargeTicker ? ( | ||
| <Flex alignSelf='flex-start'> | ||
| <Text variant='heading' size='s' color='subdued'> | ||
| {messages.tokenTicker(props.symbol, !!props.isStablecoin)} | ||
| </Text> | ||
| </Flex> | ||
| ) : null} | ||
| {!props.hasReceiveAmount ? ( | ||
| <Text variant='title' size='s' color='default'> | ||
| {messages.stackedBalance(props.formattedAvailableBalance!)} | ||
| </Text> | ||
| </Flex> | ||
| ) : null} | ||
| ) : null} | ||
| {props.hasReceiveAmount && | ||
| props.formattedReceiveAmount !== | ||
| DEFAULT_TOKEN_AMOUNT_PLACEHOLDER ? ( | ||
| <Flex direction='column'> | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: just | ||
| <Text variant='heading' size='s'> | ||
| {props.formattedReceiveAmount} | ||
| </Text> | ||
| <Text variant='title' size='s' color='subdued'> | ||
| {messages.tokenTicker(props.symbol, !!props.isStablecoin)} | ||
| </Text> | ||
| </Flex> | ||
| ) : null} | ||
| </Flex> | ||
| </Flex> | ||
| </Flex> | ||
| </Flex> | ||
| </components.SingleValue> | ||
| ) | ||
| </components.SingleValue> | ||
| ) | ||
| } | ||
| const CustomOption = (props: OptionProps<TokenOption>) => { | ||
| const { spacing, cornerRadius, color } = useTheme() | ||
| @@ -180,6 +183,7 @@ export const DropdownSection = ({ | ||
| ph='m' | ||
| borderRadius='s' | ||
| css={{ | ||
| maxHeight: spacing.unit16, | ||
| cursor: isClickable ? 'pointer' : 'default', | ||
| '&:hover': isClickable | ||
| ? { | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super nit: instead of eslint disable, would
_ = new FixedDecimal()work?