Skip to content

Commit 82aade2

Browse files
fix(ui): ensures visible list view thumbnails with enableListViewSelectAPI (#13864)
Fixes #13856 When using `enableListViewSelectAPI` on upload collections the thumbnail images require data, this PR ensures that the required data is always selected.
1 parent 0a5b7b0 commit 82aade2

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { SanitizedCollectionConfig, SelectType } from 'payload'
2+
3+
/**
4+
* Mutates the incoming select object to append fields required for upload thumbnails
5+
* @param collectionConfig
6+
* @param select
7+
*/
8+
export const appendUploadSelectFields = ({
9+
collectionConfig,
10+
select,
11+
}: {
12+
collectionConfig: SanitizedCollectionConfig
13+
select: SelectType
14+
}) => {
15+
if (!collectionConfig.upload || !select) {
16+
return
17+
}
18+
19+
select.mimeType = true
20+
select.thumbnailURL = true
21+
22+
if (collectionConfig.upload.imageSizes && collectionConfig.upload.imageSizes.length > 0) {
23+
if (
24+
collectionConfig.upload.adminThumbnail &&
25+
typeof collectionConfig.upload.adminThumbnail === 'string'
26+
) {
27+
/** Only return image size properties that are required to generate the adminThumbnailURL */
28+
select.sizes = {
29+
[collectionConfig.upload.adminThumbnail]: {
30+
filename: true,
31+
},
32+
}
33+
} else {
34+
/** Only return image size properties that are required for thumbnails */
35+
select.sizes = collectionConfig.upload.imageSizes.reduce((acc, imageSizeConfig) => {
36+
return {
37+
...acc,
38+
[imageSizeConfig.name]: {
39+
filename: true,
40+
url: true,
41+
width: true,
42+
},
43+
}
44+
}, {})
45+
}
46+
} else {
47+
select.url = true
48+
}
49+
}

packages/next/src/views/List/index.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
import type {
2+
AdminViewServerProps,
3+
CollectionPreferences,
4+
Column,
5+
ColumnPreference,
6+
ListQuery,
7+
ListViewClientProps,
8+
ListViewServerPropsOnly,
9+
PaginatedDocs,
10+
QueryPreset,
11+
SanitizedCollectionPermission,
12+
} from 'payload'
13+
114
import { DefaultListView, HydrateAuthProvider, ListQueryProvider } from '@payloadcms/ui'
215
import { RenderServerComponent } from '@payloadcms/ui/elements/RenderServerComponent'
316
import { getColumns, renderFilters, renderTable, upsertPreferences } from '@payloadcms/ui/rsc'
417
import { notFound } from 'next/navigation.js'
5-
import {
6-
type AdminViewServerProps,
7-
type CollectionPreferences,
8-
type Column,
9-
type ColumnPreference,
10-
type ListQuery,
11-
type ListViewClientProps,
12-
type ListViewServerPropsOnly,
13-
type PaginatedDocs,
14-
type QueryPreset,
15-
type SanitizedCollectionPermission,
16-
} from 'payload'
1718
import {
1819
combineWhereConstraints,
1920
formatAdminURL,
@@ -25,6 +26,7 @@ import {
2526
import React, { Fragment } from 'react'
2627

2728
import { getDocumentPermissions } from '../Document/getDocumentPermissions.js'
29+
import { appendUploadSelectFields } from './appendUploadSelectFields.js'
2830
import { handleGroupBy } from './handleGroupBy.js'
2931
import { renderListViewSlots } from './renderListViewSlots.js'
3032
import { resolveAllFilterOptions } from './resolveAllFilterOptions.js'
@@ -223,6 +225,12 @@ export const renderListView = async (
223225
? transformColumnsToSelect(columns)
224226
: undefined
225227

228+
/** Force select image fields for list view thumbnails */
229+
appendUploadSelectFields({
230+
collectionConfig,
231+
select,
232+
})
233+
226234
try {
227235
if (collectionConfig.admin.groupBy && query.groupBy) {
228236
;({ columnState, data, Table } = await handleGroupBy({

0 commit comments

Comments
 (0)