-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1476 from nervosnetwork/develop
Merge develop into testnet
- Loading branch information
Showing
7 changed files
with
316 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { type FC, useState, useRef, useEffect } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { useInfiniteQuery } from '@tanstack/react-query' | ||
import { explorerService } from '../../services/ExplorerService' | ||
import styles from './cells.module.scss' | ||
import { shannonToCkb } from '../../utils/util' | ||
import { parseUDTAmount } from '../../utils/number' | ||
import SUDTTokenIcon from '../../assets/sudt_token.png' | ||
import CKBTokenIcon from './ckb_token_icon.png' | ||
|
||
const fetchCells = async ({ | ||
address, | ||
size = 10, | ||
sort = 'capacity.desc', | ||
page = 1, | ||
}: { | ||
address: string | ||
size: number | ||
sort: string | ||
page: number | ||
}) => { | ||
const res = await explorerService.api.fetchAddressLiveCells(address, page, size, sort) | ||
return { | ||
data: res.data, | ||
nextPage: page + 1, | ||
} | ||
} | ||
|
||
const initialPageParams = { size: 10, sort: 'capacity.desc' } | ||
|
||
const Cells: FC<{ address: string; count: number }> = ({ address, count }) => { | ||
const [params] = useState(initialPageParams) | ||
const loadMoreRef = useRef<HTMLDivElement | null>(null) | ||
|
||
const { t } = useTranslation() | ||
|
||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery( | ||
['address live cells', address, params.size, params.sort], | ||
({ pageParam = 1 }) => fetchCells({ ...params, address, page: pageParam }), | ||
{ | ||
getNextPageParam: lastPage => { | ||
if (lastPage.data.length < params.size) return false | ||
return lastPage.nextPage | ||
}, | ||
}, | ||
) | ||
|
||
const isListDisplayed = count && data | ||
|
||
useEffect(() => { | ||
const trigger = loadMoreRef.current | ||
|
||
if (!isListDisplayed) return | ||
const observer = new IntersectionObserver( | ||
entries => { | ||
if (entries[0].isIntersecting) { | ||
fetchNextPage() | ||
} | ||
}, | ||
{ threshold: 0.5 }, | ||
) | ||
if (trigger) { | ||
observer.observe(trigger) | ||
} | ||
return () => { | ||
if (trigger) { | ||
observer.unobserve(trigger) | ||
} | ||
} | ||
}, [isListDisplayed, fetchNextPage]) | ||
|
||
if (!isListDisplayed) { | ||
return null | ||
} | ||
|
||
const cells = data.pages.map(page => page.data).flat() | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<div className={styles.toolbar}>UTXO: {count.toLocaleString('en')}</div> | ||
<ul> | ||
{cells.map(cell => { | ||
const ckb = Number(shannonToCkb(cell.capacity)).toLocaleString('en') | ||
const title = `${cell.txHash.slice(0, 8)}...${cell.txHash.slice(-8)}#${cell.cellIndex}` | ||
const link = `/transaction/${cell.txHash}` | ||
const assetType: string = cell.extraInfo?.type ?? cell.cellType | ||
let icon = null | ||
let assetName = null | ||
let assetAmount = null | ||
switch (assetType) { | ||
case 'ckb': { | ||
if (cell.data !== '0x') { | ||
// TODO: indicate this is a contentful cell | ||
icon = SUDTTokenIcon | ||
assetName = 'UNKNOWN' | ||
assetAmount = '-' | ||
break | ||
} | ||
icon = CKBTokenIcon | ||
assetName = 'CKB' | ||
assetAmount = ckb | ||
break | ||
} | ||
case 'udt': | ||
case 'omiga_inscription': { | ||
icon = SUDTTokenIcon | ||
assetName = cell.extraInfo.symbol || t('udt.inscription') | ||
assetAmount = cell.extraInfo.decimal | ||
? parseUDTAmount(cell.extraInfo.amount, cell.extraInfo.decimal) | ||
: 'Unknown UDT amount' | ||
break | ||
} | ||
case 'spore_cell': { | ||
icon = SUDTTokenIcon | ||
assetName = 'Spore' | ||
assetAmount = '-' | ||
break | ||
} | ||
case 'nrc_721': { | ||
icon = SUDTTokenIcon | ||
assetName = 'NRC 721' | ||
assetAmount = '-' | ||
break | ||
} | ||
default: { | ||
icon = SUDTTokenIcon | ||
assetName = 'UNKNOWN' | ||
assetAmount = '-' | ||
// ignore | ||
} | ||
} | ||
|
||
return ( | ||
<li key={cell.txHash + cell.cellIndex} className={styles.card}> | ||
<h5> | ||
<a href={link}>{title}</a> | ||
<span>{`${ckb} CKB`}</span> | ||
</h5> | ||
<div className={styles.content}> | ||
{icon ? <img src={icon} alt={assetName ?? 'sudt'} width="40" height="40" /> : null} | ||
<div> | ||
<div className={styles.assetName}>{assetName}</div> | ||
<div>{assetAmount}</div> | ||
</div> | ||
</div> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
{isFetchingNextPage ? <span className={styles.loading}>Loading...</span> : null} | ||
{!hasNextPage || isFetchingNextPage ? null : ( | ||
<div className={styles.loadMore} ref={loadMoreRef}> | ||
<button type="button" onClick={() => fetchNextPage()}> | ||
Load more | ||
</button> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
export default Cells |
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,78 @@ | ||
.container { | ||
width: 100%; | ||
height: 310px; | ||
overflow: scroll; | ||
|
||
.toolbar { | ||
margin-top: 1rem; | ||
margin-bottom: 1rem; | ||
} | ||
|
||
ul { | ||
list-style: none; | ||
padding: 0; | ||
display: grid; | ||
grid-template-columns: repeat(auto-fill, minmax(248px, 1fr)); | ||
gap: 1rem; | ||
} | ||
} | ||
|
||
.card { | ||
flex-basis: 248px; | ||
min-height: 86px; | ||
background: #fff; | ||
border-radius: 4px; | ||
overflow: hidden; | ||
|
||
h5 { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
background: var(--primary-color); | ||
color: #fff; | ||
height: 1.875rem; | ||
padding: 8px; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
|
||
&:hover { | ||
a { | ||
font-weight: bold; | ||
color: #fff; | ||
text-decoration: underline; | ||
} | ||
} | ||
} | ||
|
||
.content { | ||
padding: 8px; | ||
display: flex; | ||
|
||
img { | ||
margin-right: 8px; | ||
} | ||
|
||
.assetName { | ||
color: #666; | ||
} | ||
} | ||
} | ||
|
||
.loading { | ||
display: flex; | ||
justify-content: center; | ||
padding: 0.5rem; | ||
} | ||
|
||
.loadMore { | ||
display: flex; | ||
justify-content: center; | ||
padding: 0.5rem; | ||
|
||
button { | ||
cursor: pointer; | ||
appearance: none; | ||
border: none; | ||
} | ||
} |
Oops, something went wrong.