Rune Botten
I find solutions
Rune is located at San Francisco
Show different document lists based on a user's role
import S from '@sanity/base/structure-builder'
import userStore from "part:@sanity/base/user";
export default () =>
// Get the current user and their roles
userStore.getCurrentUser().then((user) => user.roles)
.then(roles => {
// We just want their names (identifiers)
const roleNames = roles.map(r => r.name)
// Build up an array of items depending on roles. You may of
// course do this completely different. This is just an example.
const deskItems = []
if (roleNames.includes('administrator')) {
// Return the default struture with all document types
return S.list()
.title('Admin structure')
.items(S.documentTypeListItems())
}
if (roleNames.includes('editors')) {
// Add the items that editors should see
//deskItems.push(...partOfStructureOnlyForEditors)
}
if (roleNames.includes('wizards')) {
// Add the items that wizards should see
//deskItems.push(...wizardDeskItems)
}
if (roleNames.includes('translators')) {
// Completely separate desk structure
return S.list().title('Translations').items(
[] // Would contain items only for translators, for instance
)
}
return S.list().title('Content').items(
deskItems
)
})
.catch((error) => {
console.error(error)
// 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('Empty structure')
.items([])
})
An example of how to use the Structure Builder API to return a specified desk structure based on the group(s) a logged-in user belongs to. It fetches the group documents based on the identity and builds the structure array based on the different groups. You need to specify what these desk structures are yourself to make this script work.
I find solutions
A script to validate that your schema is free of errors and warnings
Go to Validate schema scriptHow to group documents by their publishedAt year using the Structure Builder API
Go to Group documents by yearAn example of overriding the default Studio Desk pane to add some business logic around menu items.
Go to Custom default desk pane