Skip to content

Commit

Permalink
fix: enforce uniform asset height (#5181)
Browse files Browse the repository at this point in the history
* fix: show loading assets at uniform height

* fix: enforce the uniform asset height

* fix: memoize assets

* fix: be more lenient with uniformHeight

* fix: simplify mapping
  • Loading branch information
zzmp committed Nov 11, 2022
1 parent 429ade5 commit f8399fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/nft/components/collection/Card.tsx
Expand Up @@ -267,7 +267,9 @@ const Image = ({ uniformHeight, setUniformHeight }: ImageProps) => {
if (uniformHeight === UniformHeights.unset) {
setUniformHeight(e.currentTarget.clientHeight)
} else if (uniformHeight !== UniformHeights.notUniform && e.currentTarget.clientHeight !== uniformHeight) {
setUniformHeight(UniformHeights.notUniform)
if (!uniformHeight || Math.abs(uniformHeight - e.currentTarget.clientHeight) > 1) {
setUniformHeight(UniformHeights.notUniform)
}
}
}
setLoaded(true)
Expand Down Expand Up @@ -422,7 +424,9 @@ const Audio = ({ uniformHeight, setUniformHeight, shouldPlay, setCurrentTokenPla
uniformHeight !== UniformHeights.notUniform &&
e.currentTarget.clientHeight !== uniformHeight
) {
setUniformHeight(UniformHeights.notUniform)
if (!uniformHeight || Math.abs(uniformHeight - e.currentTarget.clientHeight) > 1) {
setUniformHeight(UniformHeights.notUniform)
}
}
}
setImageLoaded(true)
Expand Down
4 changes: 2 additions & 2 deletions src/nft/components/collection/CollectionAssetLoading.tsx
Expand Up @@ -5,10 +5,10 @@ import { Box } from '../../components/Box'
import { Row } from '../Flex'
import * as styles from './CollectionAssetLoading.css'

export const CollectionAssetLoading = () => {
export const CollectionAssetLoading = ({ height }: { height?: number }) => {
return (
<Box as="div" className={styles.collectionAssetLoading}>
<Box as="div" position="relative" width="full">
<Box as="div" position="relative" width="full" style={{ height }}>
<Box as="div" className={styles.collectionAssetsImageLoading} />
<Box as="img" width="full" opacity="0" src={SizingImage} draggable={false} />
</Box>
Expand Down
41 changes: 20 additions & 21 deletions src/nft/components/collection/CollectionNfts.tsx
Expand Up @@ -166,17 +166,17 @@ export const LoadingButton = styled.div`
background-size: 400%;
`

const loadingAssets = (
const loadingAssets = (height?: number) => (
<>
{Array.from(Array(ASSET_PAGE_SIZE), (_, index) => (
<CollectionAssetLoading key={index} />
<CollectionAssetLoading key={index} height={height} />
))}
</>
)

export const CollectionNftsLoading = () => (
<Box width="full" className={styles.assetList}>
{loadingAssets}
{loadingAssets()}
</Box>
)

Expand Down Expand Up @@ -323,22 +323,21 @@ export const CollectionNfts = ({ contractAddress, collectionStats, rarityVerifie
}
}, [contractAddress])

const Nfts =
collectionNfts &&
collectionNfts.map((asset) =>
asset ? (
<CollectionAsset
key={asset.address + asset.tokenId}
asset={asset}
isMobile={isMobile}
uniformHeight={uniformHeight}
setUniformHeight={setUniformHeight}
mediaShouldBePlaying={asset.tokenId === currentTokenPlayingMedia}
setCurrentTokenPlayingMedia={setCurrentTokenPlayingMedia}
rarityVerified={rarityVerified}
/>
) : null
)
const assets = useMemo(() => {
if (!collectionNfts) return null
return collectionNfts.map((asset) => (
<CollectionAsset
key={asset.address + asset.tokenId}
asset={asset}
isMobile={isMobile}
uniformHeight={uniformHeight}
setUniformHeight={setUniformHeight}
mediaShouldBePlaying={asset.tokenId === currentTokenPlayingMedia}
setCurrentTokenPlayingMedia={setCurrentTokenPlayingMedia}
rarityVerified={rarityVerified}
/>
))
}, [collectionNfts, currentTokenPlayingMedia, isMobile, rarityVerified, uniformHeight])

const hasNfts = collectionNfts && collectionNfts.length > 0
const hasErc1155s = hasNfts && collectionNfts[0] && collectionNfts[0].tokenType === TokenType.ERC1155
Expand Down Expand Up @@ -509,13 +508,13 @@ export const CollectionNfts = ({ contractAddress, collectionStats, rarityVerifie
<InfiniteScroll
next={() => loadNext(ASSET_PAGE_SIZE)}
hasMore={hasNext}
loader={hasNext && hasNfts ? loadingAssets : null}
loader={hasNext && hasNfts ? loadingAssets(uniformHeight) : null}
dataLength={collectionNfts?.length ?? 0}
style={{ overflow: 'unset' }}
className={hasNfts || isLoadingNext ? styles.assetList : undefined}
>
{hasNfts ? (
Nfts
assets
) : collectionNfts?.length === 0 ? (
<Center width="full" color="textSecondary" textAlign="center" style={{ height: '60vh' }}>
<EmptyCollectionWrapper>
Expand Down

1 comment on commit f8399fd

@vercel
Copy link

@vercel vercel bot commented on f8399fd Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

interface – ./

interface-uniswap.vercel.app
interface-git-main-uniswap.vercel.app

Please sign in to comment.