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
42 stars 10 forks source link

feat: dynamic languages based on document #7

Closed gaauwe closed 1 year ago

gaauwe commented 1 year ago

Great work so far on this plugin @SimeonGriggs! Loading the languages asynchronously helps us a lot. In our case (as discussed with Tom in our Sanity x Belsimpel trial), we needed to load languages based on data in a document, so I want to take this opportunity to share this feature.

Feature

I looked into multiple options for providing the document as parameter to the language callback, but the tricky thing is that the document changes a lot while editing. This made it so that the languages kept reloading and reloading. In the end, I wound up building a solution that's very similar to Sanity list previews. You can select your own fields in the document that should trigger a reload, and you get those fields directly in the language callback:

{
    select: {
        markets: 'markets',
    },
    languages: (client, { markets }) =>
        client.fetch(groq`*[_type == "language" && market in $markets]{id,title}`, { markets }),
}

Possible optimization

This works great already, but I have a small optimization in mind that I could use your help on. In our case, we want to load languages based on an array of references. The minor issue that we're seeing is that once you add an item to the array, it updates the document with an empty reference already before you choose a value: UI, data. Ideally we'd only want to reload languages if an actual reference value is filled in, do you know if there is a good way to fix this?

SimeonGriggs commented 1 year ago

Thanks for this @gaauwe! A feature I'd wanted myself :)

I've tagged @stipsan on this since he added the function to fetch Languages and may have thoughts on the work here.

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 1.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: