From 29027e1f34836f16323538d8845d02ce78529396 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Tue, 3 Sep 2024 08:45:44 +0200 Subject: [PATCH] feat(redirect): internal page as destination --- studio/schemas/documents/redirect.ts | 84 ++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/studio/schemas/documents/redirect.ts b/studio/schemas/documents/redirect.ts index 6b21f28b2..6d873b568 100644 --- a/studio/schemas/documents/redirect.ts +++ b/studio/schemas/documents/redirect.ts @@ -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) => { @@ -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,