generated from scwambach/nextjs-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultDocumentNode.ts
59 lines (54 loc) · 1.62 KB
/
defaultDocumentNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { SanityDocument } from 'next-sanity'
import { Iframe } from 'sanity-plugin-iframe-pane'
import {
DefaultDocumentNodeContext,
DocumentBuilder,
StructureBuilder,
} from 'sanity/structure'
type DefaultDocumentNodeResolver = (
S: StructureBuilder,
options: DefaultDocumentNodeContext
) => DocumentBuilder | null | undefined
// Customise this function to show the correct URL based on the current document
function getPreviewUrl(doc: SanityDocument) {
return doc?.slug?.current
? `${process.env.SITE_URL}/${doc.slug.current}`
: `${process.env.SITE_URL}`
}
// Import this into the deskTool() plugin
export const defaultDocumentNode: DefaultDocumentNodeResolver = (
S,
{ schemaType }
) => {
switch (schemaType) {
case `page`:
return S.document().views([
S.view.form(),
S.view
.component(Iframe)
.options({
url: (doc: SanityDocument) => {
const isHome = doc?.slug?.current === 'home'
const previewUrl = `${isHome ? process.env.SITE_URL : getPreviewUrl(doc)}?preview=${process.env.PREVIEW_TOKEN}`
return previewUrl
},
})
.title('Preview'),
])
case `post`:
return S.document().views([
S.view.form(),
S.view
.component(Iframe)
.options({
url: (doc: SanityDocument) => {
const previewUrl = `/blog/${doc?.slug?.current}?preview=${process.env.PREVIEW_TOKEN}`
return previewUrl
},
})
.title('Preview'),
])
default:
return S.document().views([S.view.form()])
}
}