How to list and group image asset documents

By Knut Melvær

Add lists of image assets based on information in the asset document using the Structure Builder API

assetsStructure.js

import React from 'react'
import S from '@sanity/desk-tool/structure-builder'

const AssetPreview = ({ document }) => {
  const { displayed } = document
  return (
    displayed.url && (
      <div style={{ padding: '1em' }}>
        <img src={`${displayed.url}?w=600`} style={{ maxWidth: '100%' }} />
      </div>
    )
  )
}
const AssetDoc = assetId =>
  S.document()
    .documentId(assetId)
    .views([
      S.view.component(AssetPreview).title('Image preview'),
      S.view.form().title('Meta-information')
    ])

const assetsStructure = S.listItem()
  .title('Assets')
  .child(
    S.list()
      .title('Assets')
      .items([
        S.listItem()
          .title('All images')
          .child(S.documentTypeList('sanity.imageAsset').child(AssetDoc)),
        // List images with width over 1000px
        S.listItem()
          .title('Large images (1000px+)')
          .child(
            S.documentList()
              .title('Large images')
              .filter(
                '_type == "sanity.imageAsset" && metadata.dimensions.width > 1000'
              )
              .child(AssetDoc)
          ),
        // List images with the file extension of “gif”
        S.listItem()
          .title('GIFs')
          .child(
            S.documentList()
              .title('GIFs')
              .filter('_type == "sanity.imageAsset" && extension == "gif"')
              .child(AssetDoc)
          ),
        // List images that has been uploaded with the unsplash asset selector
        S.listItem()
          .title('From Unsplash')
          .child(
            S.documentList()
              .title('From Unsplash')
              .filter(
                '_type == "sanity.imageAsset" && source.name == "unsplash"'
              )
              .child(AssetDoc)
          )
      ])
  )
  
 export default assetsStructure

deskStructure.js

import S from '@sanity/desk-tool/structure-builder'

import assetsStructure from './assetsStructure'
  
export default () => S.list()
  .title('Content')
  .items([
    ...S.documentTypeListItems(),
    assetsStructure
  ])

Example of how to list images in the studio desk tool based on different filters on the information in the asset documents using the Structure Builder API.

Contributor

Other recipes by the contributor

First Published Timestamp Function

Featured contribution
Official(made by Sanity team)

Automatically track when content was first published with a timestamp that sets once and never overwrites, providing reliable publication history for analytics and editorial workflows.

Knut Melvær
Go to First Published Timestamp Function

Automatically tag blog posts

Featured contribution
Official(made by Sanity team)

AI-powered automatic tagging for Sanity blog posts that analyzes content to generate 3 relevant tags, maintaining consistency by reusing existing tags from your content library.

Go to Automatically tag blog posts