Saskia Bobinska
Senior Support Engineer @Sanity
A custom publish action that opens the comments inspector instead of publishing if unresolved comments exist
import {ErrorOutlineIcon} from '@sanity/icons'
import {useCallback, useMemo} from 'react'
import {
COMMENTS_INSPECTOR_NAME,
DocumentActionComponent,
DocumentActionProps,
useComments,
} from 'sanity'
import {useDocumentPane} from 'sanity/structure'
/** Custom publish action that checks for open comments and changes the action to open the comments inspector instead of publishing */
export function commentsPublishAction(
originalAction: DocumentActionComponent,
): DocumentActionComponent {
// ** Since this is a component (React) you do not have to make any async calls but instead can use a state and effect to fetch any data you need
return (props: DocumentActionProps) => {
const originalResult = originalAction(props)
// * Document Pane enables us to open the comments pane
const {inspector, openInspector} = useDocumentPane()
// * Small handler to open comments inspector
const handleOpenCommentsInspector = useCallback(() => {
if (inspector && inspector.name === COMMENTS_INSPECTOR_NAME) return
openInspector(COMMENTS_INSPECTOR_NAME)
}, [inspector, openInspector])
// * fetch comments -> no async needed
const comments = useComments()
const hasOpenComments = useMemo(() => comments.comments.data.open.length > 0, [comments])
return {
//use all the props from the original publish action. This gives us validation etc.
...originalResult,
label: hasOpenComments ? 'Resolve comments' : 'Publish with comments',
icon: hasOpenComments ? ErrorOutlineIcon : originalResult?.icon,
tone: hasOpenComments ? 'caution' : originalResult?.tone,
onHandle: hasOpenComments
? () => {
return handleOpenCommentsInspector()
}
: originalResult?.onHandle,
}
}
}
Comments are a powerful (and often overlooked) tool for Content Operations: they help teams catch loose ends and align on changes before anything goes live.
In approval workflows, comments are especially useful for requesting updates, clarifying intent, and tracking decisions. This custom document action checks a document for unresolved comments and, if any exist, opens the Comments inspector instead of publishing - so reviewers have to resolve outstanding feedback before moving forward.
Senior Support Engineer @Sanity
Use a migration script to update all references from one document to another
Go to Update references via Content Migration scriptIf you install plugins or other packages with conflicting (peer) dependencies, you can use this useful workaround to unblock yourself.
Go to What to do when Studio crashes due to conflicts in dependency versions?When creating custom document actions, it can be necessary to check, wether all validation rules are full-filled.
Go to Use the validation status of a document in your custom document actionsOnly return a value when a condition is fulfilled
Go to Conditional values in GROQ queries