market/src/components/@shared/AssetList/index.tsx

96 lines
2.4 KiB
TypeScript

import AssetTeaser from '@shared/AssetTeaser'
import React, { ReactElement, useEffect, useState } from 'react'
import Pagination from '@shared/Pagination'
import styles from './index.module.css'
import Loader from '@shared/atoms/Loader'
import { useIsMounted } from '@hooks/useIsMounted'
import { getAccessDetailsForAssets } from '@utils/accessDetailsAndPricing'
import { useWeb3 } from '@context/Web3'
function LoaderArea() {
return (
<div className={styles.loaderWrap}>
<Loader />
</div>
)
}
export declare type AssetListProps = {
assets: AssetExtended[]
showPagination: boolean
page?: number
totalPages?: number
isLoading?: boolean
onPageChange?: React.Dispatch<React.SetStateAction<number>>
className?: string
noPublisher?: boolean
}
export default function AssetList({
assets,
showPagination,
page,
totalPages,
isLoading,
onPageChange,
className,
noPublisher
}: AssetListProps): ReactElement {
const { accountId } = useWeb3()
const [assetsWithPrices, setAssetsWithPrices] =
useState<AssetExtended[]>(assets)
const [loading, setLoading] = useState<boolean>(isLoading)
const isMounted = useIsMounted()
useEffect(() => {
if (!assets || !assets.length) return
setAssetsWithPrices(assets as AssetExtended[])
setLoading(false)
async function fetchPrices() {
const assetsWithPrices = await getAccessDetailsForAssets(
assets,
accountId || ''
)
if (!isMounted() || !assetsWithPrices) return
setAssetsWithPrices([...assetsWithPrices])
}
fetchPrices()
}, [assets, isMounted, accountId])
// // This changes the page field inside the query
function handlePageChange(selected: number) {
onPageChange(selected + 1)
}
const styleClasses = `${styles.assetList} ${className || ''}`
return assetsWithPrices && !loading ? (
<>
<div className={styleClasses}>
{assetsWithPrices.length > 0 ? (
assetsWithPrices.map((assetWithPrice) => (
<AssetTeaser
asset={assetWithPrice}
key={assetWithPrice.id}
noPublisher={noPublisher}
/>
))
) : (
<div className={styles.empty}>No results found</div>
)}
</div>
{showPagination && (
<Pagination
totalPages={totalPages}
currentPage={page}
onChangePage={handlePageChange}
/>
)}
</>
) : (
<LoaderArea />
)
}