@@ -10,19 +10,28 @@ import type { SanitizedCollectionConfig } from 'payload'
10
10
import { File } from '../../graphics/File/index.js'
11
11
import { ShimmerEffect } from '../ShimmerEffect/index.js'
12
12
13
- /**
14
- * Check if specific media exist before render it in it's appropriate HTML tag.
15
- * This internal hook allows to share this logic between Thumbnail & ThumbnailComponent
16
- */
17
- const useFileExists = ( fileSrc : string | undefined , fileType : string ) : boolean => {
13
+ export type ThumbnailProps = {
14
+ alt ?: string
15
+ className ?: string
16
+ collectionSlug ?: string
17
+ doc ?: Record < string , unknown >
18
+ fileSrc ?: string
19
+ imageCacheTag ?: string
20
+ size ?: 'expand' | 'large' | 'medium' | 'small'
21
+ uploadConfig ?: SanitizedCollectionConfig [ 'upload' ]
22
+ }
23
+
24
+ export const Thumbnail = ( props : ThumbnailProps ) => {
25
+ const { className = '' , doc : { filename, mimeType } = { } , fileSrc, imageCacheTag, size } = props
26
+ const classNames = [ baseClass , `${ baseClass } --size-${ size || 'medium' } ` , className ] . join ( ' ' )
27
+ const fileType = ( mimeType as string ) ?. split ( '/' ) ?. [ 0 ]
18
28
const [ fileExists , setFileExists ] = React . useState < boolean | undefined > ( undefined )
19
29
20
30
React . useEffect ( ( ) => {
21
31
if ( ! fileSrc || ! fileType ) {
22
32
setFileExists ( false )
23
33
return
24
34
}
25
-
26
35
if ( fileType === 'image' ) {
27
36
const img = new Image ( )
28
37
img . src = fileSrc
@@ -39,76 +48,16 @@ const useFileExists = (fileSrc: string | undefined, fileType: string): boolean =
39
48
}
40
49
} , [ fileSrc , fileType ] )
41
50
42
- return fileExists
43
- }
44
-
45
- export type ThumbnailProps = {
46
- className ?: string
47
- collectionSlug ?: string
48
- doc ?: Record < string , unknown >
49
- fileSrc ?: string
50
- imageCacheTag ?: string
51
- size ?: 'expand' | 'large' | 'medium' | 'small'
52
- uploadConfig ?: SanitizedCollectionConfig [ 'upload' ]
53
- }
54
-
55
- export const Thumbnail : React . FC < ThumbnailProps > = ( props ) => {
56
- const { className = '' , doc : { filename, mimeType } = { } , fileSrc, imageCacheTag, size } = props
57
- const classNames = [ baseClass , `${ baseClass } --size-${ size || 'medium' } ` , className ] . join ( ' ' )
58
- const fileType = ( mimeType as string ) ?. split ( '/' ) ?. [ 0 ]
59
-
60
- const fileExists = useFileExists ( fileSrc , fileType )
61
- const src = React . useMemo (
62
- ( ) => `${ fileSrc } ${ imageCacheTag ? `?${ imageCacheTag } ` : '' } ` ,
63
- [ fileSrc , imageCacheTag ] ,
64
- )
65
-
66
- return (
67
- < div className = { classNames } >
68
- { fileExists === undefined && < ShimmerEffect height = "100%" /> }
69
- { fileExists && fileType === 'image' && < img alt = { filename as string } src = { src } /> }
70
- { fileExists && fileType === 'video' && (
71
- < video
72
- aria-label = { filename as string }
73
- autoPlay = { false }
74
- controls = { false }
75
- muted = { true }
76
- playsInline = { true }
77
- src = { src }
78
- />
79
- ) }
80
- { fileExists === false && < File /> }
81
- </ div >
82
- )
83
- }
84
-
85
- type ThumbnailComponentProps = {
86
- readonly alt ?: string
87
- readonly className ?: string
88
- readonly filename : string
89
- readonly fileSrc : string
90
- readonly imageCacheTag ?: string
91
- readonly mimeType ?: string
92
- readonly size ?: 'expand' | 'large' | 'medium' | 'small'
93
- }
94
- export function ThumbnailComponent ( props : ThumbnailComponentProps ) {
95
- const { alt, className = '' , filename, fileSrc, imageCacheTag, mimeType, size } = props
96
- const classNames = [ baseClass , `${ baseClass } --size-${ size || 'medium' } ` , className ] . join ( ' ' )
97
- const fileType = mimeType ?. split ( '/' ) ?. [ 0 ]
98
-
99
- const fileExists = useFileExists ( fileSrc , fileType )
100
- const src = React . useMemo (
101
- ( ) => `${ fileSrc } ${ imageCacheTag ? `?${ imageCacheTag } ` : '' } ` ,
102
- [ fileSrc , imageCacheTag ] ,
103
- )
51
+ const src = `${ fileSrc } ${ imageCacheTag ? `?${ imageCacheTag } ` : '' } `
52
+ const alt = props . alt || ( filename as string )
104
53
105
54
return (
106
55
< div className = { classNames } >
107
56
{ fileExists === undefined && < ShimmerEffect height = "100%" /> }
108
- { fileExists && fileType === 'image' && < img alt = { alt || filename } src = { src } /> }
57
+ { fileExists && fileType === 'image' && < img alt = { alt } src = { src } /> }
109
58
{ fileExists && fileType === 'video' && (
110
59
< video
111
- aria-label = { alt || filename }
60
+ aria-label = { alt }
112
61
autoPlay = { false }
113
62
controls = { false }
114
63
muted = { true }
0 commit comments