Last active
February 7, 2023 20:33
-
-
Save SimeonGriggs/f4954ced2b1313278eaef5feab5ce4c4 to your computer and use it in GitHub Desktop.
Sanity.io – Customize Desk Structure based on User Roles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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