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

feat: improve transaction history number formatting #992

Merged
merged 4 commits into from
Jan 22, 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
20 changes: 17 additions & 3 deletions client/src/app/components/stardust/history/TransactionHistory.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@
background: var(--transaction-history-dark-row);
}

th,
td {
padding: 16px;
}

th {
@include font-size(12px);

text-align: center !important;
color: $gray-6;
font-weight: 600;
text-align: left;
text-align: center;
text-transform: uppercase;
}

td {
padding: 16px;
text-align: center;
width: 33%;
text-wrap: nowrap;
Expand Down Expand Up @@ -84,12 +87,23 @@
color: var(--amount-color);
@include font-size(16px, 21px);
font-weight: 700;
text-align: right;

&.negative {
color: var(--expanded-color);
}
}
}

td,
th {
&:first-child {
text-align: left;
}
&:last-child {
text-align: right;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TransactionRow: React.FC<ITransactionEntryProps> = ({
setIsFormattedAmounts,
}) => {
const valueView = (
<span className="pointer margin-r-5" onClick={() => setIsFormattedAmounts(!isFormattedAmounts)}>
<span className="pointer" onClick={() => setIsFormattedAmounts(!isFormattedAmounts)}>
{balanceChangeFormatted}
</span>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const getTransactionHistoryRecords = (
timestamp: lastOutputTime,
dateFormatted: `${DateHelper.formatShort(lastOutputTime * 1000)} (${ago})`,
balanceChange: balanceChange,
balanceChangeFormatted: (isSpent ? `-` : `+`) + formatAmount(Math.abs(balanceChange), tokenInfo, !isFormattedAmounts),
balanceChangeFormatted: (isSpent ? `-` : `+`) + formatAmount(Math.abs(balanceChange), tokenInfo, !isFormattedAmounts, 2, true),
outputs: outputs,
});
});
Expand Down
4 changes: 4 additions & 0 deletions client/src/helpers/stardust/valueFormatHelper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ test("formatAmount should format 1 unit with fraction properly", () => {
expect(formatAmount(1234567, tokenInfo)).toBe("1.23 IOTA");
});

test("formatAmount should format 1 unit with trailing decimals properly", () => {
expect(formatAmount(1000000, tokenInfo, false, 2, true)).toBe("1.00 IOTA");
});

test("formatAmount should handle edge case from issue 'explorer/issues/822'", () => {
expect(formatAmount(1140000, tokenInfo)).toBe("1.14 IOTA");
});
Expand Down
18 changes: 13 additions & 5 deletions client/src/helpers/stardust/valueFormatHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ const GENESIS_BLOCK_ID = "0x0000000000000000000000000000000000000000000000000000
* @param decimalPlaces The decimal places to show.
* @returns The formatted string.
*/
export function formatAmount(value: number, tokenInfo: INodeInfoBaseToken, formatFull: boolean = false, decimalPlaces: number = 2): string {
export function formatAmount(
value: number,
tokenInfo: INodeInfoBaseToken,
formatFull: boolean = false,
decimalPlaces: number = 2,
trailingDecimals?: boolean,
): string {
if (formatFull) {
return `${value} ${tokenInfo.subunit ?? tokenInfo.unit}`;
}

const baseTokenValue = value / Math.pow(10, tokenInfo.decimals);
const formattedAmount = toFixedNoRound(baseTokenValue, decimalPlaces);
const formattedAmount = toFixedNoRound(baseTokenValue, decimalPlaces, trailingDecimals);

// useMetricPrefix is broken cause it passes a float value to formatBest
const amount = tokenInfo.useMetricPrefix ? UnitsHelper.formatBest(baseTokenValue) : `${formattedAmount} `;
Expand All @@ -44,10 +50,12 @@ export function formatNumberWithCommas(value: bigint): string {
* @param precision The decimal places to show.
* @returns The formatted amount.
*/
function toFixedNoRound(value: number, precision: number = 2): string {
function toFixedNoRound(value: number, precision: number = 2, trailingDecimals?: boolean): string {
const defaultDecimals = "0".repeat(precision);
const valueString = `${value}`;
const [integer, fraction] = valueString.split(".");
if (!fraction) {
const [integer, fraction = defaultDecimals] = valueString.split(".");

if (fraction === defaultDecimals && !trailingDecimals) {
return valueString;
}

Expand Down
Loading