forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HorizontalNftCard.tsx
204 lines (187 loc) · 5.68 KB
/
HorizontalNftCard.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { AudiotrackRounded, ImageNotSupported } from '@mui/icons-material'
import clsx from 'clsx'
import NextImage from 'next/image'
import { forwardRef, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import ReactPlayer from 'react-player'
import { NftCardInfo } from '@dao-dao/types'
import {
NFT_VIDEO_EXTENSIONS,
getImageUrlForChainId,
getNftName,
objectMatchesStructure,
toAccessibleImageUrl,
} from '@dao-dao/utils'
import { AudioPlayer } from './AudioPlayer'
import { CopyToClipboard } from './CopyToClipboard'
import { LinkWrapper } from './LinkWrapper'
export interface HorizontalNftCardProps extends NftCardInfo {
className?: string
}
export const HorizontalNftCard = forwardRef<
HTMLDivElement,
HorizontalNftCardProps
>(function HorizontalNftCard(
{
collectionAddress,
collectionName,
externalLink,
imageUrl,
metadata,
name,
tokenId,
chainId,
className,
},
ref
) {
const { t } = useTranslation()
const chainImage = getImageUrlForChainId(chainId)
const chainImageNode = chainImage && (
<NextImage
alt=""
className="shrink-0"
height={36}
src={chainImage}
width={36}
/>
)
const video =
// If image contains a video, treat it as a video.
imageUrl && NFT_VIDEO_EXTENSIONS.includes(imageUrl.split('.').pop() || '')
? imageUrl
: metadata &&
objectMatchesStructure(metadata, {
properties: {
video: {},
},
})
? metadata.properties.video
: null
const [imageLoading, setImageLoading] = useState(!!imageUrl)
const [imageLoadErrored, setImageLoadErrored] = useState(false)
// Load image in background so we can listen for loading complete.
const [loadedImageSrc, setLoadedImgSrc] = useState<string>()
useEffect(() => {
if (
// If showing a video, don't load image.
video ||
!imageUrl ||
loadedImageSrc === toAccessibleImageUrl(imageUrl)
) {
return
}
setImageLoading(true)
const img = new Image()
img.onload = () => {
setLoadedImgSrc(img.src)
setImageLoading(false)
setImageLoadErrored(false)
}
img.onerror = () => {
setLoadedImgSrc(undefined)
setImageLoading(false)
setImageLoadErrored(true)
}
img.src = toAccessibleImageUrl(imageUrl)
}, [imageUrl, loadedImageSrc, video])
const audio =
metadata &&
objectMatchesStructure(metadata, {
properties: {
audio: {},
},
})
? metadata.properties.audio
: null
const showingImageUrl = imageUrl && !imageLoadErrored
return (
<div
className={clsx(
'flex flex-col items-stretch overflow-hidden rounded-lg bg-background-primary sm:grid sm:grid-cols-[auto_1fr] sm:grid-rows-1',
imageLoading && 'animate-pulse',
className
)}
ref={ref}
>
<div className="relative aspect-square sm:h-36 sm:w-36">
<div className="absolute top-0 right-0 bottom-0 left-0">
{video ? (
<ReactPlayer
controls
height="100%"
onReady={() => setImageLoading(false)}
url={video}
width="100%"
/>
) : showingImageUrl ? (
<div
className={clsx(
'relative aspect-square bg-cover bg-center transition-opacity',
loadedImageSrc ? 'opacity-100' : 'opacity-0'
)}
style={{
backgroundImage: loadedImageSrc && `url(${loadedImageSrc})`,
}}
></div>
) : (
<div className="flex h-full w-full items-center justify-center">
{audio ? (
<AudiotrackRounded className="!h-14 !w-14 text-icon-tertiary" />
) : (
<ImageNotSupported className="!h-14 !w-14 text-icon-tertiary" />
)}
</div>
)}
</div>
{audio && !video && (
<AudioPlayer
className="absolute bottom-0 left-0 right-0 bg-transparent"
iconClassName="text-icon-primary"
progressClassName="text-text-primary"
src={audio}
style={{
background:
'linear-gradient(to bottom, rgba(var(--color-background-base), 0), rgba(var(--color-background-base), 0.8) 50%, rgba(var(--color-background-base), 1) 100%)',
}}
/>
)}
</div>
<div className="flex min-w-0 grow flex-col">
<p className="title-text border-b border-border-secondary py-4 px-6">
{/* Don't include collection name since we show it below. */}
{getNftName('', tokenId, name)}
</p>
<div className="flex grow flex-row items-center justify-between gap-8 py-4 px-6">
{/* Collection */}
<div className="flex flex-col items-stretch justify-between gap-1 overflow-hidden">
<CopyToClipboard
className="text-xs"
label={t('title.collection')}
textClassName="secondary-text"
tooltip={t('button.copyAddressToClipboard')}
value={collectionAddress}
/>
<p className="primary-text truncate font-normal">
{collectionName}
</p>
</div>
{/* Source chain */}
{chainImageNode ? (
externalLink ? (
<LinkWrapper
className="shrink-0"
href={externalLink?.href}
openInNewTab
>
{chainImageNode}
</LinkWrapper>
) : (
chainImageNode
)
) : null}
</div>
</div>
</div>
)
})