Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created January 21, 2023 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmelve/b4fb4c388bed58a0942aae4101f7d7fb to your computer and use it in GitHub Desktop.
Save kmelve/b4fb4c388bed58a0942aae4101f7d7fb to your computer and use it in GitHub Desktop.
import {defineConfig, type Role} from 'sanity'
import {deskTool} from 'sanity/desk'
import {visionTool} from '@sanity/vision'
import {schemaTypes} from './schemas'
export const EDITOR_TYPES = ['post']
const isAdmin = (roles: Role[]) => !roles?.find(({name = ''}) => name === 'administrator')
export default defineConfig({
name: 'default',
title: 'test',
projectId: 'kh2fp3g7',
dataset: 'production',
plugins: [
deskTool({
name: 'default',
title: 'test',
structure: (S, context) => {
const roles = context.currentUser?.roles as Role[]
return S.list()
.title(isAdmin(roles) ? 'Content' : 'Editorial content')
.items(
S.documentTypeListItems().filter(
(item) => isAdmin(roles) || EDITOR_TYPES.includes(item.getId() || '')
)
)
},
}),
visionTool(),
],
document: {
actions: (prev, {currentUser}) => {
if (isAdmin(currentUser?.roles as Role[])) {
return prev
}
return []
},
newDocumentOptions: (prev, {currentUser, creationContext}) => {
if (isAdmin(currentUser?.roles as Role[])) {
return prev
}
return [
{
id: 'post',
templateId: 'post',
type: 'post',
},
]
},
},
studio: {
components: {
// We don't get the current user in context here, so a hack might be to set it to a global variable on the window object and then read it here
toolMenu: (props) => {
/* const roles = props.currentUser?.roles as Role[]
if (isAdmin(roles)) {
} */
return props.renderDefault(props)
},
},
},
schema: {
// We don't get the current user in context here, so a hack might be to set it to a global variable on the window object and then read it here
types: schemaTypes.map((type) => {
if (type.type === 'document' && !EDITOR_TYPES.includes(type.name)) {
return {...type, __experimental_omnisearch_visibility: false}
}
return type
}),
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment