Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add group modal

Kiran K9b7537867b29
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-sheet.tsx
index 8f14998002..f0381fdd5d 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-sheet.tsx
@@ -1,14 +1,33 @@
import { useApiMutation } from "@/lib/swr/use-api-mutation";
+import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { createGroupSchema } from "@/lib/zod/schemas/groups";
import { X } from "@/ui/shared/icons";
import { Button, Sheet } from "@dub/ui";
import { cn } from "@dub/utils";
+import slugify from "@sindresorhus/slugify";
import { Dispatch, SetStateAction, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
+const generateRandomColor = () => {
+ const colors = [
+ "#3B82F6",
+ "#EF4444",
+ "#10B981",
+ "#F59E0B",
+ "#8B5CF6",
+ "#EC4899",
+ "#06B6D4",
+ "#84CC16",
+ "#F97316",
+ "#6366F1",
+ ];
+
+ return colors[Math.floor(Math.random() * colors.length)];
+};
+
interface CreateGroupSheetProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
}
@@ -16,28 +35,30 @@ interface CreateGroupSheetProps {
type FormData = z.input<typeof createGroupSchema>;
function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {
+ const { program } = useProgram();
const { id: workspaceId } = useWorkspace();
- const { makeRequest, isSubmitting } = useApiMutation();
+ const { makeRequest: createGroup, isSubmitting } = useApiMutation();
const {
register,
handleSubmit,
+ setValue,
watch,
formState: { errors },
- } = useForm<FormData>();
+ } = useForm<FormData>({
+ defaultValues: {
+ color: generateRandomColor(),
+ },
+ });
const onSubmit = async (data: FormData) => {
if (!workspaceId) {
return;
}
- await makeRequest("/api/groups", {
+ await createGroup("/api/groups", {
method: "POST",
- body: {
- ...data,
- slug: data.name.toLowerCase(),
- color: "#000000",
- },
+ body: data,
onSuccess: () => {
toast.success("Group created successfully!");
setIsOpen(false);
@@ -72,24 +93,86 @@ function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {
Name
</label>
<div className="mt-2">
- <input
- type="text"
- id="name"
- className={cn(
- "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.name &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- )}
- {...register("name", {
- required: "Nme is required",
- })}
- placeholder="Group name"
- />
- {errors.name && (
- <p className="mt-1 text-sm text-red-600">
- {errors.name.message}
- </p>
- )}
+ <div className="relative">
+ <input
+ type="text"
+ id="name"
+ autoFocus
+ className={cn(
+ "block w-full rounded-md border-neutral-300 px-3 py-2 pr-12 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.name &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
+ {...register("name", {
+ required: true,
+ })}
+ onChange={(e) => {
+ const name = e.target.value;
+ setValue("name", name);
+ setValue("slug", slugify(name));
+ }}
+ placeholder="Group name"
+ />
+ <div className="absolute inset-y-0 right-0 flex items-center pr-2.5">
+ <div className="relative">
+ <div
+ className="h-6 w-6 cursor-pointer rounded-full border-none"
+ style={{
+ backgroundColor: watch("color") || "",
+ }}
+ onClick={() =>
+ document.getElementById("color-picker")?.click()
+ }
+ />
+ <input
+ id="color-picker"
+ type="color"
+ {...register("color", {
+ required: true,
+ })}
+ className="absolute inset-0 cursor-pointer opacity-0"
+ title="Choose group color"
+ />
+ </div>
+ </div>
+ </div>
+
+ <p className="mt-2 text-xs text-neutral-500">
+ For internal use only, never visible to partners
+ </p>
+ </div>
+ </div>
+
+ <div>
+ <label
+ htmlFor="slug"
+ className="text-sm font-medium text-neutral-800"
+ >
+ Landing page slug
+ </label>
+ <div className="mt-2">
+ <div className="flex">
+ <div className="flex items-center rounded-l-md border-y border-l border-neutral-300 px-3 py-2 text-sm text-neutral-800">
+ partners.dub.co/{program?.slug}
+ </div>
+ <input
+ type="text"
+ id="slug"
+ className={cn(
+ "block w-full rounded-r-md border border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.slug &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
+ {...register("slug", {
+ required: true,
+ })}
+ placeholder="group-name"
+ />
+ </div>
+
+ <p className="mt-2 text-xs text-neutral-500">
+ For both internal and external use
+ </p>
</div>
</div>
</div>
diff --git a/apps/web/lib/swr/use-api-mutation.ts b/apps/web/lib/swr/use-api-mutation.ts
index 72bd62314b..1ccef4f5af 100644
--- a/apps/web/lib/swr/use-api-mutation.ts
+++ b/apps/web/lib/swr/use-api-mutation.ts
@@ -107,7 +107,7 @@ export function useApiMutation<
debug("Request finished");
}
},
- [],
+ [workspaceId],
);
return {
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index 0765861af4..684b37ebd8 100644
--- a/apps/web/lib/zod/schemas/groups.ts
+++ b/apps/web/lib/zod/schemas/groups.ts
@@ -1,3 +1,5 @@
+import { validSlugRegex } from "@dub/utils";
+import slugify from "@sindresorhus/slugify";
import { z } from "zod";
import { DiscountSchema } from "./discount";
import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
@@ -34,16 +36,33 @@ export const GroupSchemaExtended = GroupSchema.extend({
});
export const createGroupSchema = z.object({
- name: z.string().trim().min(1).max(190),
+ name: z
+ .string()
+ .trim()
+ .min(1, "Name is required")
+ .max(190, "Name is too long. Max 190 characters"),
slug: z
.string()
.trim()
.min(1)
.max(100)
- .refine((val) => !/^grp_/i.test(val.trim()), {
- message: "Slug cannot start with 'grp_'.",
+ .refine(
+ (val) => {
+ const trimmed = val.trim();
+ return validSlugRegex.test(trimmed) && !/^grp_/i.test(trimmed);
+ },
+ {
+ message: "Invalid slug format.",
+ },
+ )
+ .transform((val) => slugify(val)),
+ color: z
+ .string()
+ .trim()
+ .transform((val) => val.toLowerCase())
+ .refine((val) => /^#[0-9a-f]{6}$/i.test(val), {
+ message: "Color must be a valid 6-digit hex code (e.g. #ef4444).",
}),
- color: z.string().trim().nullable(),
});
export const updateGroupSchema = createGroupSchema.partial();