Skip to content

Commit 509ef44

Browse files
committed
Type fixes
1 parent 40d9198 commit 509ef44

File tree

7 files changed

+31
-16
lines changed

7 files changed

+31
-16
lines changed

nextjs-app/app/[slug]/page.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Head from "next/head";
44
import PageBuilderPage from "@/app/components/PageBuilder";
55
import { sanityFetch } from "@/sanity/lib/live";
66
import { getPageQuery, pagesSlugs } from "@/sanity/lib/queries";
7-
import { Page as PageType } from "@/sanity.types";
7+
import { GetPageQueryResult } from "@/sanity.types";
88
import { PageOnboarding } from "@/app/components/Onboarding";
99

1010
type Props = {
@@ -77,7 +77,7 @@ export default async function Page(props: Props) {
7777
</div>
7878
</div>
7979
</div>
80-
<PageBuilderPage page={page as PageType} />
80+
<PageBuilderPage page={page as GetPageQueryResult} />
8181
</div>
8282
);
8383
}

nextjs-app/app/components/InfoSection.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function CTA({ block }: InfoProps) {
2222
{block.subheading}
2323
</span>
2424
)}
25-
<div className="prose prose-a:text-red-500 mt-4">
25+
<div className="mt-4">
2626
{block?.content?.length && (
2727
<PortableText
2828
className=""

nextjs-app/app/components/PageBuilder.tsx

+20-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { useOptimistic } from "next-sanity/hooks";
55
import Link from "next/link";
66

77
import BlockRenderer from "@/app/components/BlockRenderer";
8-
import { Page } from "@/sanity.types";
8+
import { GetPageQueryResult } from "@/sanity.types";
99
import { dataAttr } from "@/sanity/lib/utils";
1010
import { studioUrl } from "@/sanity/lib/api";
1111

1212
type PageBuilderPageProps = {
13-
page: Page;
13+
page: GetPageQueryResult;
1414
};
1515

1616
type PageBuilderSection = {
@@ -28,7 +28,13 @@ type PageData = {
2828
* The PageBuilder component is used to render the blocks from the `pageBuilder` field in the Page type in your Sanity Studio.
2929
*/
3030

31-
function renderSections(pageBuilderSections: PageBuilderSection[], page: Page) {
31+
function renderSections(
32+
pageBuilderSections: PageBuilderSection[],
33+
page: GetPageQueryResult
34+
) {
35+
if (!page) {
36+
return null;
37+
}
3238
return (
3339
<div
3440
data-sanity={dataAttr({
@@ -50,7 +56,10 @@ function renderSections(pageBuilderSections: PageBuilderSection[], page: Page) {
5056
);
5157
}
5258

53-
function renderEmptyState(page: Page) {
59+
function renderEmptyState(page: GetPageQueryResult) {
60+
if (!page) {
61+
return null;
62+
}
5463
return (
5564
<div className="container">
5665
<h1 className="text-4xl font-extrabold text-gray-900 tracking-tight sm:text-5xl">
@@ -77,12 +86,12 @@ export default function PageBuilder({ page }: PageBuilderPageProps) {
7786
const pageBuilderSections = useOptimistic<
7887
PageBuilderSection[] | undefined,
7988
SanityDocument<PageData>
80-
>(page?.pageBuilder, (currentSections, action) => {
89+
>(page?.pageBuilder || [], (currentSections, action) => {
8190
// The action contains updated document data from Sanity
8291
// when someone makes an edit in the Studio
8392

8493
// If the edit was to a different document, ignore it
85-
if (action.id !== page._id) {
94+
if (action.id !== page?._id) {
8695
return currentSections;
8796
}
8897

@@ -91,14 +100,18 @@ export default function PageBuilder({ page }: PageBuilderPageProps) {
91100
// Reconcile References. https://www.sanity.io/docs/enabling-drag-and-drop#ffe728eea8c1
92101
return action.document.pageBuilder.map(
93102
(section) =>
94-
currentSections?.find((s) => s._key === section?._key) || section,
103+
currentSections?.find((s) => s._key === section?._key) || section
95104
);
96105
}
97106

98107
// Otherwise keep the current sections
99108
return currentSections;
100109
});
101110

111+
if (!page) {
112+
return renderEmptyState(page);
113+
}
114+
102115
return pageBuilderSections && pageBuilderSections.length > 0
103116
? renderSections(pageBuilderSections, page)
104117
: renderEmptyState(page);

nextjs-app/app/components/PortableText.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ export default function CustomPortableText({
8686
};
8787

8888
return (
89-
<div className={["prose", className].filter(Boolean).join(" ")}>
89+
<div
90+
className={["prose prose-a:text-red-500", className]
91+
.filter(Boolean)
92+
.join(" ")}
93+
>
9094
<PortableText components={components} value={value} />
9195
</div>
9296
);

nextjs-app/app/posts/[slug]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function generateStaticParams() {
3535
*/
3636
export async function generateMetadata(
3737
props: Props,
38-
parent: ResolvingMetadata,
38+
parent: ResolvingMetadata
3939
): Promise<Metadata> {
4040
const params = await props.params;
4141
const { data: post } = await sanityFetch({

nextjs-app/sanity.types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ export type SettingsQueryResult = {
505505
};
506506
} | null;
507507
// Variable: getPageQuery
508-
// Query: *[_type == 'page' && slug.current == $slug][0]{ _id, _type, name, slug, heading, subheading, "pageBuilder": pageBuilder[]{ ..., _type == "callToAction" => { ..., link { ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } }, }, _type == "infoSection" => { ..., content[]{ ..., markDefs[]{ ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } } } }, }, }
508+
// Query: *[_type == 'page' && slug.current == $slug][0]{ _id, _type, name, slug, heading, subheading, "pageBuilder": pageBuilder[]{ ..., _type == "callToAction" => { link { ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } }, }, _type == "infoSection" => { content[]{ ..., markDefs[]{ ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } } } }, }, }
509509
export type GetPageQueryResult = {
510510
_id: string;
511511
_type: "page";
@@ -708,7 +708,7 @@ import "@sanity/client";
708708
declare module "@sanity/client" {
709709
interface SanityQueries {
710710
"*[_type == \"settings\"][0]": SettingsQueryResult;
711-
"\n *[_type == 'page' && slug.current == $slug][0]{\n _id,\n _type,\n name,\n slug,\n heading,\n subheading,\n \"pageBuilder\": pageBuilder[]{\n ...,\n _type == \"callToAction\" => {\n ...,\n \n link {\n ...,\n \n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n\n }\n,\n },\n _type == \"infoSection\" => {\n ...,\n content[]{\n ...,\n markDefs[]{\n ...,\n \n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n\n }\n }\n },\n },\n }\n": GetPageQueryResult;
711+
"\n *[_type == 'page' && slug.current == $slug][0]{\n _id,\n _type,\n name,\n slug,\n heading,\n subheading,\n \"pageBuilder\": pageBuilder[]{\n ...,\n _type == \"callToAction\" => {\n \n link {\n ...,\n \n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n\n }\n,\n },\n _type == \"infoSection\" => {\n content[]{\n ...,\n markDefs[]{\n ...,\n \n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n\n }\n }\n },\n },\n }\n": GetPageQueryResult;
712712
"\n *[_type == \"post\" && defined(slug.current)] | order(date desc, _updatedAt desc) {\n \n _id,\n \"status\": select(_originalId in path(\"drafts.**\") => \"draft\", \"published\"),\n \"title\": coalesce(title, \"Untitled\"),\n \"slug\": slug.current,\n excerpt,\n coverImage,\n \"date\": coalesce(date, _updatedAt),\n \"author\": author->{firstName, lastName, picture},\n\n }\n": AllPostsQueryResult;
713713
"\n *[_type == \"post\" && _id != $skip && defined(slug.current)] | order(date desc, _updatedAt desc) [0...$limit] {\n \n _id,\n \"status\": select(_originalId in path(\"drafts.**\") => \"draft\", \"published\"),\n \"title\": coalesce(title, \"Untitled\"),\n \"slug\": slug.current,\n excerpt,\n coverImage,\n \"date\": coalesce(date, _updatedAt),\n \"author\": author->{firstName, lastName, picture},\n\n }\n": MorePostsQueryResult;
714714
"\n *[_type == \"post\" && slug.current == $slug] [0] {\n content[]{\n ...,\n markDefs[]{\n ...,\n \n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n\n }\n },\n \n _id,\n \"status\": select(_originalId in path(\"drafts.**\") => \"draft\", \"published\"),\n \"title\": coalesce(title, \"Untitled\"),\n \"slug\": slug.current,\n excerpt,\n coverImage,\n \"date\": coalesce(date, _updatedAt),\n \"author\": author->{firstName, lastName, picture},\n\n }\n": PostQueryResult;

nextjs-app/sanity/lib/queries.ts

-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ export const getPageQuery = defineQuery(`
3838
"pageBuilder": pageBuilder[]{
3939
...,
4040
_type == "callToAction" => {
41-
...,
4241
${linkFields},
4342
},
4443
_type == "infoSection" => {
45-
...,
4644
content[]{
4745
...,
4846
markDefs[]{

0 commit comments

Comments
 (0)