Unlock seamless workflows and faster delivery with our latest releases – get the details

Applying a filter to the desk structure to directly edit an active schema remotely.

11 replies
Last updated: Mar 11, 2024
trying to apply a filter somewhere in desk structure that's not in a documentList? with hopes of directly editing an active schema remotely:
  const homePage = S.listItem()
    .title('Active Home Page')
    .schemaType('siteHomepage')
    .child(
      S.editor()
        .schemaType('siteHomepage')
        .filter('_id == "globalConfig" && defined(homePage) && !(_id in path("drafts.**"))')
        .title('Active Home Page')
        
I know how to get it to work filtering into a
list view
but i want to remove an extra click here and not really seeing a way to do it, vs with a global config i can select the documentId() and jump directly into that singleton
Mar 7, 2024, 11:10 PM
You can’t filter like that, but you could pass the
child
method an async function where you query for the document. Something like:
  const homePage = .listItem()
        .title('Active Home Page')
        .child(async (value, { structureContext: { getClient } }) => {
          const client = getClient({ apiVersion: '2024-03-01' });
          const { _id, title } = await client.fetch(`
            [_id == "globalConfig" 
            && defined(homePage) 
            && !(_id in path("drafts.**"))][0]{ 
                _id, 
                title 
            }`);

          return S.editor().schemaType('siteHomepage').id(_id).title(title);
        }),
Mar 8, 2024, 1:35 AM
ah yeah i've done some async stuff for other things
Mar 8, 2024, 1:47 AM
let me play with this ty
Mar 8, 2024, 1:47 AM
got itttttt
Mar 8, 2024, 1:48 AM
so much nicer than 2 clicks 😉
Mar 8, 2024, 1:48 AM
ty
Mar 8, 2024, 1:48 AM
You’re welcome!
Mar 8, 2024, 4:48 PM
What did the final solution for this look like. Sorta banging around on the same problem. For a 1 click to editing a single document from the main list, that doesn't have a static ID and was generated, displaying and editing that document directly with 1 click is way more painful than it should be I feel.
Mar 11, 2024, 12:46 AM
oh yeah it was a tiny modified one second
Mar 11, 2024, 3:05 AM
  const homePage = S.listItem()
    .title('Active Home Page')
    .schemaType('siteHomepage')
    .child(async (value, { structureContext: { getClient } }) => {
      const client = getClient({ apiVersion: '2024-03-01' });
      const sanityQuery = await client.fetch(`
        *[_type == "globalConfig"][0]{ 
          ...,
            homePage->{
              _id,
              displayTitle
            }
        }`);
      return S.editor().schemaType('siteHomepage').id(sanityQuery?.homePage?._id).title(sanityQuery?.homePage?.displayTitle)
    })
  
Mar 11, 2024, 3:06 AM
for additional context i set a homepage in a global setting and that's what i am referencing in the async function
Mar 11, 2024, 3:07 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?