-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathInstanceImageCard.tsx
More file actions
190 lines (180 loc) · 6.25 KB
/
Copy pathInstanceImageCard.tsx
File metadata and controls
190 lines (180 loc) · 6.25 KB
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
import { FC, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Box from '@mui/material/Box'
import { Button } from '@mui/base/Button'
import CardContent from '@mui/material/CardContent'
import Card from '@mui/material/Card'
import ContrastIcon from '@mui/icons-material/Contrast'
import Link from '@mui/material/Link'
import Skeleton from '@mui/material/Skeleton'
import Tooltip from '@mui/material/Tooltip'
import OpenInFullIcon from '@mui/icons-material/OpenInFull'
import { styled } from '@mui/material/styles'
import { EvmNft } from 'oasis-nexus/api'
import { processNftImageUrl } from '../../utils/nft-images'
import { hasValidProtocol } from '../../utils/url'
import { COLORS } from '../../../styles/theme/colors'
import { ImagePreview } from '../../components/ImagePreview'
import { VideoPreview } from 'app/components/VideoPreview'
import { NoPreview } from '../../components/NoPreview'
import { checkContentType } from 'app/utils/ipfs'
const imageSize = '350px'
export const StyledImage = styled('img')({
maxWidth: imageSize,
maxHeight: imageSize,
})
const StyledButton = styled(Button, {
shouldForwardProp: prop => prop !== 'darkMode',
})<{ darkMode: boolean }>(({ darkMode }) => ({
cursor: 'pointer',
border: 'none',
width: 36,
height: 36,
borderRadius: 18,
background: darkMode ? COLORS.white : COLORS.grayMedium,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
color: darkMode ? COLORS.grayMedium : COLORS.white,
}))
const DarkModeSwitch: FC<{ darkMode: boolean; onSetDarkMode: (darkMode: boolean) => void }> = ({
darkMode,
onSetDarkMode,
}) => {
const { t } = useTranslation()
return (
<Tooltip title={t('nft.switchBackgroundColor')}>
<StyledButton
darkMode={darkMode}
onClick={() => onSetDarkMode(!darkMode)}
aria-label={t('nft.switchBackgroundColor')}
>
<ContrastIcon />
</StyledButton>
</Tooltip>
)
}
const FullScreenButton: FC<{ darkMode: boolean; onClick: () => void }> = ({ darkMode, onClick }) => {
const { t } = useTranslation()
return (
<Tooltip title={t('nft.openInFullscreen')}>
<StyledButton darkMode={darkMode} onClick={onClick}>
<OpenInFullIcon />
</StyledButton>
</Tooltip>
)
}
type InstanceImageCardProps = {
nft: EvmNft | undefined
isFetched: boolean
isLoading: boolean
}
export const InstanceImageCard: FC<InstanceImageCardProps> = ({ isFetched, isLoading, nft }) => {
const { t } = useTranslation()
const [darkMode, setDarkMode] = useState(false)
const [previewOpen, setPreviewOpen] = useState(false)
const handlePreviewOpen = () => setPreviewOpen(true)
const handlePreviewClose = () => setPreviewOpen(false)
const [imageLoadError, setImageLoadError] = useState(false)
const [contentType, setContentType] = useState<string>('')
useEffect(() => {
async function fetchContentType() {
try {
const url = processNftImageUrl(nft?.image)
const type = await checkContentType(url)
setContentType(type)
} catch (error) {
console.error('Error fetching content type:', error)
}
}
fetchContentType()
}, [nft?.image])
return (
<Card
sx={{
background: darkMode ? COLORS.grayExtraDark : COLORS.white,
}}
>
<CardContent>
<Box
sx={{
background: darkMode ? COLORS.grayExtraDark : COLORS.white,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'space-between',
minHeight: imageSize,
}}
>
{isLoading && <Skeleton variant="rectangular" width={imageSize} height={imageSize} />}
{/* API did not process NFT data fully */}
{isFetched && !nft?.image && <NoPreview placeholderSize={imageSize} />}
{/* API processed NFT data, but image prop is not valid image source */}
{isFetched && nft?.image && imageLoadError && (
<Box sx={{ flex: 1, display: 'flex', alignItems: 'center' }}>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
<NoPreview placeholderSize={imageSize} />
{hasValidProtocol(nft.image) && (
<Link href={nft.image} rel="noopener noreferrer" target="_blank">
{t('nft.openInNewTab')}
</Link>
)}
</Box>
</Box>
)}
{isFetched && nft && hasValidProtocol(nft.image) && !imageLoadError && (
<>
<Box
sx={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}
>
{contentType === 'image' && (
<ImagePreview
handlePreviewClose={handlePreviewClose}
handlePreviewOpen={handlePreviewOpen}
previewOpen={previewOpen}
src={processNftImageUrl(nft.image)}
onError={() => setImageLoadError(true)}
title={nft.name}
maxThumbnailSize={imageSize}
/>
)}
{contentType === 'video' && (
<VideoPreview
handlePreviewClose={handlePreviewClose}
handlePreviewOpen={handlePreviewOpen}
previewOpen={previewOpen}
src={processNftImageUrl(nft.image)}
onError={() => setImageLoadError(true)}
title={nft.name}
maxThumbnailSize={imageSize}
/>
)}
</Box>
<Box
sx={{
width: '100%',
display: 'flex',
justifyContent: 'right',
gap: 3,
}}
>
<FullScreenButton darkMode={darkMode} onClick={handlePreviewOpen} />
<DarkModeSwitch darkMode={darkMode} onSetDarkMode={setDarkMode} />
</Box>
</>
)}
</Box>
</CardContent>
</Card>
)
}