diff --git a/.changeset/heavy-timers-design.md b/.changeset/heavy-timers-design.md new file mode 100644 index 00000000000..37ef8528cd7 --- /dev/null +++ b/.changeset/heavy-timers-design.md @@ -0,0 +1,14 @@ +--- +'@keystone-next/keystone': minor +--- + +The following config functions now also access a `context` argument: + - `list.ui.isHidden` + - `list.ui.hideCreate` + - `list.ui.hideDelete` + - `list.ui.createView.defaultFieldMode` + - `list.ui.itemView.defaultFieldMode` + - `list.ui.listView.defaultFieldMode` + - `field.ui.createView.fieldMode` + - `field.ui.itemView.fieldMode` + - `field.ui.listView.fieldMode` diff --git a/docs/pages/docs/apis/fields.mdx b/docs/pages/docs/apis/fields.mdx index 501ae98dbf8..12ae5fa6126 100644 --- a/docs/pages/docs/apis/fields.mdx +++ b/docs/pages/docs/apis/fields.mdx @@ -77,15 +77,15 @@ Options: - `ui`: Controls how the field is displayed in the Admin UI. - `views`: A [resolved](https://nodejs.org/api/modules.html#modules_require_resolve_request_options) path to a module containing code to replace or extend the default Admin UI components for this field. See the [Custom Field Views](../guides/custom-field-views) guide for details on how to use this option. - `createView` (default: `'edit'`): Controls the create view page of the Admin UI. - Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['edit', 'hidden']`. + Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['edit', 'hidden']`. Defaults to the list's `ui.createView.defaultFieldMode` config if defined. See the [Schema API](./schema#ui) for details. - `itemView` (default: `'edit'`): Controls the item view page of the Admin UI. - Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, item }` that returns one of `['edit', 'read', 'hidden']`. + Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, context, item }` that returns one of `['edit', 'read', 'hidden']`. Defaults to the list's `ui.itemView.defaultFieldMode` config if defined. See the [Schema API](./schema#ui) for details. - `listView` (default: `'read'`): Controls the list view page of the Admin UI. - Can be one of `['read', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['read', 'hidden']`. + Can be one of `['read', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['read', 'hidden']`. Defaults to the list's `ui.listView.defaultFieldMode` config if defined. See the [Schema API](./schema#ui) for details. - `graphql`: Configures certain aspects of the GraphQL API. @@ -109,13 +109,13 @@ export default config({ ui: { views: require.resolve('path/to/viewsModule.tsx'), createView: { - fieldMode: ({ session }) => 'edit', + fieldMode: ({ session, context }) => 'edit', }, itemView: { - fieldMode: ({ session, item }) => 'read', + fieldMode: ({ session, context, item }) => 'read', }, listView: { - fieldMode: ({ session }) => 'read', + fieldMode: ({ session, context }) => 'read', }, }, graphql: { diff --git a/docs/pages/docs/apis/schema.mdx b/docs/pages/docs/apis/schema.mdx index e518b9bf6ed..97c3a7b336e 100644 --- a/docs/pages/docs/apis/schema.mdx +++ b/docs/pages/docs/apis/schema.mdx @@ -81,26 +81,26 @@ Options: By default, the `labelField` is used if it has a string `contains` filter, otherwise none. - `description` (default: `undefined`): Sets the list description displayed in the Admin UI. - `isHidden` (default: `false`): Controls whether the list is visible in the navigation elements of the Admin UI. - Can be either a boolean value, or an async function with an argument `{ session }` that returns a boolean value. + Can be either a boolean value, or an async function with an argument `{ session, context }` that returns a boolean value. - `hideCreate` (default: `false`): Controls whether the `create` button is available in the Admin UI for this list. - Can be either a boolean value, or an async function with an argument `{ session }` that returns a boolean value. + Can be either a boolean value, or an async function with an argument `{ session, context }` that returns a boolean value. - `hideDelete` (default: `false`): Controls whether the `delete` button is available in the Admin UI for this list. - Can be either a boolean value, or an async function with an argument `{ session }` that returns a boolean value. + Can be either a boolean value, or an async function with an argument `{ session, context }` that returns a boolean value. - `createView`: Controls the create view page of the Admin UI. - `defaultFieldMode` (default: `'edit'`): Can be overridden by per-field values in the `field.ui.createView.fieldMode` config. See the [Fields API](./fields#common-configuration) for details. - Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['edit', 'hidden']`. + Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['edit', 'hidden']`. - `itemView`: Controls the item view page of the Admin UI. - `defaultFieldMode` (default: `'edit'`): Can be overridden by per-field values in the `field.ui.itemView.fieldMode` config. See the [Fields API](./fields#common-configuration) for details. - Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, item }` that returns one of `['edit', 'read', 'hidden']`. + Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, context, item }` that returns one of `['edit', 'read', 'hidden']`. - `listView`: Controls the list view page of the Admin UI. - `defaultFieldMode` (default: `'read'`): Controls the default mode of fields in the list view. Can be overridden by per-field values in the `field.ui.listView.fieldMode` config. See the [Fields API](./fields#common-configuration) for details. - Can be one of `['read', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['read', 'hidden']`. + Can be one of `['read', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['read', 'hidden']`. - `initialColumns` (default: The first three fields defined in the list). A list of field names to display in columns in the list view. By default only the label column, as determined by `labelField`, is shown. - `initialSort` (default: `undefined`): Sets the field and direction to be used to initially sort the data in the list view. Option `field` is the name of the field to sort by, and `direction` is either `'ASC'` or `'DESC'` for ascending and descending sorting respectively. @@ -119,17 +119,17 @@ export default config({ labelField: 'name', searchFields: ['name', 'alternativeName'], description: '...', - isHidden: ({ session }) => false, - hideCreate: ({ session }) => false, - hideDelete: ({ session }) => false, + isHidden: ({ session, context }) => false, + hideCreate: ({ session, context }) => false, + hideDelete: ({ session, context }) => false, createView: { - defaultFieldMode: ({ session }) => 'edit', + defaultFieldMode: ({ session, context }) => 'edit', }, itemView: { - defaultFieldMode: ({ session, item }) => 'edit', + defaultFieldMode: ({ session, context, item }) => 'edit', }, listView: { - defaultFieldMode: ({ session }) => 'read', + defaultFieldMode: ({ session, context }) => 'read', initialColumns: ['name', /* ... */], initialSort: { field: 'name', direction: 'ASC' }, pageSize: 50, diff --git a/packages/keystone/src/admin-ui/system/getAdminMetaSchema.ts b/packages/keystone/src/admin-ui/system/getAdminMetaSchema.ts index 1360aa1d6b8..fbf546cc0de 100644 --- a/packages/keystone/src/admin-ui/system/getAdminMetaSchema.ts +++ b/packages/keystone/src/admin-ui/system/getAdminMetaSchema.ts @@ -126,7 +126,10 @@ export function getAdminMetaSchema({ const sessionFunction = lists[rootVal.listKey].fields[rootVal.fieldPath].ui?.createView?.fieldMode ?? listConfig.ui?.createView?.defaultFieldMode; - return runMaybeFunction(sessionFunction, 'edit', { session: context.session }); + return runMaybeFunction(sessionFunction, 'edit', { + session: context.session, + context, + }); }, }), }, @@ -161,7 +164,10 @@ export function getAdminMetaSchema({ const sessionFunction = lists[rootVal.listKey].fields[rootVal.fieldPath].ui?.listView?.fieldMode ?? listConfig.ui?.listView?.defaultFieldMode; - return runMaybeFunction(sessionFunction, 'read', { session: context.session }); + return runMaybeFunction(sessionFunction, 'read', { + session: context.session, + context, + }); }, }), }, @@ -225,6 +231,7 @@ export function getAdminMetaSchema({ } return runMaybeFunction(sessionFunction, 'edit', { session: context.session, + context, item, }); }); @@ -274,7 +281,10 @@ export function getAdminMetaSchema({ ); } const listConfig = config.lists[rootVal.key]; - return runMaybeFunction(listConfig.ui?.hideCreate, false, { session: context.session }); + return runMaybeFunction(listConfig.ui?.hideCreate, false, { + session: context.session, + context, + }); }, }), hideDelete: graphql.field({ @@ -286,7 +296,10 @@ export function getAdminMetaSchema({ ); } const listConfig = config.lists[rootVal.key]; - return runMaybeFunction(listConfig.ui?.hideDelete, false, { session: context.session }); + return runMaybeFunction(listConfig.ui?.hideDelete, false, { + session: context.session, + context, + }); }, }), path: graphql.field({ type: graphql.nonNull(graphql.String) }), @@ -312,7 +325,10 @@ export function getAdminMetaSchema({ ); } const listConfig = config.lists[rootVal.key]; - return runMaybeFunction(listConfig.ui?.isHidden, false, { session: context.session }); + return runMaybeFunction(listConfig.ui?.isHidden, false, { + session: context.session, + context, + }); }, }), }, diff --git a/packages/keystone/src/types/config/lists.ts b/packages/keystone/src/types/config/lists.ts index f54128cda8b..b15f2723224 100644 --- a/packages/keystone/src/types/config/lists.ts +++ b/packages/keystone/src/types/config/lists.ts @@ -1,5 +1,6 @@ import type { CacheHint } from 'apollo-server-types'; import type { BaseGeneratedListTypes, MaybePromise } from '../utils'; +import type { KeystoneContext } from '../context'; import type { ListHooks } from './hooks'; import type { ListAccessControl } from './access-control'; import type { BaseFields, FilterOrderArgs } from './fields'; @@ -172,12 +173,13 @@ export type ListAdminUIConfig< export type MaybeSessionFunction = | T - | ((args: { session: any }) => MaybePromise); + | ((args: { session: any; context: KeystoneContext }) => MaybePromise); export type MaybeItemFunction = | T | ((args: { session: any; + context: KeystoneContext; item: { id: string | number; [path: string]: any }; }) => MaybePromise);