fix: initialize array fields with empty array to prevent insert patch error #105
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes an issue where pasting content into empty internationalized Portable Text fields causes an error: "Attempt to apply insert patch to non-array value".
The Problem
The bug only affects the document-level "Add translation" button when
buttonLocationsincludes'document'. Field-level buttons andunstable_fieldActionbuttons work correctly.When using the document-level button to add a new language entry for array-based fields (like Portable Text), the
valuefield was initialized asundefined. This causes Sanity's patch operations to fail when trying to insert content into what it expects to be an array.The Solution
The fix modifies two components:
1.
DocumentAddButtons.tsxgetInitialValueForTypefunction that uses theuseSchemahook to detect array-based fields[]instead ofundefinedjsonTypeis 'array'2.
InternationalizedInput.tsxwrappedOnChangehandler to intercept problematic insert patchesTesting
Tested locally with:
internationalizedArrayBodyfieldsRelated Issue
Fixes #104
Changes Made
src/components/DocumentAddButtons.tsxto detect array fields and initialize them properlysrc/components/InternationalizedInput.tsxto handle paste operations into empty fields