sanity-io / language-filter

A Sanity plugin that supports filtering localized fields by language
MIT License
17 stars 1 forks source link

Versions of language-filter above v3.0.1 don't work #47

Closed P1ayer4312 closed 1 year ago

P1ayer4312 commented 1 year ago

We recently updated Sanity and all the plugins to the latest versions. For the language filter only the dropdown menu is shown, but when I choose different languages nothing happens, only the language gets deselected from the dropdown menu and nothing else.

We previously had Sanity v3.7, now we have v3.14.1 For the language-filter any version above v3.0.1 does not work from our testing.

pgrzyb commented 1 year ago

Same thing happening to us. Currently running sanity@3.12.2 and language-filter@3.1.2 - I will downgrade to get it working again, but I will be eagerly waiting for a release that fixes this. The new UI looks neat 👍

SimeonGriggs commented 1 year ago

Are you able to log the result of your filterField function to confirm it's intercepting fields as expected?

pgrzyb commented 1 year ago

It is not. Logging inside the function yields no results - sorry, I forgot to mention this on GitHub after saying it on Slack 🤦‍♂️ Not that it helps, but just for reference: https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1688368828527809

SimeonGriggs commented 1 year ago

Got it, are your internationalized object fields just inline objects? For example:

export const localeString = defineType({
  title: 'Localized string',
  name: 'localeString',
  type: 'object',
  fields: [ ...

Or have you registered it as a custom type? And use it like so:

    defineField({
      title: 'Localized string (OBJECT)',
      name: 'localeStringObject',
      type: 'localeString',
    }),

You may need to register it as a custom type in order to show and hide the fields.

spacedawwwg commented 1 year ago

@SimeonGriggs we have the same issue, and yes we're using internationalised object fields

defineField({
  title: 'Name',
  name: 'name',
  type: 'object',
  fields: [ 
    {
      name: 'en',
      title: `Name (en)`,
      type: 'string',
    },
    {
      name: 'fr',
      title: `Name (fr)`,
      type: 'string',
    }
  ]
}),
SimeonGriggs commented 1 year ago

Can I see your filterField function as well @spacedawwwg?

spacedawwwg commented 1 year ago
export const languages: Languages = [
  {
    name: 'en_GB',
    code: 'en-GB',
    title: 'English',
    direction: 'ltr',
  },
  {
    name: 'ar_SA',
    code: 'ar-SA',
    title: 'Arabic',
    direction: 'rtl',
  },
  // etc.
  ...
]
  languageFilter({
    supportedLanguages: [
      ...languages.map(language => ({
        id: language.name,
        title: language.title,
      })),
    ],
    defaultLanguages: [languages[0].name],
    documentTypes: [],
    filterField: (_enclosingType, field, selectedLanguageIds) =>
      !languages.map(lang => lang.name).includes(field.name) ||
      selectedLanguageIds.includes(field.name),
  }),
SimeonGriggs commented 1 year ago

Using the same filterField function as yours (checking that the member name is not a language OR that it is a language and is currently selected), mine is still working in the latest version.

I can recreate the issue reported here, but can resolve it with the filterField function.

Screenshot 2023-07-18 at 14 33 00

I'd like to recreate a version of the filterField that worked in a previous version and does not in this newer one. There should not have been a breaking change between versions here for this logic.

spacedawwwg commented 1 year ago

@SimeonGriggs I cant even get filterField to log out anything

languageFilter({
  supportedLanguages: [
    ...languages.map(language => ({
      id: language.name,
      title: language.title,
    })),
  ],
  defaultLanguages: [languages[0].name],
  documentTypes: [],
  filterField: (_enclosingType, field, selectedLanguageIds) => {

    // THIS NEVER FIRES
    console.log(selectedLanguageIds)

    return (
      !languages.map(lang => lang.name).includes(field.name) ||
      selectedLanguageIds.includes(field.name)
    );

   }
}),

Am I missing something obvious that has changed?

spacedawwwg commented 1 year ago

ezgif-5-6415ff35b1

SimeonGriggs commented 1 year ago

@spacedawwwg Still working on recreating the issue with your code examples...

  1. Your documentTypes array is empty in this snippet. I'm guessing it's not in your Studio, but just want to double check.
  2. The object example uses field names en and fr but your languages array has locale/market pairs like en_GB and ar_SA – can you confirm your field names share the same values as your language ID's?

Using the schema and configuration you've supplied I can recreate the issue, but only because of a mismatch of the field names and language ID's. And the filterField function fires. So I'm trying to eliminate any other possibilities!

spacedawwwg commented 1 year ago

@SimeonGriggs

  1. documentTypes was an empty array actually, it can be removed as we moved to use the options to enable it for specific schemas instead, and this must have just been left by mistake.
  2. no, my example above using en, fr was just pseudo code - our real languages use the en_GB and ar_SA pattern. All languages are modelled like this:
export const languages: Languages = [
  {
    name: 'en_GB',
    code: 'en-GB',
    title: 'English',
    direction: 'ltr',
  },
  {
    name: 'ar_SA',
    code: 'ar-SA',
    title: 'Arabic',
    direction: 'rtl',
  },
  // etc.
  ...
]

this is the code we use to localize a field, too

export function localizeField(props: FieldDefinition): FieldDefinition {
  return {
    title: props.title,
    name: props.name,
    fieldset: props.fieldset,
    group: props.group,
    description: props.description,
    type: 'object',
    fields: [
      ...languages.map(language => ({
        ...props,
        name: language.name,
        title: `${language.title} (${language.code})`,
        type: props.type,
        fieldset: undefined,
        description: undefined,
        group: undefined,
      })),
    ],
  };
}
// usage 
localizeField({
  name: 'title',
  title: 'Title',
  type: 'string',
  validation: rule => rule.custom(requiredBaseLanguageOnly),
}),
SimeonGriggs commented 1 year ago

That's it!

Objects in document types where the configuration was on the schema – and not in the plugin config – weren't being run through the custom object input.

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 3.2.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: