🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Is it possible to mix explicitly defined list items with a dynamic documentList in the structure builder?

3 replies
Last updated: Jun 10, 2021
In the structure builder, is it possible to mix explicitly defined list items with a dynamic documentList?For example showing a list item at the top of the pane for categories, and then the list of all posts below, instead of having to drill down one more level to "all posts".
Jun 7, 2021, 1:57 AM
Wondering the same thing!
Jun 9, 2021, 11:45 AM
User I think it may be possible by defining the structure asynchronously , and then manually creating an array of items with the categories one first, and the document items following, instead of using a built in structure builder method. Here's something similar I did:
S.listItem()
  .title('Child Pages by Parent')
  .icon(RiPagesLine)
  .child(() =>
    documentStore.listenQuery(`*[_type == "page" && defined(content.main.parent._ref)]{'parent': content.main.parent->}`).pipe(
      map(allParentPages => {
        // Filter array for unique values since the query will return
        // the parents of all children so there will be duplicates
        const parents = allParentPages
          .filter(
            (parentDocument, index, array) =>
              array
                .map(mapObj => mapObj._id)
                .indexOf(parentDocument._id) === index,
          )
          .map(uniqueParents => uniqueParents.parent);

        const parentListItems = parents.map(parent =>
          S.listItem()
            .title(parent.content.main.title)
            .icon(RiPagesLine)
            .child(
              S.documentList()
                .title(parent.content.main.title)
                .schemaType('page')
                .filter('content.main.parent._ref == $parentId')
                .params({
                  parentId: parent._id,
                })
                .child(S.document().views(Views())),
            ),
        );

        return S.list()
          .title('Child Pages by Parent')
          .items(parentListItems);
      }),
    ),
  ),
Jun 9, 2021, 4:57 PM
Thank You Nic! Will give this a try 👍
Jun 10, 2021, 7:57 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?