Resolve-to-Publish: Comments based publish action

By Saskia Bobinska

A custom publish action that opens the comments inspector instead of publishing if unresolved comments exist

commentsPublishAction.tsx

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.

Contributor

Other recipes by the contributor