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

✨ Add max button to set max balance #1181

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 8 additions & 9 deletions packages/profile/src/components/inventory/token/helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
export const formatBalance = (balance: string) => {
export const formatBalance = (balance: string, exludes?: string[]) => {
// Catch prefix until number
let prefix = "";
for (const char of balance) {
if (!isNaN(parseInt(char))) {
break;
}
prefix += char;
}
return `${prefix}${parseFloat(balance.replace(prefix, "")).toLocaleString()}`;
const prefix = balance.slice(0, balance.search(/\d/));
// Exclude each substring from prefix
const cleaned =
exludes?.reduce((prev, curr) => prev.replace(curr, ""), prefix) ?? prefix;
return `${cleaned}${parseFloat(
balance.replace(prefix, ""),
).toLocaleString()}`;
};
32 changes: 25 additions & 7 deletions packages/profile/src/components/inventory/token/send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ export function SendToken() {
},
});

const handleMax = useCallback(
(
e: React.MouseEvent<HTMLDivElement> | React.MouseEvent<HTMLButtonElement>,
) => {
e.preventDefault();
if (!t) return;
form.setValue("amount", parseFloat(t.balance.formatted));
},
[t, form],
);

const onSubmit = useCallback(
async (values: z.infer<typeof formSchema>) => {
if (!t) return;
Expand Down Expand Up @@ -165,14 +176,12 @@ export function SendToken() {
<FormLabel>Balance:</FormLabel>
<div
className="text-xs cursor-pointer hover:opacity-90"
onClick={() =>
form.setValue(
"amount",
parseFloat(t.balance.formatted),
)
onClick={(e: React.MouseEvent<HTMLDivElement>) =>
handleMax(e)
}
>
{formatBalance(t.balance.formatted)} {t.meta.symbol}
{formatBalance(t.balance.formatted, ["~"])}{" "}
{t.meta.symbol}
</div>
</div>
</div>
Expand All @@ -186,10 +195,19 @@ export function SendToken() {
className="[&::-webkit-inner-spin-button]:appearance-none"
/>
{countervalue && (
<span className="absolute right-4 top-3.5 text-sm text-muted-foreground">
<span className="absolute right-14 top-3.5 text-sm text-muted-foreground">
{formatBalance(countervalue.formatted)}
</span>
)}
<Button
className="absolute right-2 top-1/2 -translate-y-1/2 text-xs/3 font-bold uppercase px-2 py-1.5 h-7 bg-muted text-secondary-foreground hover:opacity-70"
variant="ghost"
onClick={(e: React.MouseEvent<HTMLButtonElement>) =>
handleMax(e)
}
>
Max
</Button>
</div>
</FormControl>
<FormMessage />
Expand Down
2 changes: 1 addition & 1 deletion packages/profile/src/components/inventory/token/token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function ERC20() {
t.balance === undefined ? (
<Skeleton className="h-[20px] w-[120px] rounded" />
) : (
formatBalance(t.balance.formatted)
formatBalance(t.balance.formatted, ["~"])
)
} ${t.meta.symbol}`}
description={
Expand Down
12 changes: 7 additions & 5 deletions packages/profile/src/components/inventory/token/tokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ function TokenCardContent({
onMouseLeave={() => setHover(false)}
>
<div className="bg-secondary flex w-11 aspect-square items-center justify-center">
<img
src={token.meta.logoUrl ?? "/public/placeholder.svg"}
className="w-5 h-5"
/>
<div className="flex items-center justify-center rounded-full overflow-hidden h-7 w-7 bg-quaternary">
<img
src={token.meta.logoUrl ?? "/public/placeholder.svg"}
className="w-6 h-6"
/>
</div>
</div>

<div className="bg-secondary flex flex-1 gap-x-1.5 items-center justify-between p-3 text-medium">
<div className="flex items-center gap-2">
<p>{formatBalance(token.balance.formatted)}</p>
<p>{formatBalance(token.balance.formatted, ["~"])}</p>
<span className="text-muted-foreground">{token.meta.symbol}</span>
</div>

Expand Down
Loading