Skip to content

Commit 3c50856

Browse files
authored
fix: lower case slugs in teams (#11026)
* fix: lower case slugs in teams Signed-off-by: Udit Takkar <[email protected]> * fix: use slugify Signed-off-by: Udit Takkar <[email protected]> --------- Signed-off-by: Udit Takkar <[email protected]>
1 parent 3204d0a commit 3c50856

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
1313
import { md } from "@calcom/lib/markdownIt";
1414
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
1515
import objectKeys from "@calcom/lib/objectKeys";
16+
import slugify from "@calcom/lib/slugify";
1617
import turndown from "@calcom/lib/turndownService";
1718
import type { RouterOutputs } from "@calcom/trpc/react";
1819
import { trpc } from "@calcom/trpc/react";
@@ -229,7 +230,7 @@ const OtherTeamProfileView = () => {
229230
}
230231
onChange={(e) => {
231232
form.clearErrors("slug");
232-
form.setValue("slug", e?.target.value);
233+
form.setValue("slug", slugify(e?.target.value, true));
233234
}}
234235
/>
235236
</div>

packages/features/ee/teams/components/CreateANewTeamForm.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ export const CreateANewTeamForm = () => {
115115
? orgBranding.fullDomain.replace("https://", "").replace("http://", "") + "/"
116116
: `${extractDomainFromWebsiteUrl}/team/`
117117
}`}
118+
value={value}
118119
defaultValue={value}
119120
onChange={(e) => {
120-
newTeamFormMethods.setValue("slug", slugify(e?.target.value), {
121+
newTeamFormMethods.setValue("slug", slugify(e?.target.value, true), {
121122
shouldTouch: true,
122123
});
123124
newTeamFormMethods.clearErrors("slug");

packages/features/ee/teams/pages/team-profile-view.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useParamsWithFallback } from "@calcom/lib/hooks/useParamsWithFallback";
1616
import { md } from "@calcom/lib/markdownIt";
1717
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
1818
import objectKeys from "@calcom/lib/objectKeys";
19+
import slugify from "@calcom/lib/slugify";
1920
import turndown from "@calcom/lib/turndownService";
2021
import { MembershipRole } from "@calcom/prisma/enums";
2122
import { trpc } from "@calcom/trpc/react";
@@ -232,7 +233,7 @@ const ProfileView = () => {
232233
}
233234
onChange={(e) => {
234235
form.clearErrors("slug");
235-
form.setValue("slug", e?.target.value);
236+
form.setValue("slug", slugify(e?.target.value, true));
236237
}}
237238
/>
238239
</div>

packages/lib/slugify.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
export const slugify = (str: string) => {
2-
return str
1+
// forDisplayingInput is used to allow user to type "-" at the end and not replace with empty space.
2+
// For eg:- "test-slug" is the slug user wants to set but while typing "test-" would get replace to "test" becauser of replace(/-+$/, "")
3+
4+
export const slugify = (str: string, forDisplayingInput?: boolean) => {
5+
const s = str
36
.toLowerCase() // Convert to lowercase
47
.trim() // Remove whitespace from both sides
58
.normalize("NFD") // Normalize to decomposed form for handling accents
69
.replace(/\p{Diacritic}/gu, "") // Remove any diacritics (accents) from characters
710
.replace(/[^\p{L}\p{N}\p{Zs}\p{Emoji}]+/gu, "-") // Replace any non-alphanumeric characters (including Unicode) with a dash
811
.replace(/[\s_#]+/g, "-") // Replace whitespace, # and underscores with a single dash
9-
.replace(/^-+/, "") // Remove dashes from start
10-
.replace(/-+$/, ""); // Remove dashes from end
12+
.replace(/^-+/, ""); // Remove dashes from start
13+
14+
return forDisplayingInput ? s : s.replace(/-+$/, ""); // Remove dashes from end
1115
};
1216

1317
export default slugify;

packages/trpc/server/routers/viewer/teams/update.schema.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { z } from "zod";
22

3+
import slugify from "@calcom/lib/slugify";
4+
35
export const ZUpdateInputSchema = z.object({
46
id: z.number(),
57
bio: z.string().optional(),
68
name: z.string().optional(),
79
logo: z.string().optional(),
8-
slug: z.string().optional(),
10+
slug: z
11+
.string()
12+
.transform((val) => slugify(val.trim()))
13+
.optional(),
914
hideBranding: z.boolean().optional(),
1015
hideBookATeamMember: z.boolean().optional(),
1116
isPrivate: z.boolean().optional(),

0 commit comments

Comments
 (0)