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

Resolved : Rendering big numbers in a better way #931

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 10 additions & 3 deletions components/dashboard/PortfolioSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type PortfolioSummaryProps = {
isProtocol: boolean,
minSlicePercentage?: number
}
// Utility function to format currency
const formatCurrency = (amount) => {
return `$${amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`;
};
Comment on lines +22 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance the formatCurrency utility function for better robustness

While the function handles basic currency formatting, it needs improvements for production use:

-const formatCurrency = (amount) => {
+const formatCurrency = (amount: number): string => {
+  if (!Number.isFinite(amount)) {
+    return '$0';
+  }
+  // Handle numbers > 1e16 with scientific notation
+  if (Math.abs(amount) >= 1e16) {
+    return `$${amount.toExponential(2)}`;
+  }
+  // Format with 2 decimal places and thousand separators
+  return `$${amount.toLocaleString('en-US', {
+    minimumFractionDigits: 2,
+    maximumFractionDigits: 2
+  })}`;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Utility function to format currency
const formatCurrency = (amount) => {
return `$${amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`;
};
// Utility function to format currency
const formatCurrency = (amount: number): string => {
if (!Number.isFinite(amount)) {
return '$0';
}
// Handle numbers > 1e16 with scientific notation
if (Math.abs(amount) >= 1e16) {
return `$${amount.toExponential(2)}`;
}
// Format with 2 decimal places and thousand separators
return `$${amount.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})}`;
};



const ChartEntry: FunctionComponent<ChartItem> = ({
Expand All @@ -27,7 +31,10 @@ const ChartEntry: FunctionComponent<ChartItem> = ({
itemValue,
itemValueSymbol
}) => {
const value = itemValueSymbol === '%' ? itemValue + itemValueSymbol : itemValueSymbol + itemValue;
const formattedValue = itemValueSymbol === '%'
? `${itemValue}${itemValueSymbol}`
: `${itemValueSymbol}${formatCurrency(itemValue)}`;

Comment on lines +34 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix undefined variable reference and standardize string interpolation

There's a critical error in the ChartEntry component where an undefined 'value' variable is being rendered.

 const formattedValue = itemValueSymbol === '%' 
-    ? `${itemValue}${itemValueSymbol}` 
-    : `${itemValueSymbol}${formatCurrency(itemValue)}`;
+    ? `${itemValue}${itemValueSymbol}` 
+    : formatCurrency(itemValue);

-      <Typography type={TEXT_TYPE.BODY_MIDDLE}>{value}</Typography>
+      <Typography type={TEXT_TYPE.BODY_MIDDLE}>{formattedValue}</Typography>

Also applies to: 47-47

return (
<div className="flex w-full justify-between my-1">
<div className="flex flex-row w-full items-center gap-2">
Expand Down Expand Up @@ -71,7 +78,7 @@ const PortfolioSummary: FunctionComponent<PortfolioSummaryProps> = ({ title, dat
tooltip: {
callbacks: {
label: function (tooltipItem: TooltipItem<"doughnut">) {
return ` ${data[tooltipItem.dataIndex].itemValueSymbol}${data[tooltipItem.dataIndex].itemValue}`;
return ` ${item.itemValueSymbol}${formatCurrency(item.itemValue)}`;
}
}
}
Expand Down Expand Up @@ -138,4 +145,4 @@ const PortfolioSummary: FunctionComponent<PortfolioSummaryProps> = ({ title, dat
) : null;
}

export default PortfolioSummary;
export default PortfolioSummary;