-
Notifications
You must be signed in to change notification settings - Fork 274
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
refactor: better TokenBalance #2068
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,33 +2,47 @@ import { formatFiatAmount } from '@/internal/utils/formatFiatAmount'; | |
import { truncateDecimalPlaces } from '@/internal/utils/truncateDecimalPlaces'; | ||
import { border, cn, color, text } from '@/styles/theme'; | ||
import { TokenImage } from '@/token/components/TokenImage'; | ||
import { useCallback } from 'react'; | ||
import { useMemo } from 'react'; | ||
import { formatUnits } from 'viem'; | ||
import type { TokenBalanceProps } from '../types'; | ||
|
||
export function TokenBalance({ | ||
token, | ||
onClick, | ||
onActionPress, | ||
actionText = 'Use max', | ||
classNames, | ||
'aria-label': ariaLabel, | ||
...contentProps | ||
}: TokenBalanceProps) { | ||
if (onClick) { | ||
return ( | ||
<button | ||
type="button" | ||
onClick={() => onClick(token)} | ||
className={cn( | ||
'flex w-full items-center justify-start gap-4 px-2 py-1', | ||
classNames?.container, | ||
<div className="relative"> | ||
<button | ||
type="button" | ||
aria-label={ariaLabel ?? `${token.name} token balance`} | ||
onClick={() => onClick(token)} | ||
className={cn( | ||
'flex w-full items-center justify-start gap-4 px-2 py-1', | ||
classNames?.container, | ||
)} | ||
data-testid="ockTokenBalanceButton" | ||
> | ||
<TokenBalanceContent | ||
token={token} | ||
classNames={classNames} | ||
onActionPress={onActionPress} | ||
{...contentProps} | ||
/> | ||
</button> | ||
{onActionPress && ( | ||
<TokenBalanceActionButton | ||
actionText={actionText} | ||
onActionPress={onActionPress} | ||
className={classNames?.action} | ||
/> | ||
)} | ||
data-testid="ockTokenBalanceButton" | ||
> | ||
<TokenBalanceContent | ||
token={token} | ||
{...contentProps} | ||
classNames={classNames} | ||
/> | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
|
@@ -45,6 +59,13 @@ export function TokenBalance({ | |
{...contentProps} | ||
classNames={classNames} | ||
/> | ||
{onActionPress && ( | ||
<TokenBalanceActionButton | ||
actionText={actionText} | ||
onActionPress={onActionPress} | ||
className={classNames?.action} | ||
/> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
@@ -53,31 +74,26 @@ function TokenBalanceContent({ | |
token, | ||
subtitle, | ||
showImage = true, | ||
actionText = 'Use max', | ||
onActionPress, | ||
tokenSize = 40, | ||
classNames, | ||
}: TokenBalanceProps) { | ||
const formattedFiatValue = formatFiatAmount({ | ||
amount: token.fiatBalance, | ||
currency: 'USD', | ||
}); | ||
|
||
const formattedCryptoValue = truncateDecimalPlaces( | ||
formatUnits(BigInt(token.cryptoBalance), token.decimals), | ||
3, | ||
const formattedFiatValue = useMemo( | ||
() => | ||
formatFiatAmount({ | ||
amount: token.fiatBalance, | ||
currency: 'USD', | ||
}), | ||
[token.fiatBalance], | ||
); | ||
|
||
const handleActionPress = useCallback( | ||
( | ||
e: | ||
| React.MouseEvent<HTMLDivElement, MouseEvent> | ||
| React.KeyboardEvent<HTMLDivElement>, | ||
) => { | ||
e.stopPropagation(); | ||
onActionPress?.(); | ||
}, | ||
[onActionPress], | ||
const formattedCryptoValue = useMemo( | ||
() => | ||
truncateDecimalPlaces( | ||
formatUnits(BigInt(token.cryptoBalance), token.decimals), | ||
3, | ||
), | ||
[token.cryptoBalance, token.decimals], | ||
); | ||
|
||
return ( | ||
|
@@ -107,25 +123,7 @@ function TokenBalanceContent({ | |
</span> | ||
</div> | ||
<div className="text-right"> | ||
{onActionPress ? ( | ||
<div | ||
role="button" | ||
data-testid="ockTokenBalanceAction" | ||
aria-label={actionText} | ||
onClick={handleActionPress} | ||
onKeyDown={handleActionPress} | ||
className={cn( | ||
text.label2, | ||
color.primary, | ||
border.radius, | ||
'ml-auto cursor-pointer p-0.5 font-bold', | ||
'border border-transparent hover:border-[--ock-line-primary]', | ||
classNames?.action, | ||
)} | ||
> | ||
{actionText} | ||
</div> | ||
) : ( | ||
{!onActionPress && ( | ||
<span | ||
className={cn( | ||
text.label2, | ||
|
@@ -141,3 +139,31 @@ function TokenBalanceContent({ | |
</div> | ||
); | ||
} | ||
|
||
function TokenBalanceActionButton({ | ||
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. Because this isn't used anywhere outside of TokenBalance I think it'd be better to call this 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. makes sense, i've renamed |
||
actionText, | ||
onActionPress, | ||
className, | ||
}: Pick<TokenBalanceProps, 'actionText' | 'onActionPress'> & { | ||
className?: string; | ||
}) { | ||
return ( | ||
<button | ||
type="button" | ||
data-testid="ockTokenBalanceAction" | ||
aria-label={actionText} | ||
onClick={onActionPress} | ||
className={cn( | ||
text.label2, | ||
color.primary, | ||
border.radius, | ||
'cursor-pointer p-0.5 font-bold', | ||
'border border-transparent hover:border-[--ock-line-primary]', | ||
'-translate-y-1/2 absolute top-1/2 right-2', | ||
className, | ||
)} | ||
> | ||
{actionText} | ||
</button> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -130,12 +130,10 @@ export type TokenBalanceProps = { | |||||||||
token: PortfolioTokenWithFiatValue; | ||||||||||
/** Subtitle to display next to the token name (eg. "available") */ | ||||||||||
subtitle?: string; | ||||||||||
/** Show the token image (default: true) */ | ||||||||||
showImage?: boolean; | ||||||||||
/** Click handler for the whole component*/ | ||||||||||
onClick?: (token: PortfolioTokenWithFiatValue) => void; | ||||||||||
/** Size of the token image in px (default: 40) */ | ||||||||||
tokenSize?: number; | ||||||||||
/** Optional aria label for the component */ | ||||||||||
'aria-label'?: string; | ||||||||||
/** Optional additional CSS classes to apply to the component */ | ||||||||||
classNames?: { | ||||||||||
container?: string; | ||||||||||
|
@@ -146,13 +144,29 @@ export type TokenBalanceProps = { | |||||||||
}; | ||||||||||
} & ( | ||||||||||
| { | ||||||||||
/** Hide the action button (default)*/ | ||||||||||
actionText?: never; | ||||||||||
onActionPress?: never; | ||||||||||
/** Show the token image (default: true) */ | ||||||||||
showImage?: true; | ||||||||||
/** Size of the token image in px (default: 40) */ | ||||||||||
tokenSize?: number; | ||||||||||
} | ||||||||||
| { | ||||||||||
/** Show an additional action button (eg. "Use max") */ | ||||||||||
actionText?: string; | ||||||||||
onActionPress: () => void; | ||||||||||
/** Hide the token image */ | ||||||||||
showImage: false; | ||||||||||
Comment on lines
+153
to
+154
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:
Suggested change
|
||||||||||
/** Size of the token image in px (default: 40) */ | ||||||||||
tokenSize?: never; | ||||||||||
} | ||||||||||
); | ||||||||||
) & | ||||||||||
( | ||||||||||
| { | ||||||||||
/** Hide the action button (default)*/ | ||||||||||
onActionPress?: never; | ||||||||||
Comment on lines
+161
to
+162
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. What's the default here? The code comment looks unfinished |
||||||||||
/** Optional action button text (default: "Use max") */ | ||||||||||
actionText?: never; | ||||||||||
} | ||||||||||
| { | ||||||||||
/** Action handler for the action button */ | ||||||||||
onActionPress: () => void; | ||||||||||
/** Optional action button text (default: "Use max") */ | ||||||||||
actionText?: string; | ||||||||||
} | ||||||||||
); |
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.
swap is just "max", should this match?