Skip to content

Commit

Permalink
feat(redirect): remove slug redirect type
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 10, 2024
1 parent ac7b932 commit 971cdc7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
1 change: 0 additions & 1 deletion studio/lib/queries/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { groq } from "next-sanity";
export const REDIRECT_BY_SOURCE_SLUG_QUERY = groq`
*[_type == "redirect" && source.current == $slug][0]{
"destination": select(
destination.type == "slug" => destination.slug.current,
destination.type == "reference" => destination.reference->slug.current,
destination.type == "external" => destination.external
)
Expand Down
47 changes: 12 additions & 35 deletions studio/schemas/documents/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ const redirect = defineType({
list: [
{ value: "reference", title: "Internal Page" },
{ value: "external", title: "External URL" },
{ value: "slug", title: "Slug" },
],
},
}),
Expand Down Expand Up @@ -92,66 +91,44 @@ const redirect = defineType({
requiredIfDestinationType("external", document, value),
),
}),
defineField({
name: "slug",
title: "Slug",
description: "Where should the user be redirected?",
type: "slug",
hidden: ({ parent }) => parent?.type !== "slug",
validation: (rule) =>
rule.custom((value, { document }) =>
requiredIfDestinationType("slug", document, value),
),
options: {
isUnique: () => {
/*
does not need to be unique since multiple source paths
can point to the same destination path
*/
return true;
},
},
components: {
input: (props) => PrefixedSlugInput({ prefix: "/", ...props }),
},
}),
],
}),
],
preview: {
select: {
sourceSlug: "source.current",
destinationType: "destination.type",
destinationSlug: "destination.slug.current",
destinationReferenceSlug: "destination.reference.slug.current",
destinationExternalURL: "destination.external",
},
prepare({
sourceSlug,
destinationType,
destinationSlug,
destinationReferenceSlug,
destinationExternalURL,
}: {
sourceSlug: string;
destinationType: string;
destinationSlug: string;
destinationReferenceSlug: string;
destinationExternalURL: string;
}) {
if (
typeof sourceSlug !== "string" ||
typeof destinationType !== "string" ||
typeof destinationReferenceSlug !== "string" ||
typeof destinationExternalURL !== "string"
) {
return {};
}
const destination = {
slug: `/${destinationSlug}`,
reference: `/${destinationReferenceSlug}`,
external: destinationExternalURL,
}[destinationType];
console.log(destination);
const title =
sourceSlug && destination
? `/${sourceSlug}${destination}`
: undefined;
return {
title,
subtitle: "type: " + destinationType,
subtitle: {
reference: "Internal",
external: "External",
}[destinationType],
};
},
},
Expand Down

0 comments on commit 971cdc7

Please sign in to comment.