sanity-io / sanity-plugin-internationalized-array

A plugin to register array fields with a custom input component to store field values in multiple languages, queryable by using the language ID as an array `_key`.
MIT License
41 stars 10 forks source link

Can't delete document if defaultLanguages configured #34

Closed israelroldan closed 1 year ago

israelroldan commented 1 year ago

Thanks for making this awesome plugin available! 🎉

We just found a bug that can be reproduced in this minimal test case: https://github.dev/israelroldan/sanity-i18nplugin-tc

Description of the issue

When an internationalized field is added to a document, documents of that type can't be fully deleted.

Steps to reproduce

  1. Create a new post
  2. Try to delete it
  3. The document is deleted but immediately recreated again (same id but with all fields empty)

Expected behaviour

The document is deleted

Actual result

The document is immediately recreated empty and can't be deleted

Additional information

The root cause is the code used by the plugin to add default languages. When a document is deleted the studio hooks are re-executed and the conditions present in src/components/InternationalizedArray.tsx#108-119 are applicable so the call to handleAddLanguages(defaultLanguages) takes place, effectively adding default languages to the just deleted document, forcing its re-creation.

Disabling defaultLanguages is a workaround at the moment.

israelroldan commented 1 year ago

I took a stab at it but it feels a bit of a chicken and egg situation as the plugin is doing the right thing (initializing defaultLanguages when the document is new) but it feels to be there should be a check of "has this document just been deleted?)

I wonder if this is more of a shortcoming of the studio (which stays on the just-deleted document URL) who is triggering the hooks on just-deleted documents.