Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Display a document count in the title of a pane in Structure Builder

By RD Pennell

Add a count of documents to your Structure Builder panes

structure.js

import { map } from 'rxjs/operators';

export const structure = (S, context) => {
  const { currentUser, dataset, projectId, documentStore, getClient, schema } = context;
    
    return S.list()
    .title('Sanity Loves Content')
    .items([
      S.listItem()
        .title('<type>')
        .child(
          documentStore
            .listenQuery(`count(*[_type == '<type>'])`)
            .pipe(
              map(count =>
                S.documentTypeList('<type>').title(`Total documents: ${count}`)
              )
            )
        ),
    ]);
};

Within the Community Slack, we're often asked how to show the count of a given document type within a custom Desk structure. The documentStore and rxjs allow us to display a count inside of our title that updates in real time.

Contributor

Other schemas by author