sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.27k stars 427 forks source link

When setting `schemaDefinition.preview.select.media` to array item like `gallery.0`, the first item is always undefined #4107

Open antitoxic opened 1 year ago

antitoxic commented 1 year ago

Description & Reproduction

When you define schema like so, the preview image is not appearing even if there are images in gallery prop:

const projectSchema: SchemaTypeDefinition = {
  name: 'project',
  title: 'Project',
  type: 'document',
  // icon: Contacts,
  preview: {
    select: {
      media: 'gallery.0',
    },
  },
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      name: 'year',
      title: 'Year',
      type: 'number',
    },
    {
      name: 'location',
      title: 'Location',
      type: 'string',
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 100,
      },
    },
    {
      name: 'is_highlighted',
      title: 'Highlighted',
      type: 'boolean',
      options: {
        layout: 'checkbox',
      },
    },
    {
      name: 'is_finished',
      title: 'Finished',
      type: 'boolean',
      options: {
        layout: 'checkbox',
      },
    },
    {
      name: 'overview',
      title: 'Overview',
      type: 'blockContent',
    },
    {
      title: 'Tags',
      name: 'tags',
      type: 'array',
      of: [{ type: 'string' }],
      options: {
        layout: 'tags',
      },
    },
    {
      name: 'categories',
      title: 'Categories',
      type: 'array',
      validation: Rule => Rule.unique(),
      of: [
        {
          type: 'reference',
          to: [{ type: 'category' }],
        },
      ],
    },
    {
      name: 'gallery',
      title: 'Image gallery',
      type: 'array',
      of: [
        {
          type: 'image',
          fields: [
            {
              name: 'caption',
              type: 'string',
              title: 'Caption',
            },
          ],
          options: {
            hotspot: true,
          },
        },
      ],
    },
  ],
};
SCR-20230119-ib4

Expected behavior

Show image as preview as per documentation. Same thing is working in V2.

Which versions of Sanity are you using?

> npx sanity versions
@sanity/cli (global)  3.2.4 (up to date)
@sanity/vision        3.2.4 (up to date)
sanity                3.2.4 (up to date)

What operating system are you using?

ProductName:        macOS
ProductVersion:     13.1
BuildVersion:       22C65

Which versions of Node.js / npm are you running?

> npm -v && node -v
8.19.2
v18.12.1

Additional context When I try to select the entire collection gallery it seems that the first item is always empty:

SCR-20230119-ifu

I tried:

  1. Uploading a new image. Din't work
  2. Selecting existing. Didn't work
  3. Reordering images. Didn't work
  4. Creating a new document and adding new images. Still didn't work
jakobhenner commented 9 months ago

I have the same problem!

import {defineType, defineArrayMember} from 'sanity'

export default defineType({
  name: 'gallery',
  title: 'Gallery',
  type: 'object',
  fields: [
    {
      name: 'photos',
      type: 'array',
      options: {
        layout: 'grid',
      },
      of: [
        defineArrayMember({
          type: 'photo',
        }),
      ],
    },
  ],
  preview: {
    select: {
      photos: 'photos',
      photo: 'photos.0',
    },
    prepare(selection) {
      const {photos, photo} = selection

      return {
        title: 'Gallery',
        subtitle: `${Object.keys(photos).length} photos`,
        media: photo,
      }
    },
  },
})

Console

image

If I prepend a new image, the previous image a index 0 (now at 1) shows up, but first entry is always undefined.

FESTUSNIX commented 5 months ago

Same problem, did you find a solution?