🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

How to add UnpublishAction in Sanity studio document actions

7 replies
Last updated: Feb 17, 2022
Hi all, I'm trying to unpublish a document in Sanity studio but I don't seem to have the option to do so?Dependencies and versions:

    "@sanity/base": "^2.25.4",
    "@sanity/color-input": "^2.25.4",
    "@sanity/components": "^2.14.0",
    "@sanity/core": "^2.25.0",
    "@sanity/default-layout": "^2.25.4",
    "@sanity/default-login": "^2.24.1",
    "@sanity/desk-tool": "^2.25.4",
    "@sanity/vision": "^2.25.4",
This is what my options look like:
Feb 17, 2022, 10:06 PM
I don't think so. There's another document that does allow me to unpublish it, a location group that is referenced by the above document I am trying to unpublish. This location group document is published, and it only prevents me from unpublishing it due to it being referenced by the location document.
Feb 17, 2022, 10:17 PM
Thanks for that additional context. I also edited my comment above as you’re right—it is published.
Feb 17, 2022, 10:17 PM
What does your document actions file look like for the top case?
Feb 17, 2022, 10:19 PM
This what it looks like:
import defaultResolve, {
  PublishAction,
  DiscardChangesAction,
  DeleteAction,
  DuplicateAction,
} from "part:@sanity/base/document-actions";
import { FiEye } from "react-icons/fi";

const previewSecret = process.env.SANITY_STUDIO_PREVIEW_SECRET;
const localURL = "<http://localhost:3000>";
const baseUrl = window.location.hostname === "localhost" ? localURL : "<https://ivee.vercel.app>";

const singletons = ["pageHome", "jobsPage", "blogIndex", "settingsGeneral", "settingsSeo", "emailModal"];
const editAndDelete = ["person", "service", "page", "pageCollection", "blogPost", "location", "service"];
const previews = [
  "pageHome",
  "job",
  "jobsPage",
  "blogIndex",
  "service",
  "page",
  "location",
  "pageCollection",
  "blogPost",
];

const PreviewAction = (props) => {
  const slug = props.draft
    ? props.draft.content?.slug?.current || props.draft.slug?.current
    : props.published?.content?.slug?.current || props.published?.slug?.current;

  return {
    label: "Open Preview",
    icon: FiEye,
    onHandle: () => {
      window.open(`${baseUrl}/api/preview?token=${previewSecret}&type=${props.type}&slug=${slug || ""}`);
    },
  };
};

export default function resolveDocumentActions(props) {
  const isSingle = singletons.indexOf(props.type) > -1;
  const canEditDelete = editAndDelete.indexOf(props.type) > -1;
  const canPreview = previews.indexOf(props.type) > -1;

  if (isSingle) {
    return [PublishAction, DiscardChangesAction, ...(canPreview ? [PreviewAction] : [])];
  }

  if (canEditDelete) {
    return [PublishAction, DiscardChangesAction, DuplicateAction, DeleteAction, ...(canPreview ? [PreviewAction] : [])];
  }

  return [...defaultResolve(props), ...(canPreview ? [PreviewAction] : [])];
}
Feb 17, 2022, 10:22 PM
I believe you’ll want to import UnpublishAction and add it to your arrays at the bottom. See here for an example.
Feb 17, 2022, 10:24 PM
Thank you for the help! I'm still fairly new to Sanity and am working within this already existing codebase. That is indeed the fix :)
Feb 17, 2022, 10:27 PM
That’s great! 🎉
Unless you have a good reason to do so, you might want to spread
defaultResolve(props)
and then add your additional actions, such as is done here . That way, you don’t need to worry if Sanity updates the defaults. Of course, there are good cases for listing them out manually, too. 🙂
Feb 17, 2022, 10:30 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?