From 98f4eb91710905a557b18e94d1e86baefde6a39b Mon Sep 17 00:00:00 2001 From: Rico Kahler Date: Fri, 4 Oct 2024 15:03:29 -0500 Subject: [PATCH] fix: protect against falsy `selectedGroup` (#7593) ### Description This fixes a bug that caused the document editor to crash in certain cases where there is no selected group. This occurs when the default field group is not found. Allow this value to be `undefined` and checking for it fixes the issue. ### What to review Does this correct protect against all cases where the selectedGroup is undefined? Are all the types updated correctly? ### Testing This was tested manually in the admin studio. I built sanity package, packed it, and install it in the admin studio and the issue was resolved. ### Notes for release Fixes a bug that caused the document editor to crash when the default field group is not found. --- packages/sanity/src/core/form/store/formState.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/sanity/src/core/form/store/formState.ts b/packages/sanity/src/core/form/store/formState.ts index 0968ab4db91..1cd71aaf3be 100644 --- a/packages/sanity/src/core/form/store/formState.ts +++ b/packages/sanity/src/core/form/store/formState.ts @@ -79,7 +79,7 @@ type PrepareFieldMember = (props: { field: ObjectField parent: FormStateOptions & { groups: FormFieldGroup[] - selectedGroup: FormFieldGroup + selectedGroup?: FormFieldGroup } index: number }) => ObjectMember | HiddenField | null @@ -117,8 +117,12 @@ function isFieldEnabledByGroupFilter( // the groups config for the "enclosing object" type groupsConfig: FormFieldGroup[], fieldGroup: string | string[] | undefined, - selectedGroup: FormFieldGroup, + selectedGroup: FormFieldGroup | undefined, ) { + if (!selectedGroup) { + return false + } + if (selectedGroup.name === ALL_FIELDS_GROUP.name) { return true } @@ -827,7 +831,7 @@ export function createPrepareFormState({ }, ) - const selectedGroup = groups.find((group) => group.selected)! + const selectedGroup = groups.find((group) => group.selected) // note: this is needed because not all object types gets a ´fieldsets´ property during schema parsing. // ideally members should be normalized as part of the schema parsing and not here