Skip to content

Instantly share code, notes, and snippets.

@SimeonGriggs
Last active February 7, 2023 20:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SimeonGriggs/f4954ced2b1313278eaef5feab5ce4c4 to your computer and use it in GitHub Desktop.
Save SimeonGriggs/f4954ced2b1313278eaef5feab5ce4c4 to your computer and use it in GitHub Desktop.
Sanity.io – Customize Desk Structure based on User Roles
import S from '@sanity/desk-tool/structure-builder'
import userStore from 'part:@sanity/base/user'
// Get the logged in user
const getCurrentUser = () => {
userStore.me.subscribe((user) => {
// Instead of a local variable, we use this window object to re-use it through the Studio
if (user) {
window._sanityUser = user ?? undefined
}
})
}
getCurrentUser()
// Create different sets of List Items for different roles
const adminItems = [S.documentTypeListItem('page').title('Pages')]
const defaultItems = [S.documentTypeListItem('article').title('Articles')]
// Setup desk structure
export default () => {
return S.list()
.title('Content')
.items(
// Display List Items based on Role
window?._sanityUser?.roles.find((role) => role.name === 'administrator')
? [...adminItems, ...defaultItems]
: defaultItems
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment