Skip to content

Commit c8b7214

Browse files
authored
feat: collection-level preferences (#12909)
Needed for #12860. The new live preview pattern requires collection-level preferences, a pattern that does not yet exist. Instead of creating a new record for these types of preferences, we can simply reuse `<collectionSlug>-list` under a more general key: `collection-<slug>`. This way other relevant properties can be attached in the future that might not specifically apply to the list view. This will also match the conventions already estalished by document-level preferences in `collection-<slug>-<id>` and `global-<slug>`. --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1210628212784050
1 parent 1db0619 commit c8b7214

16 files changed

Lines changed: 138 additions & 73 deletions

File tree

‎packages/next/src/views/List/handleServerFunction.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
importtype{ListPreferences,ListQuery,ServerFunction,VisibleEntities}from'payload'
1+
importtype{CollectionPreferences,ListQuery,ServerFunction,VisibleEntities}from'payload'
22

33
import{getClientConfig}from'@payloadcms/ui/utilities/getClientConfig'
44
import{headersasgetHeaders}from'next/headers.js'
@@ -8,7 +8,7 @@ import { renderListView } from './index.js'
88

99
typeRenderListResult={
1010
List: React.ReactNode
11-
preferences: ListPreferences
11+
preferences: CollectionPreferences
1212
}
1313

1414
exportconstrenderListHandler: ServerFunction<
@@ -92,7 +92,7 @@ export const renderListHandler: ServerFunction<
9292
importMap: payload.importMap,
9393
})
9494

95-
constpreferencesKey=`${collectionSlug}-list`
95+
constpreferencesKey=`collection-${collectionSlug}`
9696

9797
constpreferences=awaitpayload
9898
.find({
@@ -119,7 +119,7 @@ export const renderListHandler: ServerFunction<
119119
],
120120
},
121121
})
122-
.then((res)=>res.docs[0]?.valueasListPreferences)
122+
.then((res)=>res.docs[0]?.valueasCollectionPreferences)
123123

124124
constvisibleEntities: VisibleEntities={
125125
collections: payload.config.collections

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
importtype{
22
AdminViewServerProps,
3+
CollectionPreferences,
34
ColumnPreference,
45
DefaultDocumentIDType,
5-
ListPreferences,
66
ListQuery,
77
ListViewClientProps,
88
ListViewServerPropsOnly,
@@ -98,8 +98,8 @@ export const renderListView = async (
9898
* This will ensure that prefs are only updated when explicitly set by the user
9999
* This could potentially be done by injecting a `sessionID` into the params and comparing it against a session cookie
100100
*/
101-
constlistPreferences=awaitupsertPreferences<ListPreferences>({
102-
key: `${collectionSlug}-list`,
101+
constcollectionPreferences=awaitupsertPreferences<CollectionPreferences>({
102+
key: `collection-${collectionSlug}`,
103103
req,
104104
value: {
105105
columns,
@@ -120,10 +120,10 @@ export const renderListView = async (
120120

121121
constpage=isNumber(query?.page) ? Number(query.page) : 0
122122

123-
constlimit=listPreferences?.limit||collectionConfig.admin.pagination.defaultLimit
123+
constlimit=collectionPreferences?.limit||collectionConfig.admin.pagination.defaultLimit
124124

125125
constsort=
126-
listPreferences?.sort||
126+
collectionPreferences?.sort||
127127
(typeofcollectionConfig.defaultSort==='string' ? collectionConfig.defaultSort : undefined)
128128

129129
letwhere=mergeListSearchAndWhere({
@@ -150,10 +150,10 @@ export const renderListView = async (
150150
letqueryPreset: QueryPreset|undefined
151151
letqueryPresetPermissions: SanitizedCollectionPermission|undefined
152152

153-
if(listPreferences?.preset){
153+
if(collectionPreferences?.preset){
154154
try{
155155
queryPreset=(awaitpayload.findByID({
156-
id: listPreferences?.preset,
156+
id: collectionPreferences?.preset,
157157
collection: 'payload-query-presets',
158158
depth: 0,
159159
overrideAccess: false,
@@ -194,7 +194,7 @@ export const renderListView = async (
194194
const{ columnState, Table }=renderTable({
195195
clientCollectionConfig,
196196
collectionConfig,
197-
columnPreferences: listPreferences?.columns,
197+
columnPreferences: collectionPreferences?.columns,
198198
columns,
199199
customCellProps,
200200
docs: data.docs,
@@ -230,7 +230,7 @@ export const renderListView = async (
230230
data,
231231
i18n,
232232
limit,
233-
listPreferences,
233+
listPreferences: collectionPreferences,
234234
listSearchableFields: collectionConfig.admin.listSearchableFields,
235235
locale: fullLocale,
236236
params,
@@ -264,7 +264,7 @@ export const renderListView = async (
264264
data={data}
265265
defaultLimit={limit}
266266
defaultSort={sort}
267-
listPreferences={listPreferences}
267+
listPreferences={collectionPreferences}
268268
modifySearchParams={!isInDrawer}
269269
orderableFieldName={collectionConfig.orderable===true ? '_order' : undefined}
270270
>
@@ -278,7 +278,7 @@ export const renderListView = async (
278278
disableQueryPresets,
279279
enableRowSelections,
280280
hasCreatePermission,
281-
listPreferences,
281+
listPreferences: collectionPreferences,
282282
newDocumentURL,
283283
queryPreset,
284284
queryPresetPermissions,

‎packages/payload/src/admin/views/list.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
SanitizedCollectionConfig,
55
}from'../../collections/config/types.js'
66
importtype{ServerProps}from'../../config/types.js'
7-
importtype{ListPreferences}from'../../preferences/types.js'
7+
importtype{CollectionPreferences}from'../../preferences/types.js'
88
importtype{QueryPreset}from'../../query-presets/types.js'
99
importtype{ResolvedFilterOptions}from'../../types/index.js'
1010
importtype{Column}from'../elements/Table.js'
@@ -30,7 +30,7 @@ export type ListViewServerPropsOnly = {
3030
collectionConfig: SanitizedCollectionConfig
3131
data: Data
3232
limit: number
33-
listPreferences: ListPreferences
33+
listPreferences: CollectionPreferences
3434
listSearchableFields: CollectionAdminOptions['listSearchableFields']
3535
}&ServerProps
3636

@@ -48,7 +48,7 @@ export type ListViewClientProps = {
4848
/**
4949
* @deprecated
5050
*/
51-
listPreferences?: ListPreferences
51+
listPreferences?: CollectionPreferences
5252
newDocumentURL: string
5353
/**
5454
* @deprecated

‎packages/payload/src/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,11 +1475,15 @@ export { restoreVersionOperation as restoreVersionOperationGlobal } from './glob
14751475
export{updateOperationasupdateOperationGlobal}from'./globals/operations/update.js'
14761476
exporttype{
14771477
CollapsedPreferences,
1478+
CollectionPreferences,
1479+
/**
1480+
* @deprecated Use `CollectionPreferences` instead.
1481+
*/
1482+
CollectionPreferencesasListPreferences,
14781483
ColumnPreference,
14791484
DocumentPreferences,
14801485
FieldsPreferences,
14811486
InsideFieldsPreferences,
1482-
ListPreferences,
14831487
PreferenceRequest,
14841488
PreferenceUpdateRequest,
14851489
TabsPreferences,

‎packages/payload/src/preferences/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type ColumnPreference = {
3434
active: boolean
3535
}
3636

37-
exporttypeListPreferences={
37+
exporttypeCollectionPreferences={
3838
columns?: ColumnPreference[]
3939
limit?: number
4040
preset?: DefaultDocumentIDType

‎packages/payload/src/query-presets/types.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
importtype{Field}from'../fields/config/types.js'
22
importtype{Access,CollectionSlug}from'../index.js'
3-
importtype{ListPreferences}from'../preferences/types.js'
3+
importtype{CollectionPreferences}from'../preferences/types.js'
44
importtype{Where}from'../types/index.js'
55

66
// Note: order matters here as it will change the rendered order in the UI
@@ -19,7 +19,7 @@ export type QueryPreset = {
1919
users?: string[]
2020
}
2121
}
22-
columns: ListPreferences['columns']
22+
columns: CollectionPreferences['columns']
2323
id: number|string
2424
isShared: boolean
2525
relatedCollection: CollectionSlug

‎packages/plugin-import-export/src/components/FieldsToExport/index.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
importtype{ListPreferences,SelectFieldClientComponent}from'payload'
3+
importtype{CollectionPreferences,SelectFieldClientComponent}from'payload'
44
importtype{ReactNode}from'react'
55

66
import{
@@ -51,10 +51,11 @@ export const FieldsToExport: SelectFieldClientComponent = (props) => {
5151
if(id||!collectionSlug){
5252
return
5353
}
54+
5455
constdoAsync=async()=>{
5556
constcurrentPreferences=awaitgetPreference<{
56-
columns: ListPreferences['columns']
57-
}>(`${collectionSlug}-list`)
57+
columns: CollectionPreferences['columns']
58+
}>(`collection-${collectionSlug}`)
5859

5960
constcolumns=currentPreferences?.columns?.filter((a)=>a.active).map((b)=>b.accessor)
6061
setValue(columns??collectionConfig?.admin?.defaultColumns??[])

‎packages/ui/src/elements/RelationshipTable/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro
124124
newQuery.where=hoistQueryParamsToAnd(newQuery.where,filterOptions)
125125
}
126126

127-
// map columns from string[] to ListPreferences['columns']
127+
// map columns from string[] to CollectionPreferences['columns']
128128
constdefaultColumns=field.admin.defaultColumns
129129
? field.admin.defaultColumns.map((accessor)=>({
130130
accessor,

‎packages/ui/src/exports/client/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export type {
387387
/**
388388
* @deprecated
389389
* This export will be removed in the next major version.
390-
* Use `import type { ListPreferences } from 'payload'` instead.
390+
* Use `import type { CollectionPreferences } from 'payload'` instead.
391391
*/
392392
ListPreferences,
393393
}from'payload'

‎packages/ui/src/providers/ListQuery/types.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
importtype{
22
ClientCollectionConfig,
3+
CollectionPreferences,
34
ColumnPreference,
4-
ListPreferences,
55
ListQuery,
66
PaginatedDocs,
77
Sort,
@@ -25,7 +25,7 @@ export type ListQueryProps = {
2525
readonlydata: PaginatedDocs
2626
readonlydefaultLimit?: number
2727
readonlydefaultSort?: Sort
28-
readonlylistPreferences?: ListPreferences
28+
readonlylistPreferences?: CollectionPreferences
2929
readonlymodifySearchParams?: boolean
3030
readonlyonQueryChange?: OnListQueryChange
3131
readonlyorderableFieldName?: string

0 commit comments

Comments
 (0)