Skip to content

Instantly share code, notes, and snippets.

@ndimatteo
Created January 31, 2022 18:51
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 ndimatteo/f0b9f0620caf775d4d5f6750acfc343a to your computer and use it in GitHub Desktop.
Save ndimatteo/f0b9f0620caf775d4d5f6750acfc343a to your computer and use it in GitHub Desktop.
import React from 'react'
import defaultResolve, {
PublishAction,
DiscardChangesAction
} from 'part:@sanity/base/document-actions'
import { Eye } from 'phosphor-react'
const remoteURL = 'https://xxx.vercel.app'
const localURL = 'http://localhost:3000'
const frontendURL =
window.location.hostname === 'localhost' ? localURL : remoteURL
const singletons = [
'generalSettings',
'cookieSettings',
'headerSettings',
'footerSettings',
'seoSettings'
]
const previews = ['page', 'program']
const PreviewAction = props => {
const slug = props.draft
? props.draft.slug?.current
: props.published?.slug?.current
return {
label: 'Open Preview',
icon: () => <Eye weight="light" data-sanity-icon="eye" />,
onHandle: () => {
window.open(
`${frontendURL}/api/preview?token=YYY&type=${
props.type
}&slug=${slug || ''}`
)
}
}
}
export default function resolveDocumentActions(props) {
const isSingle = singletons.indexOf(props.type) > -1
const canPreview = previews.indexOf(props.type) > -1
if (isSingle) {
return [
PublishAction,
DiscardChangesAction,
...(canPreview ? [PreviewAction] : [])
]
}
return [...defaultResolve(props), ...(canPreview ? [PreviewAction] : [])]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment