The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Create group modal
Tim Wilsondaf283240d17
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-button.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-button.tsxindex 17a8089a5e..6ac4bdca48 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-button.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-button.tsx@@ -1,19 +1,17 @@"use client";import { Button, useKeyboardShortcut } from "@dub/ui";-import { useCreateGroupSheet } from "./create-group-sheet";+import { useCreateGroupModal } from "./create-group-modal";export function CreateGroupButton() {- const { createGroupSheet, setIsOpen: setShowCreateGroupSheet } =- useCreateGroupSheet({- nested: false,- });+ const { createGroupModal, setIsOpen: setShowCreateGroupSheet } =+ useCreateGroupModal({});useKeyboardShortcut("c", () => setShowCreateGroupSheet(true));return (<>- {createGroupSheet}+ {createGroupModal}<Buttontype="button"onClick={() => setShowCreateGroupSheet(true)}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-modal.tsxsimilarity index 69%rename from apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-sheet.tsxrename to apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/groups/create-group-modal.tsxindex e99887663f..9364c45d46 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-modal.tsx@@ -2,39 +2,23 @@ import { mutatePrefix } from "@/lib/swr/mutate";import { useApiMutation } from "@/lib/swr/use-api-mutation";import useProgram from "@/lib/swr/use-program";import { createGroupSchema } from "@/lib/zod/schemas/groups";-import { X } from "@/ui/shared/icons";-import { Button, Sheet } from "@dub/ui";+import { RESOURCE_COLORS } from "@/ui/colors";+import { GroupColorPicker } from "@/ui/partners/groups/group-color-picker";+import { Button, Modal } 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 { Controller, 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 {+interface CreateGroupModalProps {setIsOpen: Dispatch<SetStateAction<boolean>>;}type FormData = z.input<typeof createGroupSchema>;-function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {+function CreateGroupModalContent({ setIsOpen }: CreateGroupModalProps) {const { program } = useProgram();const { makeRequest: createGroup, isSubmitting } = useApiMutation();@@ -42,11 +26,12 @@ function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {register,handleSubmit,setValue,- watch,+ control,formState: { errors },} = useForm<FormData>({defaultValues: {- color: generateRandomColor(),+ color:+ RESOURCE_COLORS[Math.floor(Math.random() * RESOURCE_COLORS.length)],},});@@ -66,16 +51,7 @@ function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {<form onSubmit={handleSubmit(onSubmit)} className="flex h-full flex-col"><div className="sticky top-0 z-10 border-b border-neutral-200 bg-white"><div className="flex h-16 items-center justify-between px-6 py-4">- <Sheet.Title className="text-lg font-semibold">- Create group- </Sheet.Title>- <Sheet.Close asChild>- <Button- variant="outline"- icon={<X className="size-5" />}- className="h-auto w-fit p-1"- />- </Sheet.Close>+ <h2 className="text-lg font-semibold">Create group</h2></div></div>@@ -88,7 +64,7 @@ function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {>Name</label>- <div className="mt-2">+ <div className="mt-1.5"><div className="relative"><inputtype="text"@@ -110,26 +86,16 @@ function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {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>+ <Controller+ control={control}+ name="color"+ render={({ field }) => (+ <GroupColorPicker+ color={field.value}+ onChange={field.onChange}+ />+ )}+ /></div></div>@@ -146,7 +112,7 @@ function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {>Landing page slug</label>- <div className="mt-2">+ <div className="mt-1.5"><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}@@ -198,29 +164,27 @@ function CreateGroupSheetContent({ setIsOpen }: CreateGroupSheetProps) {);}-export function CreateGroupSheet({+export function CreateGroupModal({isOpen,- nested,- ...rest-}: CreateGroupSheetProps & {+ setIsOpen,+}: CreateGroupModalProps & {isOpen: boolean;- nested?: boolean;}) {return (- <Sheet open={isOpen} onOpenChange={rest.setIsOpen} nested={nested}>- <CreateGroupSheetContent {...rest} />- </Sheet>+ <Modal showModal={isOpen} setShowModal={setIsOpen}>+ <CreateGroupModalContent setIsOpen={setIsOpen} />+ </Modal>);}-export function useCreateGroupSheet(- props: { nested?: boolean } & Omit<CreateGroupSheetProps, "setIsOpen">,+export function useCreateGroupModal(+ props: Omit<CreateGroupModalProps, "setIsOpen">,) {const [isOpen, setIsOpen] = useState(false);return {- createGroupSheet: (- <CreateGroupSheet setIsOpen={setIsOpen} isOpen={isOpen} {...props} />+ createGroupModal: (+ <CreateGroupModal setIsOpen={setIsOpen} isOpen={isOpen} {...props} />),setIsOpen,};diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.tsindex 24da1fd262..8238c8ef29 100644--- a/apps/web/lib/zod/schemas/groups.ts+++ b/apps/web/lib/zod/schemas/groups.ts@@ -57,7 +57,7 @@ export const createGroupSchema = z.object({},).transform((val) => slugify(val)),- color: z.enum(RESOURCE_COLORS),+ color: z.enum(RESOURCE_COLORS).nullable(),});export const updateGroupSchema = createGroupSchema.partial();diff --git a/apps/web/ui/partners/groups/group-color-picker.tsx b/apps/web/ui/partners/groups/group-color-picker.tsxnew file mode 100644index 0000000000..292a77054f--- /dev/null+++ b/apps/web/ui/partners/groups/group-color-picker.tsx@@ -0,0 +1,111 @@+"use client";++import {+ getResourceColorData,+ RAINBOW_CONIC_GRADIENT,+ RESOURCE_COLORS_DATA,+} from "@/ui/colors";+import { Popover, Tooltip } from "@dub/ui";+import { capitalize, cn } from "@dub/utils";+import { useState } from "react";++export function GroupColorPicker({+ color,+ onChange,+ id,+}: {+ color: string | null;+ onChange: (color: string | null) => void;+ id?: string;+}) {+ const [isOpen, setIsOpen] = useState(false);++ const onSelect = (color: string | null) => {+ onChange(color);+ setIsOpen(false);+ };++ const colorClassName = color+ ? getResourceColorData(color)?.groupVariants+ : undefined;++ return (+ <Popover+ openPopover={isOpen}+ setOpenPopover={setIsOpen}+ content={+ <div className="flex items-center gap-2 p-2">+ <div className="sr-only" tabIndex={0}>+ Select a color+ </div>+ <Swatch+ name="Rainbow"+ colorClassName={null}+ onSelect={() => onSelect(null)}+ />+ {RESOURCE_COLORS_DATA.map(({ color, groupVariants }) => (+ <Swatch+ key={color}+ colorClassName={groupVariants}+ name={capitalize(color)!}+ onSelect={() => onSelect(color)}+ />+ ))}+ </div>+ }+ side="bottom"+ align="end"+ >+ <button+ id={id}+ type="button"+ className={cn(+ "relative size-5 overflow-hidden rounded-full outline-none ring-black/10 transition-all duration-75",+ "hover:ring focus:ring data-[state=open]:ring data-[state=open]:ring-black/20",+ "focus-visible:ring-1 focus-visible:ring-black/40 focus-visible:ring-offset-2",+ colorClassName,+ )}+ >+ {!colorClassName && <Rainbow />}+ </button>+ </Popover>+ );+}++function Swatch({+ colorClassName,+ name,+ onSelect,+}: {+ colorClassName: string | null;+ name: string;+ onSelect: () => void;+}) {+ return (+ <Tooltip content={name} delayDuration={1000} disableHoverableContent>+ <div className="w-fit rounded-full">+ <button+ type="button"+ onClick={onSelect}+ className={cn(+ "relative block size-5 overflow-hidden rounded-full ring-transparent ring-offset-2 transition-all duration-75",+ "hover:ring-1 hover:ring-[var(--ring-color)]",+ "outline-none focus-visible:ring-1 focus-visible:ring-[var(--ring-color)]",+ colorClassName,+ )}+ >+ {!colorClassName && <Rainbow />}+ </button>+ </div>+ </Tooltip>+ );+}++const Rainbow = () => (+ <div+ className="absolute -inset-[50%] rounded-full blur-[2px]"+ style={{+ backgroundImage: RAINBOW_CONIC_GRADIENT,+ }}+ />+);