Skip to content

Commit cc1edcf

Browse files
authored
fix(packages): implement multiple fixes to new components (#43)
* fix(packages): implement multiple fixes to new components * fix(packages): resolve ReDoS vulnerability in toKebabCase regex * fix(packages): always show counter even at 1/1 * fix(packages): remove regex * fix(packages): improve FinalityProviderItem
1 parent be4fb70 commit cc1edcf

File tree

13 files changed

+119
-100
lines changed

13 files changed

+119
-100
lines changed

packages/babylon-core-ui/src/components/CounterButton/CounterButton.stories.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export const AlmostAtMax: Story = {
3838

3939
export const AtMaxCapacity: Story = {
4040
args: {
41-
counter: 5,
42-
max: 5,
41+
counter: 1,
42+
max: 1,
4343
},
4444
};
4545

@@ -49,26 +49,4 @@ export const AlwaysShowCounter: Story = {
4949
max: 3,
5050
alwaysShowCounter: true,
5151
},
52-
};
53-
54-
export const AlwaysShowCounterWithValue: Story = {
55-
args: {
56-
counter: 1,
57-
max: 3,
58-
alwaysShowCounter: true,
59-
},
60-
};
61-
62-
export const SingleMax: Story = {
63-
args: {
64-
counter: 0,
65-
max: 1,
66-
},
67-
};
68-
69-
export const SingleMaxAtCapacity: Story = {
70-
args: {
71-
counter: 1,
72-
max: 1,
73-
},
74-
};
52+
};

packages/babylon-core-ui/src/components/CounterButton/CounterButton.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import { twJoin } from "tailwind-merge";
44
interface CounterButtonProps {
55
counter: number;
66
max: number;
7-
onAdd: () => void;
87
alwaysShowCounter?: boolean;
8+
onAdd: () => void;
99
}
1010

1111
export function CounterButton({ counter, max, onAdd, alwaysShowCounter = false }: CounterButtonProps) {
1212
const isClickable = counter < max;
13-
const showsCounter = (0 < counter && 1 < max) || (alwaysShowCounter && counter === 0);
13+
const showsCounter = (0 < counter) || (alwaysShowCounter && counter === 0);
1414

1515
return (
1616
<div
1717
className={twJoin(
18-
"bg-primary-highlight flex overflow-hidden rounded-md border border-accent-primary",
18+
"bg-primary-highlight flex overflow-hidden rounded-md border border-accent-primary w-fit",
1919
isClickable && "cursor-pointer",
2020
!showsCounter && !isClickable && "hidden",
2121
!showsCounter && "w-10",
@@ -28,7 +28,10 @@ export function CounterButton({ counter, max, onAdd, alwaysShowCounter = false }
2828
</div>
2929
)}
3030
{showsCounter && (
31-
<div className="flex h-10 items-center border-l border-accent-primary px-2 text-sm sm:px-4 sm:text-base">
31+
<div className={twJoin(
32+
"flex h-10 items-center px-2 text-sm sm:px-4 sm:text-base",
33+
isClickable && "border-l border-accent-primary"
34+
)}>
3235
{counter}/{max}
3336
</div>
3437
)}

packages/babylon-core-ui/src/components/SubSection/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/babylon-core-ui/src/elements/FinalityProviderItem/FinalityProviderItem.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,22 @@ export function FinalityProviderItem({ bsnId, bsnName, bsnLogoUrl, address, prov
2525
if (!provider) return null;
2626

2727
const renderBsnLogo = () => {
28-
if (!bsnLogoUrl) return null;
28+
if (bsnLogoUrl) {
29+
return <Avatar url={bsnLogoUrl} alt={bsnName} variant="rounded" size="tiny" className="mr-1" />;
30+
}
31+
32+
const placeholderLetter = bsnName?.charAt(0).toUpperCase() || "?";
2933

30-
return <Avatar url={bsnLogoUrl} alt={bsnName} variant="rounded" size="tiny" className="mr-1" />;
34+
return (
35+
<Avatar variant="rounded" size="tiny" className="mr-1">
36+
<Text
37+
as="span"
38+
className="inline-flex h-full w-full items-center justify-center bg-secondary-main text-xs text-accent-contrast"
39+
>
40+
{placeholderLetter}
41+
</Text>
42+
</Avatar>
43+
);
3144
};
3245

3346
const shortenAddress = (value: string): string => {
@@ -54,12 +67,14 @@ export function FinalityProviderItem({ bsnId, bsnName, bsnLogoUrl, address, prov
5467
return (
5568
<div className="flex flex-row items-center justify-between">
5669
<div className="flex h-10 flex-row gap-2">
57-
<FinalityProviderLogo
58-
logoUrl={provider.logo_url}
59-
rank={provider.rank}
60-
moniker={provider.description?.moniker}
61-
size="lg"
62-
/>
70+
<div className="shrink-0">
71+
<FinalityProviderLogo
72+
logoUrl={provider.logo_url}
73+
rank={provider.rank}
74+
moniker={provider.description?.moniker}
75+
size="lg"
76+
/>
77+
</div>
6378
<div className="flex flex-col justify-center text-accent-primary">
6479
{renderChainOrAddress()}
6580
<Text as="div" className="text-base font-medium text-accent-primary">
@@ -70,7 +85,7 @@ export function FinalityProviderItem({ bsnId, bsnName, bsnLogoUrl, address, prov
7085
{onRemove ?
7186
<button
7287
onClick={() => onRemove(bsnId)}
73-
className="cursor-pointer rounded bg-accent-secondary/20 px-2 py-0.5 text-xs tracking-[0.4px] text-accent-primary"
88+
className="ml-[10px] cursor-pointer rounded bg-accent-secondary/20 px-2 py-0.5 text-xs tracking-[0.4px] text-accent-primary"
7489
>
7590
Remove
7691
</button> : null}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const BTC_DECIMAL_PLACES = 8;
2+
export const BBN_DECIMAL_PLACES = 5;

packages/babylon-core-ui/src/widgets/sections/AmountSubsection/AmountSubsection.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SubSection } from "@/components/SubSection";
33
import { useFormContext, useWatch } from "react-hook-form";
44

55
import { calculateTokenValueInCurrency, maxDecimals } from "@/utils/helpers";
6+
import { BTC_DECIMAL_PLACES } from "@/utils/constants";
67

78
interface BalanceDetails {
89
balance: number | string;
@@ -73,7 +74,7 @@ export const AmountSubsection = ({
7374
onKeyDown={handleKeyDown}
7475
placeholder={placeholder}
7576
autoFocus={autoFocus}
76-
className="w-2/3 bg-transparent text-right text-lg outline-none"
77+
className="w-2/3 bg-transparent text-right text-lg outline-none appearance-none [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
7778
/>
7879
</div>
7980
<HiddenField name={fieldName} defaultValue="" />
@@ -83,7 +84,7 @@ export const AmountSubsection = ({
8384
<div>
8485
Stakable:{" "}
8586
<span className="cursor-default">
86-
{maxDecimals(Number(balanceDetails.balance), balanceDetails.decimals ?? 8)}
87+
{maxDecimals(Number(balanceDetails.balance), balanceDetails.decimals ?? BTC_DECIMAL_PLACES)}
8788
</span>{" "}
8889
{balanceDetails.symbol}
8990
</div>

packages/babylon-core-ui/src/widgets/sections/FeesSection/BBNFeeAmount.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FeeItem } from "./FeeItem";
2+
import { BBN_DECIMAL_PLACES } from "../../../utils/constants";
23

34
interface BBNFeeAmountProps {
45
amount: number | string;
@@ -9,7 +10,7 @@ interface BBNFeeAmountProps {
910
decimals?: number;
1011
}
1112

12-
export function BBNFeeAmount({ amount, coinSymbol, hint, title, className, decimals = 5 }: BBNFeeAmountProps) {
13+
export function BBNFeeAmount({ amount, coinSymbol, hint, title, className, decimals = BBN_DECIMAL_PLACES }: BBNFeeAmountProps) {
1314
const formattedAmount = typeof amount === "number" ? amount.toFixed(decimals) : amount;
1415

1516
return (

packages/babylon-core-ui/src/widgets/sections/FeesSection/BTCFeeAmount.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FeeItem } from "./FeeItem";
2+
import { BTC_DECIMAL_PLACES } from "../../../utils/constants";
23

34
interface BTCFeeAmountProps {
45
amount: number | string;
@@ -9,16 +10,19 @@ interface BTCFeeAmountProps {
910
decimals?: number;
1011
}
1112

12-
export function BTCFeeAmount({ amount, coinSymbol, hint, title, className, decimals = 8 }: BTCFeeAmountProps) {
13-
const formattedAmount =
14-
typeof amount === "number"
15-
? amount === 0
16-
? "0"
17-
: (() => {
18-
const str = amount.toFixed(decimals);
19-
return str.replace(/0+$/, "").replace(/\.$/, "");
20-
})()
21-
: amount;
13+
export function BTCFeeAmount({ amount, coinSymbol, hint, title, className, decimals = BTC_DECIMAL_PLACES }: BTCFeeAmountProps) {
14+
let formattedAmount: string;
15+
16+
if (typeof amount === "number") {
17+
if (amount === 0) {
18+
formattedAmount = "0";
19+
} else {
20+
const str = amount.toFixed(decimals);
21+
formattedAmount = str.replace(/0+$/, "").replace(/\.$/, "");
22+
}
23+
} else {
24+
formattedAmount = amount;
25+
}
2226

2327
return (
2428
<FeeItem title={title ?? `${coinSymbol} Network Fee`} hint={hint} className={className}>

packages/babylon-core-ui/src/widgets/sections/FeesSection/FeeItem.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ export function FeeItem({ title, children, className, hint }: FeeItemProps) {
2121
{title}
2222
</Text>
2323

24-
{!hint ? (
25-
<Text as="div" className="flex items-center gap-2">
26-
{children}
27-
</Text>
28-
) : (
24+
{hint ? (
2925
<div className="flex flex-col items-end">
3026
<Text as="div" className="flex items-center gap-2">
3127
{children}
@@ -34,6 +30,10 @@ export function FeeItem({ title, children, className, hint }: FeeItemProps) {
3430
{hint}
3531
</Text>
3632
</div>
33+
) : (
34+
<Text as="div" className="flex items-center gap-2">
35+
{children}
36+
</Text>
3737
)}
3838
</div>
3939
);

packages/babylon-core-ui/src/widgets/sections/FeesSection/FeesSection.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export function FeesSection({
3030
feeAmountHint,
3131
total,
3232
totalHint,
33-
3433
bbnFeeAmount,
3534
bbnCoinSymbol,
3635
bbnFeeAmountHint,

0 commit comments

Comments
 (0)