mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 05:56:59 +00:00
149 lines
4.7 KiB
TypeScript
149 lines
4.7 KiB
TypeScript
import Kodamera from '@/lib/sanity/components/icons/kodamera'
|
||
import { schemaTypes } from '@/lib/sanity/schemas'
|
||
import { documentInternationalization } from '@sanity/document-internationalization'
|
||
import { visionTool } from '@sanity/vision'
|
||
import { defineConfig } from 'sanity'
|
||
import { Iframe, IframeOptions } from 'sanity-plugin-iframe-pane'
|
||
|
||
import { previewUrl } from 'sanity-plugin-iframe-pane/preview-url'
|
||
import { media } from 'sanity-plugin-media'
|
||
import { deskTool } from 'sanity/desk'
|
||
import { apiVersion, dataset, previewSecretId, projectId } from './lib/sanity/sanity.api'
|
||
import category from './lib/sanity/schemas/documents/category'
|
||
import page from './lib/sanity/schemas/documents/page'
|
||
import product from './lib/sanity/schemas/documents/product'
|
||
import home from './lib/sanity/schemas/singletons/home'
|
||
import search from './lib/sanity/schemas/singletons/search'
|
||
import { pageStructure, singletonPlugin } from './plugins/settings'
|
||
|
||
const devOnlyPlugins = [visionTool({ defaultApiVersion: apiVersion})]
|
||
|
||
export const PREVIEWABLE_DOCUMENT_TYPES = [
|
||
home.name,
|
||
page.name,
|
||
category.name,
|
||
product.name,
|
||
search.name
|
||
] satisfies string[]
|
||
|
||
// Used to generate URLs for drafts and live previews
|
||
export const PREVIEW_BASE_URL = '/api/preview'
|
||
|
||
// export const urlResolver = defineUrlResolver({
|
||
// base: PREVIEW_BASE_URL,
|
||
// })
|
||
|
||
// import {MissingSlug, type UrlResolver} from 'sanity-plugin-iframe-pane'
|
||
|
||
export const urlResolver = (document: any, urlSecret: any) => {
|
||
const url = new URL(PREVIEW_BASE_URL, location.origin)
|
||
url.searchParams.set('type', document._type)
|
||
const lang = document?.language
|
||
url.searchParams.set('locale', lang)
|
||
const slug = (document?.slug as any)?.current
|
||
if (slug) {
|
||
url.searchParams.set('slug', slug)
|
||
}
|
||
|
||
if (urlSecret) {
|
||
url.searchParams.set('secret', urlSecret)
|
||
}
|
||
return url.toString()
|
||
}
|
||
|
||
export const iframeOptions = {
|
||
url: urlResolver,
|
||
urlSecretId: previewSecretId,
|
||
} satisfies IframeOptions
|
||
|
||
// Define the actions that should be available for singleton documents
|
||
const singletonActions = new Set(["publish", "discardChanges", "restore"])
|
||
|
||
// Define the singleton document types
|
||
const singletonTypes = new Set(["settings", "home", "utilityMenu", "media.tag", ])
|
||
|
||
// console.log(process.env.SANITY_API_READ_TOKEN)
|
||
|
||
export default defineConfig({
|
||
name: 'default',
|
||
title: 'KM Storefront CMS',
|
||
projectId: projectId,
|
||
basePath: '/studio',
|
||
dataset: dataset,
|
||
plugins: [
|
||
// deskTool({structure}),
|
||
deskTool({
|
||
structure: pageStructure([home, search]),
|
||
// `defaultDocumentNode` is responsible for adding a “Preview” tab to the document pane
|
||
// You can add any React component to `S.view.component` and it will be rendered in the pane
|
||
// and have access to content in the form in real-time.
|
||
// It's part of the Studio's “Structure Builder API” and is documented here:
|
||
// https://www.sanity.io/docs/structure-builder-reference
|
||
defaultDocumentNode: (S, { schemaType }) => {
|
||
if ((PREVIEWABLE_DOCUMENT_TYPES as string[]).includes(schemaType)) {
|
||
return S.document().views([
|
||
// Default form view
|
||
S.view.form(),
|
||
// Preview
|
||
S.view.component(Iframe).options(iframeOptions).title('Preview'),
|
||
])
|
||
}
|
||
|
||
return null
|
||
},
|
||
}),
|
||
// Configures the global "new document" button, and document actions, to suit the Settings document singleton
|
||
singletonPlugin([home.name]),
|
||
|
||
// Add the "Open preview" action
|
||
previewUrl({
|
||
base: PREVIEW_BASE_URL,
|
||
urlSecretId: previewSecretId,
|
||
matchTypes: PREVIEWABLE_DOCUMENT_TYPES,
|
||
}),
|
||
media(),
|
||
// ...(isDev ? devOnlyPlugins : []),
|
||
documentInternationalization({
|
||
// Required, either:
|
||
// An array of supported languages
|
||
supportedLanguages: [
|
||
{id: 'sv', title: 'Swedish'},
|
||
{id: 'en', title: 'English'}
|
||
],
|
||
// Required
|
||
schemaTypes: [
|
||
'home',
|
||
'page',
|
||
'product',
|
||
'category',
|
||
'settings',
|
||
'blurb',
|
||
'section',
|
||
'usp',
|
||
'footerMenu',
|
||
'utilityMenu',
|
||
'search'
|
||
],
|
||
}),
|
||
],
|
||
schema: {
|
||
types: schemaTypes,
|
||
// Filter out singleton types from the global “New document” menu options
|
||
templates: (templates) =>
|
||
templates.filter(({ schemaType }) => !singletonTypes.has(schemaType)),
|
||
},
|
||
document: {
|
||
// For singleton types, filter out actions that are not explicitly included
|
||
// in the `singletonActions` list defined above
|
||
actions: (input, context) =>
|
||
singletonTypes.has(context.schemaType)
|
||
? input.filter(({ action }) => action && singletonActions.has(action))
|
||
: input,
|
||
},
|
||
studio: {
|
||
components: {
|
||
logo: Kodamera,
|
||
}
|
||
},
|
||
})
|