Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prettify large numbers in metrics (#2176)
* prettify large numbers * add tooltip for larger numbers * add test ids, add unit test * move dependency to devDependency * remove unused import * use conditional render component * use prettify large number component for feature overview metrics
- Loading branch information
1 parent
e1b903a
commit 1a09d17
Showing
7 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
frontend/src/component/common/PrettifyLargeNumber/PrettifyLargeNumber.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { render } from 'utils/testRenderer'; | ||
import { screen } from '@testing-library/react'; | ||
import { PrettifyLargeNumber } from './PrettifyLargeNumber'; | ||
import { LARGE_NUMBER_PRETTIFIED } from 'utils/testIds'; | ||
|
||
describe('PrettifyLargeNumber', () => { | ||
it('should render number with separator for value less than threshold', async () => { | ||
render(<PrettifyLargeNumber value={999999} threshold={1000000} />); | ||
|
||
const prettifiedText = await screen.getByTestId( | ||
LARGE_NUMBER_PRETTIFIED | ||
); | ||
|
||
expect(prettifiedText.textContent).toBe('999,999'); | ||
}); | ||
|
||
it('should render prettified number for value equal to the threshold', async () => { | ||
render(<PrettifyLargeNumber value={1000000} threshold={1000000} />); | ||
|
||
const prettifiedText = await screen.getByTestId( | ||
LARGE_NUMBER_PRETTIFIED | ||
); | ||
|
||
expect(prettifiedText.textContent).toBe('1M'); | ||
}); | ||
|
||
it('should render prettified number for value greater than threshold', async () => { | ||
render(<PrettifyLargeNumber value={12345678} threshold={1000000} />); | ||
|
||
const prettifiedText = await screen.getByTestId( | ||
LARGE_NUMBER_PRETTIFIED | ||
); | ||
|
||
expect(prettifiedText.textContent).toBe('12.35M'); | ||
}); | ||
|
||
it('should render prettified number with tooltip having raw value for value greater than threshold', async () => { | ||
render(<PrettifyLargeNumber value={12345678} threshold={1000000} />); | ||
|
||
const prettifiedText = await screen.getByTestId( | ||
LARGE_NUMBER_PRETTIFIED | ||
); | ||
|
||
expect(prettifiedText.getAttribute('aria-label')).toBe('12,345,678'); | ||
}); | ||
|
||
it('should render prettified number with provided significant figures for value greater than threshold', async () => { | ||
render( | ||
<PrettifyLargeNumber | ||
value={12345678} | ||
threshold={1000000} | ||
precision={4} | ||
/> | ||
); | ||
|
||
const prettifiedText = await screen.getByTestId( | ||
LARGE_NUMBER_PRETTIFIED | ||
); | ||
|
||
expect(prettifiedText.textContent).toBe('12.3457M'); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
frontend/src/component/common/PrettifyLargeNumber/PrettifyLargeNumber.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { FC } from 'react'; | ||
import millify from 'millify'; | ||
import { Tooltip } from '@mui/material'; | ||
import { LARGE_NUMBER_PRETTIFIED } from 'utils/testIds'; | ||
import { ConditionallyRender } from '../ConditionallyRender/ConditionallyRender'; | ||
|
||
interface IPrettifyLargeNumberProps { | ||
/** | ||
* Value to prettify | ||
*/ | ||
value: number; | ||
/** | ||
* Threshold above which the number will be prettified. Values lower than this will just have comma separators added | ||
* @default 1_000_000 | ||
*/ | ||
threshold?: number; | ||
/** | ||
* The number of significant figures | ||
* @default 2 | ||
*/ | ||
precision?: number; | ||
} | ||
|
||
export const PrettifyLargeNumber: FC<IPrettifyLargeNumberProps> = ({ | ||
value, | ||
threshold = 1_000_000, | ||
precision = 2, | ||
}) => { | ||
let prettyValue: string; | ||
let showTooltip = false; | ||
|
||
if (value < threshold) { | ||
prettyValue = value.toLocaleString(); | ||
} else { | ||
prettyValue = millify(value, { precision }); | ||
showTooltip = true; | ||
} | ||
|
||
const valueSpan = ( | ||
<span data-testid={LARGE_NUMBER_PRETTIFIED}>{prettyValue}</span> | ||
); | ||
|
||
return ( | ||
<ConditionallyRender | ||
condition={showTooltip} | ||
show={ | ||
<Tooltip title={value.toLocaleString()} arrow> | ||
{valueSpan} | ||
</Tooltip> | ||
} | ||
elseShow={valueSpan} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters