Skip to content

Commit

Permalink
feat(redirect): internal page as destination
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 3, 2024
1 parent e125198 commit 29027e1
Showing 1 changed file with 66 additions and 18 deletions.
84 changes: 66 additions & 18 deletions studio/schemas/documents/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { defineField, defineType, type Slug } from "sanity";
import { SlugRule } from "@sanity/types";
import RedirectThumbnail from "../../components/RedirectThumbnail";
import { pageBuilderID } from "../builders/pageBuilder";
import { blogId } from "./blog";
import { compensationsId } from "./compensations";

const slugValidator = (rule: SlugRule) =>
rule.required().custom((value: Slug | undefined) => {
Expand Down Expand Up @@ -30,45 +33,90 @@ export const redirect = defineType({
fields: [
defineField({
name: "source",
title: "Source",
description: "Which url should this redirect apply for",
type: "slug",
validation: slugValidator,
}),
defineField({
name: "destination",
description: "Where should the user be redirected?",
type: "slug",
validation: slugValidator,
options: {
isUnique: () => {
/*
does not need to be unique since multiple source paths
can point to the same destination path
*/
return true;
},
},
title: "Destination",
type: "object",
fields: [
defineField({
name: "type",
title: "Type",
description: "Choose between an internal page or a static slug",
type: "string",
initialValue: "reference",
options: {
layout: "radio",
list: [
{ value: "reference", title: "Internal Page" },
{ value: "slug", title: "Slug" },
],
},
}),
defineField({
name: "reference",
title: "Internal Page",
description: "Where should the user be redirected?",
type: "reference",
to: [
{ type: pageBuilderID },
{ type: blogId },
{ type: compensationsId },
],
hidden: ({ parent }) => parent?.type !== "reference",
}),
defineField({
name: "slug",
title: "Slug",
description: "Where should the user be redirected?",
type: "slug",
hidden: ({ parent }) => parent?.type !== "slug",
validation: slugValidator,
options: {
isUnique: () => {
/*
does not need to be unique since multiple source paths
can point to the same destination path
*/
return true;
},
},
}),
],
}),
defineField({
name: "permanent",
title: "Permanent",
description:
"Will this redirect exist throughout the foreseeable future?",
type: "boolean",
initialValue: false,
}),
],
initialValue: {
permanent: false,
},
preview: {
select: {
source: "source",
destination: "destination",
destinationType: "destination.type",
destinationSlug: "destination.slug.current",
destinationReferenceSlug: "destination.reference.slug.current",
permanent: "permanent",
},
prepare({ source, destination, permanent }) {
prepare({
source,
destinationType,
destinationSlug,
destinationReferenceSlug,
permanent,
}) {
const destination =
destinationType === "slug" ? destinationSlug : destinationReferenceSlug;
const title =
source && destination
? `${source.current}${destination.current}`
? `${source.current}${destination}`
: undefined;
return {
title,
Expand Down

0 comments on commit 29027e1

Please sign in to comment.