|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { SanityDocument } from "next-sanity"; |
| 4 | +import { useOptimistic } from "next-sanity/hooks"; |
1 | 5 | import Link from "next/link";
|
2 | 6 |
|
3 | 7 | import BlockRenderer from "@/app/components/BlockRenderer";
|
4 | 8 | import { Page } from "@/sanity.types";
|
| 9 | +import { dataAttr } from "@/sanity/lib/utils"; |
5 | 10 | import { studioUrl } from "@/sanity/lib/api";
|
6 | 11 |
|
7 | 12 | type PageBuilderPageProps = {
|
8 | 13 | page: Page;
|
9 | 14 | };
|
10 | 15 |
|
| 16 | +type PageBuilderSection = { |
| 17 | + _key: string; |
| 18 | + _type: string; |
| 19 | +}; |
| 20 | + |
| 21 | +type PageData = { |
| 22 | + _id: string; |
| 23 | + _type: string; |
| 24 | + pageBuilder?: PageBuilderSection[]; |
| 25 | +}; |
| 26 | + |
11 | 27 | /**
|
12 | 28 | * The PageBuilder component is used to render the blocks from the `pageBuilder` field in the Page type in your Sanity Studio.
|
13 | 29 | */
|
14 |
| -export default function PageBuilder({ page }: PageBuilderPageProps) { |
15 |
| - if (page?.pageBuilder && page.pageBuilder.length > 0) { |
16 |
| - return ( |
17 |
| - <> |
18 |
| - {page.pageBuilder.map((block: any, index: number) => ( |
19 |
| - <BlockRenderer key={block._key} index={index} block={block} /> |
20 |
| - ))} |
21 |
| - </> |
22 |
| - ); |
23 |
| - } |
24 |
| - |
25 |
| - // If there are no blocks in the page builder. |
| 30 | + |
| 31 | +function renderSections(pageBuilderSections: PageBuilderSection[], page: Page) { |
| 32 | + return ( |
| 33 | + <div |
| 34 | + data-sanity={dataAttr({ |
| 35 | + id: page._id, |
| 36 | + type: page._type, |
| 37 | + path: `pageBuilder`, |
| 38 | + }).toString()} |
| 39 | + > |
| 40 | + {pageBuilderSections.map((block: any, index: number) => ( |
| 41 | + <BlockRenderer |
| 42 | + key={block._key} |
| 43 | + index={index} |
| 44 | + block={block} |
| 45 | + pageId={page._id} |
| 46 | + pageType={page._type} |
| 47 | + /> |
| 48 | + ))} |
| 49 | + </div> |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +function renderEmptyState(page: Page) { |
26 | 54 | return (
|
27 |
| - <> |
28 |
| - <div className="container"> |
29 |
| - <h1 className="text-4xl font-extrabold text-gray-900 tracking-tight sm:text-5xl"> |
30 |
| - This page has no content! |
31 |
| - </h1> |
32 |
| - <p className="mt-2 text-base text-gray-500"> |
33 |
| - Open the page in Sanity Studio to add content. |
34 |
| - </p> |
35 |
| - <div className="mt-10 flex"> |
36 |
| - <Link |
37 |
| - className="rounded-full flex gap-2 mr-6 items-center bg-black hover:bg-red-500 focus:bg-cyan-500 py-3 px-6 text-white transition-colors duration-200" |
38 |
| - href={`${studioUrl}/structure/intent/edit/template=page;type=page;path=pageBuilder;id=${page._id}`} |
39 |
| - target="_blank" |
40 |
| - rel="noopener noreferrer" |
41 |
| - > |
42 |
| - Add content to this page |
43 |
| - </Link> |
44 |
| - </div> |
| 55 | + <div className="container"> |
| 56 | + <h1 className="text-4xl font-extrabold text-gray-900 tracking-tight sm:text-5xl"> |
| 57 | + This page has no content! |
| 58 | + </h1> |
| 59 | + <p className="mt-2 text-base text-gray-500"> |
| 60 | + Open the page in Sanity Studio to add content. |
| 61 | + </p> |
| 62 | + <div className="mt-10 flex"> |
| 63 | + <Link |
| 64 | + className="rounded-full flex gap-2 mr-6 items-center bg-black hover:bg-red-500 focus:bg-cyan-500 py-3 px-6 text-white transition-colors duration-200" |
| 65 | + href={`${studioUrl}/structure/intent/edit/template=page;type=page;path=pageBuilder;id=${page._id}`} |
| 66 | + target="_blank" |
| 67 | + rel="noopener noreferrer" |
| 68 | + > |
| 69 | + Add content to this page |
| 70 | + </Link> |
45 | 71 | </div>
|
46 |
| - </> |
| 72 | + </div> |
47 | 73 | );
|
48 | 74 | }
|
| 75 | + |
| 76 | +export default function PageBuilder({ page }: PageBuilderPageProps) { |
| 77 | + const pageBuilderSections = useOptimistic< |
| 78 | + PageBuilderSection[] | undefined, |
| 79 | + SanityDocument<PageData> |
| 80 | + >(page?.pageBuilder, (currentSections, action) => { |
| 81 | + // The action contains updated document data from Sanity |
| 82 | + // when someone makes an edit in the Studio |
| 83 | + |
| 84 | + // If the edit was to a different document, ignore it |
| 85 | + if (action.id !== page._id) { |
| 86 | + return currentSections; |
| 87 | + } |
| 88 | + |
| 89 | + // If there are sections in the updated document, use them |
| 90 | + if (action.document.pageBuilder) { |
| 91 | + // Reconcile References. https://www.sanity.io/docs/enabling-drag-and-drop#ffe728eea8c1 |
| 92 | + return action.document.pageBuilder.map( |
| 93 | + (section) => |
| 94 | + currentSections?.find((s) => s._key === section?._key) || section |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + // Otherwise keep the current sections |
| 99 | + return currentSections; |
| 100 | + }); |
| 101 | + |
| 102 | + return pageBuilderSections && pageBuilderSections.length > 0 |
| 103 | + ? renderSections(pageBuilderSections, page) |
| 104 | + : renderEmptyState(page); |
| 105 | +} |
0 commit comments