Skip to content

Commit

Permalink
update structure schema and add createCollection helper with Preview …
Browse files Browse the repository at this point in the history
…component
  • Loading branch information
milewskibogumil committed Sep 16, 2024
1 parent a9f922c commit 6867fa5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
35 changes: 0 additions & 35 deletions apps/sanity/schema/ui/cta.ts

This file was deleted.

29 changes: 29 additions & 0 deletions apps/sanity/structure/create-collection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { StructureBuilder } from "sanity/structure";
import { schemaTypes } from "./schema-types";
import { TYPES_TO_EXCLUDE_PREVIEWS } from ".";
import { Preview } from "./preview";

export const createCollection = (S: StructureBuilder, name: string) => {
const { title, icon } = schemaTypes.find(item => item.name === name) as { title: string, icon: React.ReactNode };
return S.listItem()
.title(title)
.icon(icon)
.child(
S.documentTypeList(name)
.title(title)
.child(documentId =>
S.document()
.documentId(documentId)
.schemaType(name)
.views([
S.view.form().title('Editor').icon(() => '🖋️'),
...(!TYPES_TO_EXCLUDE_PREVIEWS.includes(name) ? [
S.view
.component(Preview)
.title('Preview')
.icon(() => '👀')
] : []),
])
)
);
};
17 changes: 2 additions & 15 deletions apps/sanity/structure/create-singleton.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import type { StructureBuilder } from 'sanity/structure'
import { Iframe, type IframeProps } from 'sanity-plugin-iframe-pane'
import { schemaTypes } from "./schema-types";
import { PREVIEW_DEPLOYMENT_DOMAIN } from '../constants';

const Preview = ({ document }: { document: IframeProps['document'] }) => {
const slug = (document.displayed.slug as { current?: string })?.current;
if (!slug) return <div style={{ padding: '1rem' }}>🛑 Preview not available: The slug is missing</div>;
return <Iframe
document={document}
options={{
url: `${PREVIEW_DEPLOYMENT_DOMAIN}${slug}`,
reload: { button: true }
}} />
}

const TYPES_TO_EXCLUDE_PREVIEWS = ['global', 'redirects'];
import { TYPES_TO_EXCLUDE_PREVIEWS } from '.';
import { Preview } from './preview';

export const createSingleton = (S: StructureBuilder, name: string) => {
const { title, icon } = schemaTypes.find(item => item.name === name) as { title: string, icon: React.ReactNode };
Expand Down
7 changes: 5 additions & 2 deletions apps/sanity/structure/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { StructureResolver } from 'sanity/structure'
import { createSingleton } from './create-singleton';
import { createSingleton } from './create-singleton'
import { createCollection } from './create-collection';

export const TYPES_TO_EXCLUDE_PREVIEWS = ['global', 'redirects', 'Faq_Collection'];

export const structure: StructureResolver = (S) =>
S.list()
Expand All @@ -11,5 +14,5 @@ export const structure: StructureResolver = (S) =>
S.divider(),
createSingleton(S, "Index_Page"),
S.divider(),
S.documentTypeListItem("Faq_Collection"),
createCollection(S, "Faq_Collection"),
])
13 changes: 13 additions & 0 deletions apps/sanity/structure/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Iframe, type IframeProps } from "sanity-plugin-iframe-pane";
import { PREVIEW_DEPLOYMENT_DOMAIN } from "../constants";

export const Preview = ({ document }: { document: IframeProps['document'] }) => {
const slug = (document.displayed.slug as { current?: string })?.current;
if (!slug) return <div style={{ padding: '1rem' }}>🛑 Preview not available: The slug is missing</div>;
return <Iframe
document={document}
options={{
url: `${PREVIEW_DEPLOYMENT_DOMAIN}${slug}`,
reload: { button: true }
}} />
}

0 comments on commit 6867fa5

Please sign in to comment.