Skip to content

Commit

Permalink
refactor: formatter usage, adapt balance units
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Sep 13, 2023
1 parent 2150d7c commit 234223b
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 43 deletions.
1 change: 1 addition & 0 deletions .eslintcache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"/home/tonio/Workspaces/Origin/origin-defi/libs/shared/utils/src/formatters.ts":"1"},{"size":1067,"mtime":1694612446615,"results":"2","hashOfConfig":"3"},{"filePath":"4","messages":"5","suppressedMessages":"6","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"pshlvk","/home/tonio/Workspaces/Origin/origin-defi/libs/shared/utils/src/formatters.ts",[],[]]
3 changes: 2 additions & 1 deletion libs/oeth/history/src/components/APYContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Divider, Stack, Typography } from '@mui/material';
import { valueFormat } from '@origin/shared/components';
import { valueFormat } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { useAccount } from 'wagmi';

import { useHistoryTableQuery } from '../queries.generated';

export function APYContainer() {
Expand Down
8 changes: 4 additions & 4 deletions libs/oeth/history/src/components/HistoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { useMemo } from 'react';

import {
Box,
Button,
Stack,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
} from '@mui/material';
import { LinkIcon, quantityFormat } from '@origin/shared/components';
import { LinkIcon } from '@origin/shared/components';
import { quantityFormat } from '@origin/shared/utils';
import {
createColumnHelper,
flexRender,
Expand All @@ -19,11 +19,11 @@ import {
} from '@tanstack/react-table';
import { useIntl } from 'react-intl';

import type { HistoryTableQuery } from '../queries.generated';
import usePagination from '@mui/material/usePagination/usePagination';
import { HistoryFilterButton } from './HistoryButton';
import { HistoryCell } from './HistoryCell';

import type { HistoryTableQuery } from '../queries.generated';

export type Rows = HistoryTableQuery['addressById']['history'];

interface Props {
Expand Down
2 changes: 1 addition & 1 deletion libs/oeth/swap/src/components/SwapRouteAccordionItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { alpha, Box, Stack, Typography } from '@mui/material';
import { currencyFormat, quantityFormat } from '@origin/shared/components';
import { tokens } from '@origin/shared/contracts';
import { usePrices } from '@origin/shared/providers';
import { currencyFormat, quantityFormat } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { formatUnits } from 'viem';

Expand Down
2 changes: 1 addition & 1 deletion libs/oeth/swap/src/components/SwapRouteCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { alpha, Box, Card, CardHeader, Stack, Typography } from '@mui/material';
import { currencyFormat, quantityFormat } from '@origin/shared/components';
import { tokens } from '@origin/shared/contracts';
import { usePrices } from '@origin/shared/providers';
import { currencyFormat, quantityFormat } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { formatUnits } from 'viem';

Expand Down
12 changes: 3 additions & 9 deletions libs/oeth/swap/src/components/TokenSelectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Typography,
} from '@mui/material';
import { usePrices } from '@origin/shared/providers';
import { currencyFormat, formatAmount } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { useAccount, useBalance } from 'wagmi';

Expand Down Expand Up @@ -141,18 +142,11 @@ function TokenListItem({ token, ...rest }: TokenListItemProps) {
{isBalanceLoading ? (
<Skeleton width={30} />
) : (
intl.formatNumber(bal, {
minimumFractionDigits: 0,
maximumFractionDigits: 4,
})
formatAmount(balance.value, balance.decimals)
)}
</Typography>
<Typography color="text.secondary" variant="body2">
{intl.formatNumber(balUsd, {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
})}
{intl.formatNumber(balUsd, currencyFormat)}
</Typography>
</Box>
</MenuItem>
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/components/src/Cards/SwapCard/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useState } from 'react';

import { alpha, Box, InputBase, Stack, Typography } from '@mui/material';
import { currencyFormat } from '@origin/shared/utils';
import { useDebouncedEffect } from '@react-hookz/web';
import { useIntl } from 'react-intl';

import { Loader } from '../../Loader';
import { cardStyles } from '../Card';
import { currencyFormat } from './SwapCard';
import { SwapItem } from './SwapItem';
import { styles } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion libs/shared/components/src/Cards/SwapCard/Output.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { alpha, Box, Typography } from '@mui/material';
import { currencyFormat, valueFormat } from '@origin/shared/utils';
import { useIntl } from 'react-intl';

import { Loader } from '../../Loader';
import { cardStyles } from '../Card';
import { currencyFormat, valueFormat } from './SwapCard';
import { SwapItem } from './SwapItem';
import { styles } from './utils';

Expand Down
15 changes: 0 additions & 15 deletions libs/shared/components/src/Cards/SwapCard/SwapCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import { Input } from './Input';
import { Output } from './Output';
import { SwapButton } from './SwapButton';

import type { FormatNumberOptions } from 'react-intl';

export const valueFormat: FormatNumberOptions = {
minimumFractionDigits: 2,
};
export const currencyFormat: FormatNumberOptions = {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
};
export const quantityFormat: FormatNumberOptions = {
minimumFractionDigits: 0,
maximumFractionDigits: 4,
};

interface Props {
title: string | React.ReactNode;
baseTokenName: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Box, MenuItem, Stack, Typography } from '@mui/material';
import { currencyFormat, quantityFormat } from '@origin/shared/utils';
import { useIntl } from 'react-intl';

import { Mix } from '../../Mix';
import { currencyFormat, quantityFormat } from './SwapCard';

import type { Option } from './TokenListModal';

Expand Down
11 changes: 2 additions & 9 deletions libs/shared/components/src/Inputs/TokenInput.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { forwardRef } from 'react';

import { alpha, Box, IconButton, Stack, Typography } from '@mui/material';
import { formatAmount } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { formatUnits } from 'viem';

import { currencyFormat } from '../Cards';
import { Loader } from '../Loader';
import { BigIntInput } from './BigIntInput';

Expand Down Expand Up @@ -60,13 +60,6 @@ export const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>(
) => {
const intl = useIntl();

// const handleMaxClick = () => {
// if (onAmountChange) {
// onAmountChange(balance);
// }
// };

const bal = +formatUnits(balance, decimals);
const amountUsd = +formatUnits(amount, decimals) * tokenPriceUsd;

return (
Expand Down Expand Up @@ -127,7 +120,7 @@ export const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>(
{intl.formatMessage(
{ defaultMessage: 'Balance: {number}' },
{
number: intl.formatNumber(bal || 0, currencyFormat),
number: formatAmount(balance, decimals),
},
)}
</Typography>
Expand Down

0 comments on commit 234223b

Please sign in to comment.