0

I am having the following issue. I have created 2 arrays named: editorDesk and adminDesk the only difference is actually, that the adminDesk has menu items => Routes, Navigation, and Friends.

The problem is I am having duplicate code to make this work. But I would like to have it simple with one desk like so: defaultDesk with some checks if is admin user show this else do nothing.

How can I make this work?

My code:

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

import sanityClient from 'part:@sanity/base/client'

import PagePreview from '../studio/src/components/preview/page/PagePreview'

import {
   FiPaperclip,
   FiUser,
   FiUsers,
   FiEdit,
   FiSettings,
   FiMessageSquare,
   FiHeart,
   FiInfo,
   FiNavigation,
   FiFileText,
   FiFile,
   FiTag,
   FiCheckSquare
} from 'react-icons/fi'

const client = sanityClient.withConfig({ apiVersion: 'v1' })

const groupQuery = '* [_type == "system.group" && $identity in members] {_id}'

const hiddenDocTypes = (listItem) =>
   ![
      'author',
      'post',
      'page',
      'category',
      'theme',
      'comment',
      'friends',
      'route',
      'navigation',
      'general',
      'featured',
      'cookie'
   ].includes(listItem.getId())

const siteMenu = () =>
   client
      .fetch(groupQuery)
      .then((docs) => docs.map((doc) => doc._id.split('.').pop()))
      .then((groupNames) => {
         const defaultDesk = [
            S.listItem()
               .title('Posts')
               .child(S.documentTypeList('post').title('Posts overview'))
               .schemaType('post')
               .icon(FiEdit),

            S.divider(),

            S.listItem()
               .title('Pages')
               .child(S.documentTypeList('page').title('Pages overview'))
               .schemaType('page')
               .icon(FiFile),

            S.divider(),

            S.listItem()
               .title('Authors')
               .child(S.documentTypeList('author').title('Authors overview'))
               .schemaType('author')
               .icon(FiUser),

            S.divider(),

            S.listItem()
               .title('Themes')
               .child(S.documentTypeList('theme').title('Themes overview'))
               .schemaType('theme')
               .icon(FiHeart),

            S.divider(),

            S.listItem()
               .title('Categories')
               .child(
                  S.documentTypeList('category').title('Categories overview')
               )
               .schemaType('category')
               .icon(FiTag),

            S.divider(),

            S.listItem()
               .title('Comments')
               .child(S.documentTypeList('comment').title('Comments overview'))
               .schemaType('comment')
               .icon(FiMessageSquare),

            S.divider()
         ]

         const editorDesk = [
            // Settings menu with sub-menu's.
            S.listItem()
               .title('Settings')
               .child(
                  S.list()
                     .title('Settings')
                     .items([
                        S.listItem()
                           .title('General')
                           .icon(FiInfo)
                           .child(
                              S.document()
                                 .schemaType('general')
                                 .documentId('generalSettings')
                                 .title('General Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Featured articles')
                           .icon(FiFileText)
                           .child(
                              S.document()
                                 .schemaType('featured')
                                 .documentId('featuredArticlesSettings')
                                 .title('Featured Articles Settings')
                           ),

                        S.divider()
                     ])
               )
               .icon(FiSettings),

            ...S.documentTypeListItems().filter(hiddenDocTypes)
         ]

         const adminDesk = [
            // Settings menu with sub-menu's.
            S.listItem()
               .title('Settings')
               .child(
                  S.list()
                     .title('Settings')
                     .items([
                        S.listItem()
                           .title('General')
                           .icon(FiInfo)
                           .child(
                              S.document()
                                 .schemaType('general')
                                 .documentId('generalSettings')
                                 .title('General Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Navigation')
                           .icon(FiNavigation)
                           .child(
                              S.document()
                                 .schemaType('navigation')
                                 .documentId('navigationSettings')
                                 .title('Navigation Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Featured articles')
                           .icon(FiFileText)
                           .child(
                              S.document()
                                 .schemaType('featured')
                                 .documentId('featuredArticlesSettings')
                                 .title('Featured Articles Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Cookie Consent')
                           .icon(FiCheckSquare)
                           .child(
                              S.document()
                                 .schemaType('cookie')
                                 .documentId('cookieSettings')
                                 .title('Cookie Consent Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Friends team')
                           .icon(FiUsers)
                           .child(
                              S.document()
                                 .schemaType('friends')
                                 .documentId('settingsFriends')
                                 .title('Friends Settings')
                           )
                     ])
               )
               .icon(FiSettings),

            ...S.documentTypeListItems().filter(hiddenDocTypes)
         ]

         if (groupNames.includes('editors')) {
            return S.list()
               .title('Website (editor)')
               .items([...defaultDesk, ...editorDesk])
         }

         if (groupNames.includes('administrator')) {
            return S.list()
               .title('Website (admin)')
               .items([...defaultDesk, ...adminDesk])
         }

         return S.list().title('Website').items(deskItems)
      })
      .catch(() => {
         // In case of any errors fetching the groups, just return some standard
         // structure. This will only happen if the query cannot be performed for
         // some reason.
         return S.list().title('Website').items([])
      })

export default siteMenu

export const getDefaultDocumentNode = (props) => {
   /**
    * Here you can define fallback views for document types without
    * a structure definition for the document node. If you want different
    * fallbacks for different types, or document values (e.g. if there is a slug present)
    * you can set up that logic in here too.
    * https://www.sanity.io/docs/structure-builder-reference#getdefaultdocumentnode-97e44ce262c9
    */
   const { schemaType } = props
   if (schemaType === 'page') {
      return S.document().views([
         S.view.form(),
         S.view.component(PagePreview).title('Preview Page')
      ])
   }

   return S.document().views([S.view.form()])
}

1 Answers1

0

Merge would work here

You could defined default desk with some structure, then

const someOtherDesk = [
   ...defaultDesk,
   restOfTheCode
]

Or if it is not that simple and it has to be merged deeper then in root level =>

const someOtherDesk = Object.assign([], defaultDesk, adminDesk);
  • creates new array, merges defaultDesk and then merges adminDesk