Skip to content

fix: avoid Sanity 5 document-context crash in tool dialogs - #79

Open
wdtamagi wants to merge 1 commit into
ndimatteo:mainfrom
wdtamagi:fix/sanity-5-formfield-context
Open

fix: avoid Sanity 5 document-context crash in tool dialogs#79
wdtamagi wants to merge 1 commit into
ndimatteo:mainfrom
wdtamagi:fix/sanity-5-formfield-context

Conversation

@wdtamagi

Copy link
Copy Markdown

Problem

On Sanity 5, opening either of the plugin's tool dialogs (Add project or the Vercel Access Token form) crashes the Studio:

useDocumentDivergences must be used within a DocumentDivergencesContext

Root cause

Since Sanity 5, the core FormField exported from sanity renders FormDivergenceIndicator and FormNodeDivergenceDetail, and both call useDocumentDivergences() unconditionally. That hook throws when rendered outside a document pane (no DocumentDivergencesContext provider).

This plugin uses <FormField> inside its standalone tool dialogs (src/deploy-form.tsx and src/vercel-deploy.tsx), which are not inside a document pane — so the hook throws and the whole tool view errors out.

Fix

Add a small, context-free FormField shim (src/form-field.tsx) built from @sanity/ui (Stack + Text) that reproduces the same title/description layout without depending on document-editing context. Swap the two import { FormField } from 'sanity' usages to the local shim.

  • src/form-field.tsx (new)
  • src/deploy-form.tsx — import swap
  • src/vercel-deploy.tsx — import swap (keeps useColorSchemeValue from sanity)

No other sanity imports are affected; they're all context-free.

Verification

  • npm run build (pkg-utils build --strict --check) passes.
  • Manually: on a Sanity 5 Studio, opening Add project and the access-token dialog now render correctly instead of crashing.

Since Sanity 5, the core `FormField` renders `FormDivergenceIndicator`
and `FormNodeDivergenceDetail`, both of which call
`useDocumentDivergences()` unconditionally. That hook throws when
rendered outside a document pane:

  useDocumentDivergences must be used within a DocumentDivergencesContext

This plugin renders `<FormField>` inside its standalone tool dialogs
(the 'Add project' and access-token forms), so opening either dialog
crashes the Studio on Sanity 5.

Replace the core `FormField` with a local, context-free shim built from
@sanity/ui (Stack + Text) that preserves the same title/description
layout without depending on document-editing context.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant