## Description V2 PR [here](https://github.com/payloadcms/payload/pull/6733) Additionally fixes issue with image thumbnails not updating properly until page refresh. Image thumbnails properly update on document save now. - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
'use client'
|
|
import React from 'react'
|
|
|
|
import './index.scss'
|
|
|
|
const baseClass = 'thumbnail'
|
|
|
|
import type { SanitizedCollectionConfig } from 'payload/types'
|
|
|
|
import { File } from '../../graphics/File/index.js'
|
|
import { ShimmerEffect } from '../ShimmerEffect/index.js'
|
|
|
|
export type ThumbnailProps = {
|
|
className?: string
|
|
collectionSlug?: string
|
|
doc?: Record<string, unknown>
|
|
fileSrc?: string
|
|
imageCacheTag?: string
|
|
size?: 'expand' | 'large' | 'medium' | 'small'
|
|
uploadConfig?: SanitizedCollectionConfig['upload']
|
|
}
|
|
|
|
const ThumbnailContext = React.createContext({
|
|
className: '',
|
|
filename: '',
|
|
size: 'medium',
|
|
src: '',
|
|
})
|
|
|
|
export const useThumbnailContext = () => React.useContext(ThumbnailContext)
|
|
|
|
export const Thumbnail: React.FC<ThumbnailProps> = (props) => {
|
|
const { className = '', doc: { filename } = {}, fileSrc, imageCacheTag, size } = props
|
|
const [fileExists, setFileExists] = React.useState(undefined)
|
|
|
|
const classNames = [baseClass, `${baseClass}--size-${size || 'medium'}`, className].join(' ')
|
|
|
|
React.useEffect(() => {
|
|
if (!fileSrc) {
|
|
setFileExists(false)
|
|
return
|
|
}
|
|
|
|
const img = new Image()
|
|
img.src = fileSrc
|
|
img.onload = () => {
|
|
setFileExists(true)
|
|
}
|
|
img.onerror = () => {
|
|
setFileExists(false)
|
|
}
|
|
}, [fileSrc])
|
|
|
|
return (
|
|
<div className={classNames}>
|
|
{fileExists === undefined && <ShimmerEffect height="100%" />}
|
|
{fileExists && (
|
|
<img
|
|
alt={filename as string}
|
|
src={`${fileSrc}${imageCacheTag ? `?${imageCacheTag}` : ''}`}
|
|
/>
|
|
)}
|
|
{fileExists === false && <File />}
|
|
</div>
|
|
)
|
|
}
|