Skip to content

Commit

Permalink
feat(redirect): slash prefix in slug input
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 3, 2024
1 parent 29027e1 commit 7adc9c0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
21 changes: 21 additions & 0 deletions studio/components/PrefixedSlugInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SlugInputProps } from "sanity";
import styles from "./prefixedSlugInput.module.css";
import { useTheme } from "@sanity/ui";

type PrefixedSlugInputProps = SlugInputProps & {
prefix: string;
};

const PrefixedSlugInput = ({ prefix, ...props }: PrefixedSlugInputProps) => {
const theme = useTheme();
const prefersDark = theme.sanity.v2?.color._dark ?? false;

return (
<div className={`${prefersDark ? `${styles.dark} ` : ""}${styles.wrapper}`}>
<span className={styles.prefixWrapper}>{prefix}</span>
{props.renderDefault(props)}
</div>
);
};

export default PrefixedSlugInput;
28 changes: 28 additions & 0 deletions studio/components/prefixedSlugInput.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.wrapper {
display: flex;
}

.wrapper input {
padding-left: 0.5rem;
border-left: none;
}

.prefixWrapper + * * {
border-top-left-radius: 0 !important;
border-bottom-left-radius: 0 !important;
}

.prefixWrapper {
background-color: rgb(246, 246, 248);
border-radius: 4px 0 0 4px;
padding: 0.375rem 0.5rem 0.375rem 0.5rem;
border: 1px solid rgb(225, 226, 230);
border-right: none;
}

[data-scheme="dark"] .prefixWrapper {
background-color: rgb(25, 26, 36);
border: 1px solid rgb(41, 44, 61);
border-right: none;
color: white;
}
12 changes: 8 additions & 4 deletions studio/schemas/documents/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import RedirectThumbnail from "../../components/RedirectThumbnail";
import { pageBuilderID } from "../builders/pageBuilder";
import { blogId } from "./blog";
import { compensationsId } from "./compensations";
import PrefixedSlugInput from "../../components/PrefixedSlugInput";

const slugValidator = (rule: SlugRule) =>
rule.required().custom((value: Slug | undefined) => {
if (!value || !value.current) return "Can't be blank";
if (!value.current.startsWith("/")) {
return "The path must start with a forward slash ('/')";
}
return true;
});

Expand All @@ -37,6 +35,9 @@ export const redirect = defineType({
description: "Which url should this redirect apply for",
type: "slug",
validation: slugValidator,
components: {
input: (props) => PrefixedSlugInput({ prefix: "/", ...props }),
},
}),
defineField({
name: "destination",
Expand Down Expand Up @@ -85,6 +86,9 @@ export const redirect = defineType({
return true;
},
},
components: {
input: (props) => PrefixedSlugInput({ prefix: "/", ...props }),
},
}),
],
}),
Expand Down Expand Up @@ -116,7 +120,7 @@ export const redirect = defineType({
destinationType === "slug" ? destinationSlug : destinationReferenceSlug;
const title =
source && destination
? `${source.current}${destination}`
? `/${source.current}/${destination}`
: undefined;
return {
title,
Expand Down

0 comments on commit 7adc9c0

Please sign in to comment.