forked from Chia-Network/chia-blockchain-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNFTPreview.tsx
More file actions
425 lines (388 loc) · 12.4 KB
/
Copy pathNFTPreview.tsx
File metadata and controls
425 lines (388 loc) · 12.4 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { Color, IconMessage, Loading, Flex, SandboxedIframe, usePersistState, useDarkMode } from '@chia-network/core';
import { t, Trans } from '@lingui/macro';
import { NotInterested } from '@mui/icons-material';
import { alpha, Box } from '@mui/material';
import React, { useMemo, useRef, Fragment, useCallback, useEffect, type ReactNode } from 'react';
import styled from 'styled-components';
import AudioSmallIcon from '../../assets/img/audio-small.svg';
import DocumentBlobIcon from '../../assets/img/document-blob.svg';
import DocumentSmallIcon from '../../assets/img/document-small.svg';
import DocumentPngIcon from '../../assets/img/document.png';
import DocumentPngDarkIcon from '../../assets/img/document_dark.png';
import ModelBlobIcon from '../../assets/img/model-blob.svg';
import ModelSmallIcon from '../../assets/img/model-small.svg';
import ModelPngIcon from '../../assets/img/model.png';
import ModelPngDarkIcon from '../../assets/img/model_dark.png';
import UnknownBlobIcon from '../../assets/img/unknown-blob.svg';
import UnknownSmallIcon from '../../assets/img/unknown-small.svg';
import UnknownPngIcon from '../../assets/img/unknown.png';
import UnknownPngDarkIcon from '../../assets/img/unknown_dark.png';
import VideoBlobIcon from '../../assets/img/video-blob.svg';
import VideoSmallIcon from '../../assets/img/video-small.svg';
import VideoPngIcon from '../../assets/img/video.png';
import VideoPngDarkIcon from '../../assets/img/video_dark.png';
import FileType from '../../constants/FileType';
import useCache from '../../hooks/useCache';
import useFileType from '../../hooks/useFileType';
import useHideObjectionableContent from '../../hooks/useHideObjectionableContent';
import useNFT from '../../hooks/useNFT';
import useNFTImageFittingMode from '../../hooks/useNFTImageFittingMode';
import useNFTMetadata from '../../hooks/useNFTMetadata';
import useNFTVerifyHash from '../../hooks/useNFTVerifyHash';
import useStateAbort from '../../hooks/useStateAbort';
import getFileExtension from '../../util/getFileExtension';
import getNFTId from '../../util/getNFTId';
import hasSensitiveContent from '../../util/hasSensitiveContent';
import NFTHashStatus from './NFTHashStatus';
const StyledCardPreview = styled(Box)`
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
overflow: hidden;
background-color: ${({ theme }) => theme.palette.background.paper};
`;
const IframePreventEvents = styled.div`
position: absolute;
height: 100%;
width: 100%;
z-index: 2;
`;
const ModelExtension = styled.div<{ isDarkMode: boolean }>`
position: relative;
top: -20px;
display: flex;
justify-content: center;
align-items: center;
padding: 8px 16px;
background: ${(props) => (props.isDarkMode ? Color.Neutral[800] : Color.Neutral[50])};
box-shadow:
0px 0px 24px ${alpha(Color.Green[500], 0.5)},
0px 4px 8px ${alpha(Color.Green[700], 0.32)};
border-radius: 32px;
color: ${(props) => (props.isDarkMode ? Color.Neutral[50] : Color.Neutral[800])};
`;
const BlobBg = styled.div<{ isDarkMode: boolean }>`
> svg {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
linearGradient {
>stop: first-child {
stop-color: ${(props) => (props.isDarkMode ? Color.Green[800] : Color.Lime[100])};
}
>stop: last-child {
stop-color: ${(props) => (props.isDarkMode ? Color.Green[300] : Color.Green[400])};
}
}
}
> img {
position: relative;
}
`;
const CompactVideoIcon = styled(VideoSmallIcon)``;
const CompactAudioIcon = styled(AudioSmallIcon)``;
const CompactUnknownIcon = styled(UnknownSmallIcon)``;
const CompactDocumentIcon = styled(DocumentSmallIcon)``;
const CompactModelIcon = styled(ModelSmallIcon)``;
const CompactExtension = styled.div`
position: absolute;
top: 48px;
left: 0;
right: 4px;
text-align: center;
color: ${Color.Green[500]};
`;
export type NFTPreviewProps = {
id: string;
width?: number | string;
height?: number | string;
ratio?: number;
fit?: 'cover' | 'contain' | 'fill';
background?: any;
preview?: boolean;
icon?: boolean;
isCompact?: boolean;
disableInteractions?: boolean;
hideStatus?: boolean;
};
export default function NFTPreview(props: NFTPreviewProps) {
const [nftImageFittingMode] = useNFTImageFittingMode();
const {
id,
width = '100%',
height = 'auto',
ratio = 1,
fit = nftImageFittingMode,
background: Background = Fragment,
preview: isPreview = false,
isCompact = false,
disableInteractions = false,
icon = false,
hideStatus = false,
} = props;
const { getURI } = useCache();
const nftId = useMemo(() => getNFTId(id), [id]);
const iframeRef = useRef<any>(null);
const { isDarkMode } = useDarkMode();
const [, setError] = useStateAbort<Error | undefined>(undefined);
const [previewContent, setPreviewContent] = useStateAbort<ReactNode | undefined>(undefined);
const abortControllerRef = useRef(new AbortController());
const [hideObjectionableContent] = useHideObjectionableContent();
const [ignoreSizeLimit /* , setIgnoreSizeLimit */] = usePersistState<boolean>(
false,
`nft-preview-ignore-size-limit-${nftId}`,
);
const { preview, isLoading: isLoadingVerifyHash } = useNFTVerifyHash(nftId, {
preview: isPreview,
ignoreSizeLimit,
});
const { type: previewFileType, isLoading: isLoadingFileType } = useFileType(preview?.uri);
const { isLoading: isLoadingNFT } = useNFT(nftId);
const { metadata, isLoading: isLoadingMetadata } = useNFTMetadata(nftId);
const isLoading = isLoadingVerifyHash || isLoadingMetadata || isLoadingNFT || isLoadingFileType;
const blurPreview = useMemo(() => {
if (!hideObjectionableContent) {
return false;
}
if (isLoading) {
return false;
}
if (!metadata) {
return false;
}
if (hasSensitiveContent(metadata)) {
return true;
}
return false;
}, [hideObjectionableContent, isLoading, metadata]);
const previewExtension = useMemo(() => getFileExtension(preview?.uri), [preview]);
const preparePreview = useCallback(
async (signal: AbortSignal) => {
try {
setError(undefined, signal);
if (!preview?.uri) {
setPreviewContent(undefined, signal);
return;
}
const style = `
html, body {
border: 0px;
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
img {
object-fit: ${fit};
}
`;
const cachedURI = await getURI(preview.uri, { maxSize: ignoreSizeLimit ? -1 : undefined });
if (!cachedURI || !cachedURI.startsWith('cache://')) {
setPreviewContent(undefined, signal);
return;
}
setPreviewContent(
<>
<style>{style}</style>
{previewFileType === FileType.VIDEO ? (
<video width="100%" height="100%" controls={!disableInteractions}>
<source src={cachedURI} />
</video>
) : previewFileType === FileType.AUDIO ? (
<audio className={isDarkMode ? 'dark' : ''} controls={!disableInteractions}>
<source src={cachedURI} />
</audio>
) : (
<img src={cachedURI} alt={t`Preview`} width="100%" height="100%" />
)}
</>,
signal,
);
} catch (e) {
setError(e as Error, signal);
}
},
[
preview,
fit,
getURI,
ignoreSizeLimit,
previewFileType,
disableInteractions,
isDarkMode,
setPreviewContent,
setError,
],
);
useEffect(() => {
abortControllerRef.current.abort();
abortControllerRef.current = new AbortController();
preparePreview(abortControllerRef.current.signal);
}, [preparePreview]);
const previewCompactIcon = useMemo(() => {
switch (previewFileType) {
case FileType.VIDEO:
return <CompactVideoIcon width="100%" />;
case FileType.AUDIO:
return <CompactAudioIcon width="100%" />;
case FileType.MODEL:
return <CompactModelIcon width="100%" />;
case FileType.DOCUMENT:
return <CompactDocumentIcon width="100%" />;
default: {
if (previewExtension) {
return <CompactExtension>.{previewExtension}</CompactExtension>;
}
return <CompactUnknownIcon width="100%" />;
}
}
}, [previewFileType, previewExtension]);
const previewIcon = useMemo(() => {
switch (previewFileType) {
case FileType.DOCUMENT:
return (
<BlobBg isDarkMode={isDarkMode}>
<DocumentBlobIcon />
<img src={isDarkMode ? DocumentPngDarkIcon : DocumentPngIcon} />
</BlobBg>
);
/*
case FileType.AUDIO:
return (
<BlobBg isDarkMode={isDarkMode}>
<AudioBlobIcon />
<img src={isDarkMode ? AudioPngDarkIcon : AudioPngIcon} />
</BlobBg>
);
*/
case FileType.VIDEO:
return (
<BlobBg isDarkMode={isDarkMode}>
<VideoBlobIcon />
<img src={isDarkMode ? VideoPngDarkIcon : VideoPngIcon} />
</BlobBg>
);
case FileType.MODEL:
return (
<BlobBg isDarkMode={isDarkMode}>
<ModelBlobIcon />
<img src={isDarkMode ? ModelPngDarkIcon : ModelPngIcon} />
</BlobBg>
);
default:
return (
<BlobBg isDarkMode={isDarkMode}>
<UnknownBlobIcon />
<img src={isDarkMode ? UnknownPngDarkIcon : UnknownPngIcon} />
</BlobBg>
);
}
}, [previewFileType, isDarkMode]);
const previewIframe = useMemo(() => {
if (isCompact && previewFileType !== FileType.IMAGE) {
return (
<Flex alignItems="center" justifyContent="center">
<Flex width="50%" alignItems="center" justifyContent="center">
{previewCompactIcon}
</Flex>
</Flex>
);
}
if (icon || [FileType.MODEL, FileType.DOCUMENT].includes(previewFileType)) {
return (
<>
{previewIcon}
{previewExtension && <ModelExtension isDarkMode={isDarkMode}>.{previewExtension}</ModelExtension>}
</>
);
}
const canInteract =
!isPreview && !disableInteractions && (previewFileType === FileType.VIDEO || previewFileType === FileType.AUDIO);
return (
<Box
ref={iframeRef}
sx={{
padding: 0,
margin: 0,
height: '100%',
width: '100%',
position: 'relative',
}}
>
{!canInteract && <IframePreventEvents />}
<SandboxedIframe hideUntilLoaded allowPointerEvents={!canInteract}>
{previewContent}
</SandboxedIframe>
{blurPreview && (
<Box
sx={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: 'background.paper',
}}
>
<UnknownBlobIcon width="100%" height="100%" />
</Box>
)}
</Box>
);
}, [
isPreview,
isCompact,
previewFileType,
disableInteractions,
previewIcon,
icon,
previewExtension,
previewContent,
iframeRef,
isDarkMode,
blurPreview,
previewCompactIcon,
]);
const hasFile = !!preview;
return (
<StyledCardPreview width={width} height={height} sx={{ aspectRatio: ratio.toString() }}>
{isLoading ? (
<Flex position="absolute" left="0" top="0" bottom="0" right="0" justifyContent="center" alignItems="center">
<Loading center>{!isCompact && (isPreview ? t`Loading preview...` : t`Loading NFT...`)}</Loading>
</Flex>
) : !hasFile ? (
<Background>
<IconMessage icon={<NotInterested fontSize="large" />}>
<Trans>No file available</Trans>
</IconMessage>
</Background>
) : (
previewIframe
)}
{!isCompact && !hideStatus && (
<Box
sx={{
display: 'flex',
position: 'absolute',
top: 16,
left: 16,
right: 16,
justifyContent: 'center',
zIndex: 1,
}}
>
<NFTHashStatus nftId={nftId} hideValid />
</Box>
)}
</StyledCardPreview>
);
}