Skip to content

Commit

Permalink
refactor: refactor Capacity component
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Dec 22, 2023
1 parent b3571a6 commit fdb9c06
Show file tree
Hide file tree
Showing 28 changed files with 286 additions and 324 deletions.
48 changes: 48 additions & 0 deletions src/components/Capacity/Capacity.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.container {
display: inline-flex;
align-items: flex-end;
font-size: 1rem;

span[data-role='dec'] {
font-size: 0.875em;
}

.unit {
margin-left: 5px;
}

&[data-type='value'] {
span[data-role='dec'] {
color: var(--decimal-color);
}
}

&[data-type='diff'] {
&[data-diff-status='positive'] {
color: var(--primary-color);

&::before {
content: '+';
}
}

&[data-diff-status='negative'] {
color: var(--accent-color);
}
}

&[data-layout='responsive'] {
// FIXME: use css variable later
@media screen and (width <= 1000px) {
font-size: 0.75rem;

span[data-role='dec'] {
font-size: 0.9em;
}

.unit {
font-size: 1rem;
}
}
}
}
41 changes: 41 additions & 0 deletions src/components/Capacity/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import BigNumber from 'bignumber.js'
import { useMemo } from 'react'
import styles from './Capacity.module.scss'

interface CapacityProps {
capacity: string
type?: 'value' | 'diff'
layout?: 'responsive' | 'fixed'
unit?: 'CKB' | null
display?: 'full' | 'short'
}

const Capacity: React.FC<CapacityProps> = ({
capacity,
type = 'value',
layout = 'fixed',
unit = 'CKB',
display = 'full',
}) => {
const [int, dec, diffStatus] = useMemo(() => {
const c = new BigNumber(capacity)
const [int, dec] = c.toFormat(display === 'full' ? 8 : undefined).split('.')
if (type !== 'diff' || c.isZero()) return [int, dec]
if (c.isPositive()) return [int, dec, 'positive']
return [int, dec, 'negative']
}, [capacity, display, type])

return (
<div className={styles.container} data-type={type} data-diff-status={diffStatus} data-layout={layout}>
<span data-role="int">{int}</span>
{dec ? (
<span data-role="dec" className="monospace">
{`.${dec}`}
</span>
) : null}
{unit && <span className={`${styles.unit} monospace`}>{unit}</span>}
</div>
)
}

export default Capacity
60 changes: 0 additions & 60 deletions src/components/DecimalCapacity/index.tsx

This file was deleted.

45 changes: 0 additions & 45 deletions src/components/DecimalCapacity/styled.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions src/components/DecimalCapacity/styles.module.scss

This file was deleted.

33 changes: 0 additions & 33 deletions src/components/LiteTransactionList/LiteTransactionList.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,6 @@
white-space: pre;
}

.income > span {
display: inline-flex;

* {
color: inherit;
font-size: 1rem;

// REFACTOR: it's hard to maintain, the component of decimal should be refactored
:global {
div.addition,
div.subtraction {
font-size: 0.875rem;
}
}
}

&[data-is-positive='true'][data-is-zero='false'] {
color: var(--primary-color);

&::before {
content: '+';
}
}

&[data-is-zero='true'] {
color: #999;
}

&[data-is-positive='false'] {
color: var(--accent-color);
}
}

// TODO: use https://github.com/Magickbase/ckb-explorer-frontend/pull/178/files#diff-0f21b8f9c1d917feff4b138db3e62f202f2e3c1b691a2cd309397400edfca591R3
@media screen and (width <= 1200px) {
padding: 0;
Expand Down
12 changes: 3 additions & 9 deletions src/components/LiteTransactionList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import styles from './LiteTransactionList.module.scss'
import AddressText from '../AddressText'
import { localeNumberString } from '../../utils/number'
import { useParseDate } from '../../utils/date'
import DecimalCapacity from '../DecimalCapacity'
import Capacity from '../Capacity'
import { shannonToCkb } from '../../utils/util'

const LiteTransactionList: React.FC<{
Expand Down Expand Up @@ -68,7 +68,6 @@ const LiteTransactionList: React.FC<{
if (bigIncome.isNaN()) {
bigIncome = new BigNumber(0)
}
const isIncome = bigIncome.isGreaterThanOrEqualTo(0)
return (
<tr key={item.transactionHash}>
<td className={styles.hash} title={t('transaction.transaction_hash')}>
Expand Down Expand Up @@ -97,13 +96,8 @@ const LiteTransactionList: React.FC<{
}`}
</td>
{address ? (
<td className={styles.income} title={t('transaction.capacity_change')}>
<span data-is-positive={bigIncome.isPositive()} data-is-zero={bigIncome.isZero()}>
<DecimalCapacity
value={localeNumberString(shannonToCkb(bigIncome))}
balanceChangeType={isIncome ? 'income' : 'payment'}
/>
</span>
<td title={t('transaction.capacity_change')}>
<Capacity capacity={shannonToCkb(bigIncome)} type="diff" />
</td>
) : null}
</tr>
Expand Down
8 changes: 2 additions & 6 deletions src/components/TransactionItem/TransactionIncome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Tooltip } from 'antd'
import { useTranslation } from 'react-i18next'
import { TransactionIncomePanel, TransactionCapacityValuePanel } from './styled'
import { shannonToCkb } from '../../../utils/util'
import { localeNumberString } from '../../../utils/number'
import DecimalCapacity from '../../DecimalCapacity'
import Capacity from '../../Capacity'
import { useIsMobile } from '../../../hooks'
import CurrentAddressIcon from '../../../assets/current_address.svg'

Expand All @@ -24,10 +23,7 @@ export default ({ income }: { income: string }) => {
<img src={CurrentAddressIcon} alt="current Address" />
</Tooltip>
)}
<DecimalCapacity
value={`${bigIncome.isPositive() ? '+' : ''}${localeNumberString(shannonToCkb(bigIncome))}`}
balanceChangeType={isIncome ? 'income' : 'payment'}
/>
<Capacity capacity={shannonToCkb(bigIncome)} type="diff" />
{!isMobile && (
<Tooltip placement="top" title={`${t('address.current-address')} `}>
<img src={CurrentAddressIcon} alt="current Address" />
Expand Down
15 changes: 5 additions & 10 deletions src/components/TransactionItem/TransactionItemCell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from './styled'
import { CellType } from '../../../constants/common'
import TransactionCellArrow from '../../Transaction/TransactionCellArrow'
import DecimalCapacity from '../../DecimalCapacity'
import Capacity from '../../Capacity'
import { parseDiffDate } from '../../../utils/date'
import Cellbase from '../../Transaction/Cellbase'
import styles from './index.module.scss'
Expand Down Expand Up @@ -89,7 +89,6 @@ const WithdrawPopoverItem = ({
)

const WithdrawPopoverInfo = ({ cell }: { cell: Cell }) => {
const isMobile = useIsMobile()
const { t } = useTranslation()
const currentLanguage = useCurrentLanguage()
let width = 'short'
Expand All @@ -104,18 +103,14 @@ const WithdrawPopoverInfo = ({ cell }: { cell: Cell }) => {
<WithdrawPopoverItem
width={width}
title={`${t('nervos_dao.deposit_capacity')}: `}
content={
<DecimalCapacity value={localeNumberString(shannonToCkb(cell.capacity))} fontSize={isMobile ? '8px' : ''} />
}
content={<Capacity capacity={shannonToCkb(cell.capacity)} />}
/>
<WithdrawPopoverItem
width={width}
title={`${t(
isDaoWithdrawCell(cell.cellType) ? 'nervos_dao.compensation' : 'nervos_dao.unissued_compensation',
)}: `}
content={
<DecimalCapacity value={localeNumberString(shannonToCkb(cell.interest))} fontSize={isMobile ? '8px' : ''} />
}
content={<Capacity capacity={shannonToCkb(cell.interest)} />}
/>
<WithdrawPopoverItem
width={width}
Expand Down Expand Up @@ -172,7 +167,7 @@ const TransactionCellNervosDao = ({ cell, cellType }: { cell: Cell; cellType: Ce
const { t } = useTranslation()
return (
<TransactionCellWithdraw>
<DecimalCapacity value={localeNumberString(shannonToCkb(cell.capacity))} />
<Capacity capacity={shannonToCkb(cell.capacity)} />
{cellType === CellType.Input ? (
<Popover placement="right" title="" content={<WithdrawPopoverInfo cell={cell} />} trigger="click">
<img src={isDaoWithdrawCell(cell.cellType) ? NervosDAOWithdrawingIcon : NervosDAOCellIcon} alt="withdraw" />
Expand Down Expand Up @@ -225,7 +220,7 @@ const TransactionCellCapacity = ({ cell, cellType }: { cell: Cell; cellType: Cel

return (
<div className="transactionCellWithoutIcon">
<DecimalCapacity value={localeNumberString(shannonToCkb(cell.capacity))} />
<Capacity capacity={shannonToCkb(cell.capacity)} />
</div>
)
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/TransactionItem/TransactionItemCell/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ export const TransactionCellWithdraw = styled.div`
align-items: center;
margin-top: 2px;
span {
margin-left: 6px;
}
img {
margin-left: 5px;
width: 16px;
Expand Down Expand Up @@ -185,6 +181,10 @@ export const WithdrawItemPanel = styled.div`
@media (max-width: 750px) {
font-size: 10px;
* {
font-size: inherit;
}
}
@media (max-width: 375px) {
Expand Down
1 change: 1 addition & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ body {
--primary-chiffon-color: #e6fcf7;
--navbar-height: 64px;
--table-separator-color: #f5f5f5;
--decimal-color: #999;

margin: 0;
padding: 0;
Expand Down
Loading

0 comments on commit fdb9c06

Please sign in to comment.