Skip to content

Commit

Permalink
fix: protect against falsy selectedGroup (#7593)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
ricokahler committed Oct 4, 2024
1 parent 30c046b commit 98f4eb9
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/sanity/src/core/form/store/formState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type PrepareFieldMember = <T>(props: {
field: ObjectField
parent: FormStateOptions<ObjectSchemaType, T> & {
groups: FormFieldGroup[]
selectedGroup: FormFieldGroup
selectedGroup?: FormFieldGroup
}
index: number
}) => ObjectMember | HiddenField | null
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98f4eb9

Please sign in to comment.