Files
Cody OlsenandGitHub f1d72998e8 Update Sanity example for App Router (#63187)
- Updates deps with minor bugfixes.
- Fixes typos and removes debug console logs.
- Less aggressive validation rules for alt text
2024-03-12 16:50:04 -07:00

95 lines
2.6 KiB
TypeScript

import { map, Observable } from "rxjs";
import type {
DocumentLocation,
DocumentLocationResolver,
DocumentLocationsState,
} from "sanity/presentation";
import { resolveHref } from "@/sanity/lib/utils";
const homeLocation = {
title: "Home",
href: "/",
} satisfies DocumentLocation;
export const locate: DocumentLocationResolver = (params, context) => {
if (params.type === "settings") {
const doc$ = context.documentStore.listenQuery(
`*[_type == "post" && defined(slug.current)]{title,slug}`,
{},
{ perspective: "previewDrafts" },
) as Observable<
| {
slug: { current: string };
title: string | null;
}[]
| null
>;
return doc$.pipe(
map((docs) => {
return {
message: "This document is used on all pages",
tone: "caution",
locations: docs?.length
? [
homeLocation,
...docs
.map((doc) => ({
title: doc?.title || "Untitled",
href: resolveHref("post", doc?.slug?.current)!,
}))
.filter((doc) => doc.href !== undefined),
]
: [],
} satisfies DocumentLocationsState;
}),
);
}
if (params.type === "post" || params.type === "author") {
const doc$ = context.documentStore.listenQuery(
`*[defined(slug.current) && _id==$id || references($id)]{_type,slug,title}`,
params,
{ perspective: "previewDrafts" },
) as Observable<
| {
_type: string;
slug: { current: string };
title?: string | null;
}[]
| null
>;
return doc$.pipe(
map((docs) => {
switch (params.type) {
case "author":
case "post":
return {
locations: docs?.length
? [
homeLocation,
...docs
.map((doc) => {
const href = resolveHref(doc._type, doc?.slug?.current);
return {
title: doc?.title || "Untitled",
href: href!,
};
})
.filter((doc) => doc.href !== undefined),
]
: [],
} satisfies DocumentLocationsState;
default:
return {
message: "Unable to map document type to locations",
tone: "critical",
} satisfies DocumentLocationsState;
}
}),
);
}
return null;
};