Skip to content

Commit 6f90d62

Browse files
authored
fix(ui): upload.displayPreview should affect all previews in the admin panel (#11496)
### What? We have the option to set `displayPreview: true || false` on upload collections / upload fields - with the **field** option taking precedence. Currently, `displayPreview` is only affecting the list view for the **_related_** collection. i.e. if you go to a collection that has an upload field - the preview will be hidden/shown correctly according to the `displayPreview` option. <img width="620" alt="Screenshot 2025-03-03 at 12 38 18 PM" src="https://github.com/user-attachments/assets/c11c2a84-0f64-4a08-940e-8c3f9096484b" /> However, when you go directly to the upload collection and look at the list view - the preview is always shown, not affected by the `displayPreview` option. <img width="446" alt="Screenshot 2025-03-03 at 12 39 24 PM" src="https://github.com/user-attachments/assets/f5e1267a-d98a-4c8c-8d54-93dea6cd2e31" /> Also, we have previews within the file field itself - also not being affected by the `displayPreview` option. <img width="528" alt="Screenshot 2025-03-03 at 12 40 06 PM" src="https://github.com/user-attachments/assets/3dd04c9a-3d9f-4823-90f8-b538f3d420f9" /> All the upload related previews (excluding preview sizes and upload editing options) should be affected by the `displayPreview` option. ### How? Checks for `collection.displayPreview` and `field.displayPreview` in all places where previews are displayed. Closes #11404
1 parent 6699844 commit 6f90d62

File tree

9 files changed

+83
-37
lines changed

9 files changed

+83
-37
lines changed

packages/ui/src/elements/FileDetails/StaticFileDetails/index.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@ export const StaticFileDetails: React.FC<StaticFileDetailsProps> = (props) => {
3838

3939
const { filename, filesize, height, mimeType, thumbnailURL, url, width } = doc
4040

41+
const previewAllowed = uploadConfig.displayPreview ?? true
42+
4143
return (
4244
<div className={baseClass}>
4345
<header>
44-
<Thumbnail
45-
// size="small"
46-
className={`${baseClass}__thumbnail`}
47-
doc={doc}
48-
fileSrc={thumbnailURL || url}
49-
imageCacheTag={imageCacheTag}
50-
uploadConfig={uploadConfig}
51-
/>
46+
{previewAllowed && (
47+
<Thumbnail
48+
// size="small"
49+
className={`${baseClass}__thumbnail`}
50+
doc={doc}
51+
fileSrc={thumbnailURL || url}
52+
imageCacheTag={imageCacheTag}
53+
uploadConfig={uploadConfig}
54+
/>
55+
)}
5256
<div className={`${baseClass}__main-detail`}>
5357
<FileMeta
5458
filename={filename as string}

packages/ui/src/elements/Table/DefaultCell/fields/File/index.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,30 @@ export interface FileCellProps
2121
export const FileCell: React.FC<FileCellProps> = ({
2222
cellData: filename,
2323
collectionConfig,
24+
field,
2425
rowData,
2526
}) => {
26-
return (
27-
<div className={baseClass}>
28-
<Thumbnail
29-
className={`${baseClass}__thumbnail`}
30-
collectionSlug={collectionConfig?.slug}
31-
doc={{
32-
...rowData,
33-
filename,
34-
}}
35-
fileSrc={rowData?.thumbnailURL || rowData?.url}
36-
size="small"
37-
uploadConfig={collectionConfig?.upload}
38-
/>
39-
<span className={`${baseClass}__filename`}>{String(filename)}</span>
40-
</div>
41-
)
27+
const fieldPreviewAllowed = 'displayPreview' in field ? field.displayPreview : undefined
28+
const previewAllowed = fieldPreviewAllowed ?? collectionConfig.upload?.displayPreview ?? true
29+
30+
if (previewAllowed) {
31+
return (
32+
<div className={baseClass}>
33+
<Thumbnail
34+
className={`${baseClass}__thumbnail`}
35+
collectionSlug={collectionConfig?.slug}
36+
doc={{
37+
...rowData,
38+
filename,
39+
}}
40+
fileSrc={rowData?.thumbnailURL || rowData?.url}
41+
size="small"
42+
uploadConfig={collectionConfig?.upload}
43+
/>
44+
<span className={`${baseClass}__filename`}>{String(filename)}</span>
45+
</div>
46+
)
47+
} else {
48+
return <>{String(filename)}</>
49+
}
4250
}

packages/ui/src/elements/Table/DefaultCell/fields/Relationship/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ export const RelationshipCell: React.FC<RelationshipCellProps> = ({
110110
let fileField = null
111111

112112
if (field.type === 'upload') {
113-
const relatedCollectionPreview = !!relatedCollection.upload.displayPreview
113+
const fieldPreviewAllowed = 'displayPreview' in field ? field.displayPreview : undefined
114114
const previewAllowed =
115-
field.displayPreview || (relatedCollectionPreview && field.displayPreview !== false)
115+
fieldPreviewAllowed ?? relatedCollection.upload?.displayPreview ?? true
116116

117117
if (previewAllowed && document) {
118118
fileField = (

packages/ui/src/fields/Upload/HasMany/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import './index.scss'
1717

1818
type Props = {
1919
readonly className?: string
20+
readonly displayPreview?: boolean
2021
readonly fileDocs: {
2122
relationTo: string
2223
value: JsonObject
@@ -30,8 +31,17 @@ type Props = {
3031
}
3132

3233
export function UploadComponentHasMany(props: Props) {
33-
const { className, fileDocs, isSortable, onRemove, onReorder, readonly, reloadDoc, serverURL } =
34-
props
34+
const {
35+
className,
36+
displayPreview,
37+
fileDocs,
38+
isSortable,
39+
onRemove,
40+
onReorder,
41+
readonly,
42+
reloadDoc,
43+
serverURL,
44+
} = props
3545

3646
const moveRow = React.useCallback(
3747
(moveFromIndex: number, moveToIndex: number) => {
@@ -120,6 +130,7 @@ export function UploadComponentHasMany(props: Props) {
120130
alt={(value?.alt || value?.filename) as string}
121131
byteSize={value.filesize as number}
122132
collectionSlug={relationTo}
133+
displayPreview={displayPreview}
123134
filename={value.filename as string}
124135
id={id}
125136
mimeType={value?.mimeType as string}

packages/ui/src/fields/Upload/HasOne/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const baseClass = 'upload upload--has-one'
1414

1515
type Props = {
1616
readonly className?: string
17+
readonly displayPreview?: boolean
1718
readonly fileDoc: {
1819
relationTo: string
1920
value: JsonObject
@@ -25,7 +26,7 @@ type Props = {
2526
}
2627

2728
export function UploadComponentHasOne(props: Props) {
28-
const { className, fileDoc, onRemove, readonly, reloadDoc, serverURL } = props
29+
const { className, displayPreview, fileDoc, onRemove, readonly, reloadDoc, serverURL } = props
2930
const { relationTo, value } = fileDoc
3031
const id = String(value?.id)
3132

@@ -56,6 +57,7 @@ export function UploadComponentHasOne(props: Props) {
5657
alt={(value?.alt || value?.filename) as string}
5758
byteSize={value.filesize as number}
5859
collectionSlug={relationTo}
60+
displayPreview={displayPreview}
5961
filename={value.filename as string}
6062
id={id}
6163
mimeType={value?.mimeType as string}

packages/ui/src/fields/Upload/Input.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type UploadInputProps = {
5555
readonly customUploadActions?: React.ReactNode[]
5656
readonly Description?: React.ReactNode
5757
readonly description?: StaticDescription
58+
readonly displayPreview?: boolean
5859
readonly Error?: React.ReactNode
5960
readonly filterOptions?: FilterOptionsResult
6061
readonly hasMany?: boolean
@@ -85,6 +86,7 @@ export function UploadInput(props: UploadInputProps) {
8586
className,
8687
Description,
8788
description,
89+
displayPreview,
8890
Error,
8991
filterOptions: filterOptionsFromProps,
9092
hasMany,
@@ -495,6 +497,7 @@ export function UploadInput(props: UploadInputProps) {
495497
<>
496498
{populatedDocs && populatedDocs?.length > 0 ? (
497499
<UploadComponentHasMany
500+
displayPreview={displayPreview}
498501
fileDocs={populatedDocs}
499502
isSortable={isSortable && !readOnly}
500503
onRemove={onRemove}
@@ -516,6 +519,7 @@ export function UploadInput(props: UploadInputProps) {
516519
<>
517520
{populatedDocs && populatedDocs?.length > 0 && populatedDocs[0].value ? (
518521
<UploadComponentHasOne
522+
displayPreview={displayPreview}
519523
fileDoc={populatedDocs[0]}
520524
onRemove={onRemove}
521525
readonly={readOnly}

packages/ui/src/fields/Upload/RelationshipContent/index.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { ReloadDoc } from '../types.js'
1010
import { Button } from '../../../elements/Button/index.js'
1111
import { useDocumentDrawer } from '../../../elements/DocumentDrawer/index.js'
1212
import { ThumbnailComponent } from '../../../elements/Thumbnail/index.js'
13+
import { useConfig } from '../../../providers/Config/index.js'
1314
import './index.scss'
1415

1516
const baseClass = 'upload-relationship-details'
@@ -21,6 +22,7 @@ type Props = {
2122
readonly byteSize: number
2223
readonly className?: string
2324
readonly collectionSlug: string
25+
readonly displayPreview?: boolean
2426
readonly filename: string
2527
readonly id?: number | string
2628
readonly mimeType: string
@@ -41,6 +43,7 @@ export function RelationshipContent(props: Props) {
4143
byteSize,
4244
className,
4345
collectionSlug,
46+
displayPreview,
4447
filename,
4548
mimeType,
4649
onRemove,
@@ -52,6 +55,12 @@ export function RelationshipContent(props: Props) {
5255
y,
5356
} = props
5457

58+
const { config } = useConfig()
59+
const collectionConfig =
60+
'collections' in config
61+
? config.collections.find((collection) => collection.slug === collectionSlug)
62+
: undefined
63+
5564
const [DocumentDrawer, _, { openDrawer }] = useDocumentDrawer({
5665
id: src ? id : undefined,
5766
collectionSlug,
@@ -80,17 +89,20 @@ export function RelationshipContent(props: Props) {
8089
}
8190

8291
const metaText = withMeta ? generateMetaText(mimeType, byteSize) : ''
92+
const previewAllowed = displayPreview ?? collectionConfig.upload?.displayPreview ?? true
8393

8494
return (
8595
<div className={[baseClass, className].filter(Boolean).join(' ')}>
8696
<div className={`${baseClass}__imageAndDetails`}>
87-
<ThumbnailComponent
88-
alt={alt}
89-
className={`${baseClass}__thumbnail`}
90-
filename={filename}
91-
fileSrc={thumbnailSrc}
92-
size="small"
93-
/>
97+
{previewAllowed && (
98+
<ThumbnailComponent
99+
alt={alt}
100+
className={`${baseClass}__thumbnail`}
101+
filename={filename}
102+
fileSrc={thumbnailSrc}
103+
size="small"
104+
/>
105+
)}
94106
<div className={`${baseClass}__details`}>
95107
<p className={`${baseClass}__filename`}>
96108
{src ? (

packages/ui/src/fields/Upload/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export function UploadComponent(props: UploadFieldClientProps) {
3535

3636
const { config } = useConfig()
3737

38+
const displayPreview = field.displayPreview
39+
3840
const memoizedValidate = React.useCallback(
3941
(value, options) => {
4042
if (typeof validate === 'function') {
@@ -66,6 +68,7 @@ export function UploadComponent(props: UploadFieldClientProps) {
6668
className={className}
6769
Description={Description}
6870
description={description}
71+
displayPreview={displayPreview}
6972
Error={Error}
7073
filterOptions={filterOptions}
7174
hasMany={hasMany}

test/uploads/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,9 @@ export default buildConfigWithDefaults({
658658
type: 'text',
659659
},
660660
],
661-
upload: true,
661+
upload: {
662+
displayPreview: false,
663+
},
662664
},
663665
{
664666
slug: relationPreviewSlug,

0 commit comments

Comments
 (0)