Skip to content

Commit

Permalink
try to hack away toLocaleString being undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
gudnuf committed Nov 26, 2024
1 parent 6d68b16 commit f1f0eb0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/components/utility/amounts/Amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const Amount: React.FC<AmountProps> = ({
const decimalParts = val.split('.');

if (!hasDecimal) {
/* Ensure numericValue is defined before calling toLocaleString */
if (typeof numericValue !== 'number' || isNaN(numericValue)) {
return { formattedValue: '0', greyZeros: '' };
}
return { formattedValue: numericValue.toLocaleString('en-US'), greyZeros: '' };
}

Expand All @@ -49,8 +53,10 @@ const Amount: React.FC<AmountProps> = ({
}

const val = stringValue.startsWith('.') ? `0${stringValue}` : stringValue;
/* Ensure val is a valid number before calling toLocaleString */
const num = Number(val);
return {
formattedValue: val === '' ? '0' : Number(val).toLocaleString('en-US'),
formattedValue: val === '' || isNaN(num) ? '0' : num.toLocaleString('en-US'),
greyZeros: '',
};
}, [value, unit, isDollarAmount]);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/boardwalk/useBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ export const BalanceProvider: React.FC<React.PropsWithChildren> = ({ children })
if (usdBalance === null || satBalance === null) return '';

if (!showFxValue) {
return activeUnit === 'usd' ? formatUsdBalance(usdBalance) : satBalance.toLocaleString();
return activeUnit === 'usd' ? formatUsdBalance(usdBalance) : satBalance?.toLocaleString();
} else {
if (activeUnit === 'usd') {
const sats = await unitToSats(usdBalance / 100, 'usd');
return sats.toLocaleString();
return sats?.toLocaleString();
} else {
const usd = await satsToUnit(satBalance, 'usd');
return formatUsdBalance(usd);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const formatCents = (cents: number, decimals = true) => {
return `$${(cents / 100).toFixed(decimals ? 2 : 0)}`;
};

export const formatSats = (sats: number) => `${sats.toLocaleString()}₿`;
export const formatSats = (sats: number) => `${sats?.toLocaleString()}₿`;

export const shortenString = (str: string, maxLength: number) => {
if (str.length <= maxLength) {
Expand Down

0 comments on commit f1f0eb0

Please sign in to comment.