Skip to content

Commit

Permalink
feat: support display token collection result
Browse files Browse the repository at this point in the history
  • Loading branch information
PainterPuppets committed May 11, 2024
1 parent 9a6813f commit 5ca3e03
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/components/Search/AggregateSearchResults.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@
.categoryList {
padding: 6px 8px;
}

.icon {
display: inline-flex;
width: 30px;
height: 30px;
border-radius: 50%;
margin-right: 6px;
flex-shrink: 0;
object-fit: cover;
}
76 changes: 60 additions & 16 deletions src/components/Search/AggregateSearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import classNames from 'classnames'
import { ComponentProps, FC } from 'react'
import { SearchResultType, AggregateSearchResult } from '../../services/ExplorerService'
import { getURLByAggregateSearchResult, getDisplayNameByAggregateSearchResult } from './utils'
import { handleNftImgError, patchMibaoImg } from '../../utils/util'
import styles from './AggregateSearchResults.module.scss'
import EllipsisMiddle from '../EllipsisMiddle'
import SmallLoading from '../Loading/SmallLoading'
Expand Down Expand Up @@ -72,15 +73,49 @@ const SearchResultItem: FC<{ keyword?: string; item: AggregateSearchResult }> =
) : (
<HighlightText text={displayName} keyword={keyword} style={{ maxWidth: 'min(200px, 60%)' }} />
)}
{item.type === SearchResultType.UDT && (
<EllipsisMiddle
className={classNames(styles.typeHash, 'monospace')}
style={{ maxWidth: 'min(200px, 40%)' }}
useTextWidthForPlaceholderWidth
title={item.attributes.typeHash}
>
{item.attributes.typeHash}
</EllipsisMiddle>
<EllipsisMiddle
className={classNames(styles.typeHash, 'monospace')}
style={{ maxWidth: 'min(200px, 40%)' }}
useTextWidthForPlaceholderWidth
title={item.attributes.typeHash}
>
{item.attributes.typeHash}
</EllipsisMiddle>
</div>
</Link>
)
}

if (item.type === SearchResultType.TokenCollection) {
return (
<Link className={styles.searchResult} to={to}>
<div className={styles.content}>
{!displayName ? (
t('udt.unknown_token')
) : (
<>
{item.attributes.iconUrl ? (
<img
src={`${patchMibaoImg(item.attributes.iconUrl)}?size=small`}
alt="cover"
loading="lazy"
className={styles.icon}
onError={handleNftImgError}
/>
) : (
<img
src={
item.attributes.standard === 'spore'
? '/images/spore_placeholder.svg'
: '/images/nft_placeholder.png'
}
alt="cover"
loading="lazy"
className={styles.icon}
/>
)}
<HighlightText text={displayName} keyword={keyword} maxHighlightLength={16} sideCharLength={8} />
</>
)}
</div>
</Link>
Expand All @@ -89,11 +124,13 @@ const SearchResultItem: FC<{ keyword?: string; item: AggregateSearchResult }> =

return (
<Link className={styles.searchResult} to={to}>
{!displayName ? (
t('udt.unknown_token')
) : (
<HighlightText text={displayName} keyword={keyword} maxHighlightLength={16} />
)}
<div className={styles.content}>
{!displayName ? (
t('udt.unknown_token')
) : (
<HighlightText text={displayName} keyword={keyword} maxHighlightLength={16} sideCharLength={8} />
)}
</div>
</Link>
)
}
Expand All @@ -102,16 +139,23 @@ interface HighlightTextProps extends ComponentProps<'span'> {
text: string
keyword: string
maxHighlightLength?: number
sideCharLength?: number
}

const HighlightText: FC<HighlightTextProps> = ({ text, keyword, maxHighlightLength = 5, ...props }) => {
const HighlightText: FC<HighlightTextProps> = ({
text,
keyword,
maxHighlightLength = 5,
sideCharLength = 3,
...props
}) => {
const keywordIndex = text.toUpperCase().indexOf(keyword.toUpperCase())
if (keywordIndex === -1) return <>text</>
const startIndex = Math.max(0, keywordIndex - 1)
const keywordLength = Math.min(keyword.length, maxHighlightLength)
const preLength = startIndex
const afterLength = text.length - (keywordLength + 1 + keywordIndex)
const sideChar = Math.min(Math.max(1, maxHighlightLength - keywordLength), 3)
const sideChar = Math.min(Math.max(1, maxHighlightLength - keywordLength), sideCharLength)

return (
<span className={styles.highlightText} {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { isValidBTCAddress } from '../../utils/bitcoin'
// Therefore, here we are implementing a frontend-level limitation on the number of displayed results.
const DISPLAY_COUNT = 10

// The return value of the `TokenCollection` type is missing the `typehash` and there is no way to jump to it, so it is not supported at this time.
const ALLOW_SEARCH_TYPES = [
SearchResultType.Address,
SearchResultType.Block,
Expand All @@ -46,6 +45,7 @@ const ALLOW_SEARCH_TYPES = [
SearchResultType.LockScript,
SearchResultType.Transaction,
SearchResultType.TypeScript,
SearchResultType.TokenCollection,
SearchResultType.UDT,
]

Expand Down
3 changes: 3 additions & 0 deletions src/components/Search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const getURLByAggregateSearchResult = (result: AggregateSearchResult) =>
}
break

case SearchResultType.TokenCollection:
return `/nft-collections/${attributes.sn}`

case SearchResultType.BtcTx:
return `/transaction/${attributes.ckbTransactionHash}`

Expand Down
3 changes: 2 additions & 1 deletion src/services/ExplorerService/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ export type AggregateSearchResult =
standard: string
name: string
description: string
icon_url: string
iconUrl: string
symbol: string
sn: string
},
SearchResultType.TokenCollection
>
Expand Down

0 comments on commit 5ca3e03

Please sign in to comment.