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 trailingZeros option to ValueView #1949

Merged
merged 3 commits into from
Dec 19, 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
5 changes: 5 additions & 0 deletions .changeset/five-boxes-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/ui': minor
---

Add trailingZeros option to ValueView
17 changes: 10 additions & 7 deletions packages/ui/src/ValueView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ReactNode } from 'react';
import { ValueView } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb';
import { getMetadata } from '@penumbra-zone/getters/value-view';
import { getFormattedAmtFromValueView } from '@penumbra-zone/types/value-view';
import { ConditionalWrap } from '../ConditionalWrap';
import { Pill, PillProps } from '../Pill';
import { Text } from '../Text';
Expand All @@ -10,6 +9,7 @@ import { Density, useDensity } from '../utils/density';
import cn from 'clsx';
import { shortify } from '@penumbra-zone/types/shortify';
import { detailTechnical, technical } from '../utils/typography.ts';
import { pnum } from '@penumbra-zone/types/pnum';

type Context = 'default' | 'table';

Expand Down Expand Up @@ -47,6 +47,10 @@ export interface ValueViewComponentProps<SelectedContext extends Context> {
* If false, the amount will not be displayed.
*/
showValue?: boolean;
/**
* If true, the amount will have trailing zeros.
*/
trailingZeros?: boolean;
}

/**
Expand All @@ -63,18 +67,17 @@ export const ValueViewComponent = <SelectedContext extends Context = 'default'>(
showSymbol = true,
abbreviate = false,
showValue = true,
trailingZeros = false,
}: ValueViewComponentProps<SelectedContext>) => {
const density = useDensity();

if (!valueView) {
return null;
}

let formattedAmount: string | undefined;
if (showValue) {
const amount = getFormattedAmtFromValueView(valueView, !abbreviate);
formattedAmount = abbreviate ? shortify(Number(amount)) : amount;
}
const formattedAmount = abbreviate
? shortify(pnum(valueView).toNumber())
: pnum(valueView).toFormattedString({ trailingZeros });

const metadata = getMetadata.optional(valueView);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- possibly empty string
Expand Down Expand Up @@ -113,7 +116,7 @@ export const ValueViewComponent = <SelectedContext extends Context = 'default'>(
)}
>
{showValue && (
<div className='shrink grow' title={formattedAmount ?? undefined}>
<div className='shrink grow' title={formattedAmount}>
<ValueText density={density}>{formattedAmount}</ValueText>
</div>
)}
Expand Down
Loading