πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Question about desk structure and refreshing when new documents are created.

4 replies
Last updated: Aug 24, 2023
Hey all! I've got a question about desk structure. I'm trying to create a list of documents without the top-level "add new document" and "menu" items (working around i18n not working for
documentID
for one-off content types). I've got it working querying for the content type I want and building, but it doesn't refresh when a new document that matches that criteria is created. Here's a snippet of the code:
const microcopy = await context
    .getClient({ apiVersion: '2023-08-24' })
    .fetch('*[_type == "microcopy"]');
Later, in the desk builder:

S.list().items(microcopy.map((item) =>
      S.documentListItem().id(item._id).schemaType(type))
Aug 24, 2023, 4:06 PM
Sounds like you will need to add some sort of listener for events when new documents are added. Perhaps take a look at the useDocumentStore hook?
Returns an observable that can be used to listen for changes and events to documents in the current project.
Aug 24, 2023, 4:56 PM
Thanks, unfortunately because this is my desk structure it doesn't appear to be a React component I can use hooks with?
Aug 24, 2023, 5:57 PM
The context has documentStore, but I can't use the
useMemoObservable
to get the value of the observable
Aug 24, 2023, 6:01 PM
Ok! I got it working! Your tip on
documentStore
is what sealed it. Turns out there's a sample of this working inside a studio test
https://github.com/sanity-io/sanity/blob/c5bfa12f8f5e8caffe717287b81cc5d0a43e231e/dev/test-studio/structure/resolveStructure.ts#L430C10-L450
Aug 24, 2023, 6:11 PM

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?