Skip to content

Commit

Permalink
fix: fix btc explorer link in rgbpp tx list
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Jul 1, 2024
1 parent 48b32aa commit 226885f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 97 deletions.
100 changes: 3 additions & 97 deletions src/pages/RGBPP/TransactionList/List/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import { TFunction, useTranslation } from 'react-i18next'
import styles from './styles.module.scss'
import { TransactionLeapDirection } from '../../../../components/RGBPP/types'
import AddressText from '../../../../components/AddressText'
import { Link } from '../../../../components/Link'
import { localeNumberString } from '../../../../utils/number'
import SortButton from '../../../../components/SortButton'
import FilterButton from '../../../../components/FilterButton'
import { useStatistics } from '../../../../services/ExplorerService'
import { useIsMobile } from '../../../../hooks'
import { dayjs } from '../../../../utils/date'
import { ReactComponent as ShareIcon } from './share_icon.svg'
import config from '../../../../config'
import Item, { type Transaction } from './item'

const RGBTransactionList: React.FC<{ list: Transaction[] }> = ({ list }) => {
const [t] = useTranslation()
const statistics = useStatistics()
const tipBlockNumber = parseInt(statistics?.tipBlockNumber ?? '0', 10)

const filterFields = getFilterList(t)

const headers = getTableHeaders(t)

const isMobile = useIsMobile()

return (
Expand Down Expand Up @@ -80,82 +69,7 @@ const RGBTransactionList: React.FC<{ list: Transaction[] }> = ({ list }) => {
</thead>
<tbody>
{list.length > 0 ? (
list.map(item => {
let leapDirection = '/'
if (item.type === TransactionLeapDirection.IN) {
leapDirection = t('address.leap_in')
}

if (item.type === TransactionLeapDirection.OUT) {
leapDirection = t('address.leap_out')
}

return (
<tr key={item.ckbTxId}>
<td className={styles.hash} title={t('rgbpp.transaction.ckb_txid')}>
<div className={styles.transactionHash}>
<AddressText
disableTooltip
linkProps={{
to: `/transaction/${item.ckbTxId}`,
}}
>
{item.ckbTxId}
</AddressText>
</div>
</td>
<td className={styles.height} title={t('rgbpp.transaction.block_number')}>
<Link className={styles.blockLink} to={`/block/${item.blockNumber}`}>
{localeNumberString(item.blockNumber)}
</Link>
</td>
<td className={styles.confirmation} title={t('rgbpp.transaction.confirmation')}>
{localeNumberString(tipBlockNumber - item.blockNumber)}{' '}
{tipBlockNumber - item.blockNumber === 1
? t('rgbpp.transaction.confirmation')
: t('rgbpp.transaction.confirmations')}
</td>
<td className={styles.time} title={t('rgbpp.transaction.time')}>
{dayjs(item.time).fromNow()}
</td>
<td className={styles.type} title={t('rgbpp.transaction.type')}>
{leapDirection}
</td>
<td className={styles.cellChange} title={t('rgbpp.transaction.rgbpp_cell_change')}>
{`${item.cellChange > 0 ? '+' : ''}${item.cellChange}`}{' '}
{Math.abs(item.cellChange) === 1 ? t('rgbpp.transaction.cell') : t('rgbpp.transaction.cells')}
</td>
<td className={styles.hash} title={t('rgbpp.transaction.btc_txid')}>
{item.btcTxId ? (
<div
style={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: 8 }}
title={t('rgbpp.transaction.view_on_bitcoin_explorer')}
>
<AddressText
style={{ marginLeft: 'auto' }}
disableTooltip
linkProps={{
to: `${config.BITCOIN_EXPLORER}/tx/${item.btcTxId}`,
}}
>
{item.btcTxId}
</AddressText>
<a
href={`${config.BITCOIN_EXPLORER}/tx/${item.btcTxId}`}
target="_blank"
rel="noopener noreferrer"
style={{ display: 'flex', alignItems: 'center' }}
>
<ShareIcon />
</a>
</div>
) : (
'/'
)}
</td>
</tr>
)
})
list.map(item => <Item key={item.ckbTxId} item={item} />)
) : (
<tr>
<td colSpan={headers.length} className={styles.noRecords}>
Expand All @@ -170,15 +84,7 @@ const RGBTransactionList: React.FC<{ list: Transaction[] }> = ({ list }) => {
}

export default RGBTransactionList

export type Transaction = {
ckbTxId: string
blockNumber: number
time: number
type: TransactionLeapDirection
cellChange: number
btcTxId: string
}
export { Transaction }

const getFilterList = (t: TFunction): Record<'key' | 'title' | 'value' | 'to', string>[] => {
return [
Expand Down
112 changes: 112 additions & 0 deletions src/pages/RGBPP/TransactionList/List/item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { useTranslation } from 'react-i18next'
import { useQuery } from '@tanstack/react-query'
import AddressText from '../../../../components/AddressText'
import { Link } from '../../../../components/Link'
import { useStatistics } from '../../../../services/ExplorerService'
import { localeNumberString } from '../../../../utils/number'
import { dayjs } from '../../../../utils/date'
import { ReactComponent as ShareIcon } from './share_icon.svg'
import config from '../../../../config'
import { TransactionLeapDirection } from '../../../../components/RGBPP/types'
import styles from './styles.module.scss'
import { getBtcChainIdentify } from '../../../../services/BTCIdentifier'
import { IS_MAINNET } from '../../../../constants/common'

export type Transaction = {
ckbTxId: string
blockNumber: number
time: number
type: TransactionLeapDirection
cellChange: number
btcTxId: string
}

const Item = ({ item }: { item: Transaction }) => {
const [t] = useTranslation()
const statistics = useStatistics()
const tipBlockNumber = parseInt(statistics?.tipBlockNumber ?? '0', 10)

let leapDirection = '/'
if (item.type === TransactionLeapDirection.IN) {
leapDirection = t('address.leap_in')
}

if (item.type === TransactionLeapDirection.OUT) {
leapDirection = t('address.leap_out')
}
const { data: identity } = useQuery({
queryKey: ['btc-testnet-identity', item.btcTxId],
queryFn: () => (item.btcTxId ? getBtcChainIdentify(item.btcTxId) : null),
enabled: !IS_MAINNET && !!item.btcTxId,
})

const btcExplorerUrl = `${config.BITCOIN_EXPLORER}${IS_MAINNET ? '' : `/${identity}`}`

return (
<tr key={item.ckbTxId}>
<td className={styles.hash} title={t('rgbpp.transaction.ckb_txid')}>
<div className={styles.transactionHash}>
<AddressText
disableTooltip
linkProps={{
to: `/transaction/${item.ckbTxId}`,
}}
>
{item.ckbTxId}
</AddressText>
</div>
</td>
<td className={styles.height} title={t('rgbpp.transaction.block_number')}>
<Link className={styles.blockLink} to={`/block/${item.blockNumber}`}>
{localeNumberString(item.blockNumber)}
</Link>
</td>
<td className={styles.confirmation} title={t('rgbpp.transaction.confirmation')}>
{localeNumberString(tipBlockNumber - item.blockNumber)}{' '}
{tipBlockNumber - item.blockNumber === 1
? t('rgbpp.transaction.confirmation')
: t('rgbpp.transaction.confirmations')}
</td>
<td className={styles.time} title={t('rgbpp.transaction.time')}>
{dayjs(item.time).fromNow()}
</td>
<td className={styles.type} title={t('rgbpp.transaction.type')}>
{leapDirection}
</td>
<td className={styles.cellChange} title={t('rgbpp.transaction.rgbpp_cell_change')}>
{`${item.cellChange > 0 ? '+' : ''}${item.cellChange}`}{' '}
{Math.abs(item.cellChange) === 1 ? t('rgbpp.transaction.cell') : t('rgbpp.transaction.cells')}
</td>
<td className={styles.hash} title={t('rgbpp.transaction.btc_txid')}>
{item.btcTxId ? (
<div
style={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: 8 }}
title={t('rgbpp.transaction.view_on_bitcoin_explorer')}
>
<AddressText
style={{ marginLeft: 'auto' }}
disableTooltip
linkProps={{
to: `${btcExplorerUrl}/tx/${item.btcTxId}`,
}}
>
{item.btcTxId}
</AddressText>
<a
href={`${btcExplorerUrl}/tx/${item.btcTxId}`}
target="_blank"
rel="noopener noreferrer"
style={{ display: 'flex', alignItems: 'center' }}
>
<ShareIcon />
</a>
</div>
) : (
'/'
)}
</td>
</tr>
)
}

export default Item

0 comments on commit 226885f

Please sign in to comment.