The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add color management
Tim Wilson0bf33fd6bb10
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/add-color-modal.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/add-color-modal.tsxnew file mode 100644index 0000000000..d6f5d8b46c--- /dev/null+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/add-color-modal.tsx@@ -0,0 +1,223 @@+"use client";++import { addProgramResourceAction } from "@/lib/actions/partners/program-resources/add-program-resource";+import useProgramResources from "@/lib/swr/use-program-resources";+import useWorkspace from "@/lib/swr/use-workspace";+import { Button, Modal } from "@dub/ui";+import { cn } from "@dub/utils";+import { useAction } from "next-safe-action/hooks";+import { useParams } from "next/navigation";+import {+ Dispatch,+ SetStateAction,+ useCallback,+ useEffect,+ useMemo,+ useState,+} from "react";+import { HexColorPicker } from "react-colorful";+import { useForm } from "react-hook-form";+import { toast } from "sonner";+import { z } from "zod";++type AddColorModalProps = {+ showAddColorModal: boolean;+ setShowAddColorModal: Dispatch<SetStateAction<boolean>>;+};++const colorFormSchema = z.object({+ name: z.string().min(1, "Name is required"),+ color: z.string().min(1, "Color is required"),+});++type ColorFormData = z.infer<typeof colorFormSchema>;++function AddColorModal(props: AddColorModalProps) {+ return (+ <Modal+ showModal={props.showAddColorModal}+ setShowModal={props.setShowAddColorModal}+ >+ <AddColorModalInner {...props} />+ </Modal>+ );+}++const DEFAULT_COLORS = ["#dc2626", "#84cc16", "#14b8a6", "#0ea5e9", "#d946ef"];++function AddColorModalInner({ setShowAddColorModal }: AddColorModalProps) {+ const { programId } = useParams();+ const { id: workspaceId } = useWorkspace();+ const { mutate } = useProgramResources();+ const [hexInputValue, setHexInputValue] = useState("#000000");++ const {+ register,+ handleSubmit,+ setValue,+ watch,+ setError,+ formState: { errors, isSubmitting, isSubmitSuccessful },+ } = useForm<ColorFormData>({+ defaultValues: {+ name: "",+ color: DEFAULT_COLORS[Math.floor(Math.random() * DEFAULT_COLORS.length)],+ },+ });++ const selectedColor = watch("color");++ // Keep hex input in sync with form value+ useEffect(() => setHexInputValue(selectedColor), [selectedColor]);++ const handleHexInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {+ const value = e.target.value;+ setHexInputValue(value);++ // Only update form value if it's a valid hex color+ if (/^#?[0-9A-F]{6}$/i.test(value)) {+ setValue("color", value.startsWith("#") ? value : `#${value}`);+ }+ };++ const { executeAsync } = useAction(addProgramResourceAction, {+ onSuccess: () => {+ mutate();+ setShowAddColorModal(false);+ toast.success("Color added successfully!");+ },+ onError({ error }) {+ if (error.serverError) {+ setError("root.serverError", {+ message: error.serverError,+ });+ toast.error(error.serverError);+ } else {+ toast.error("Failed to add color");+ }+ },+ });++ return (+ <>+ <div className="space-y-2 border-b border-neutral-200 p-4 sm:p-6">+ <h3 className="text-lg font-medium leading-none">Add color</h3>+ </div>++ <form+ onSubmit={handleSubmit(async (data: ColorFormData) => {+ await executeAsync({+ workspaceId: workspaceId!,+ programId: programId as string,+ name: data.name,+ resourceType: "color",+ color: data.color,+ });+ })}+ >+ <div className="bg-neutral-50 p-4 sm:p-6">+ <div className="space-y-4">+ <div>+ <span className="mb-1 block text-sm font-medium text-neutral-700">+ Color+ </span>+ <div className="flex justify-center [&_.react-colorful]:h-[180px] [&_.react-colorful]:w-full">+ <HexColorPicker+ color={selectedColor}+ onChange={(color) =>+ setValue("color", color, { shouldDirty: true })+ }+ />+ </div>+ </div>++ <label className="block">+ <span className="mb-1 block text-sm font-medium text-neutral-700">+ Hex+ </span>+ <input+ type="text"+ value={hexInputValue}+ onChange={handleHexInputChange}+ className={cn(+ "block w-full rounded-md border-neutral-300 shadow-sm focus:border-neutral-500 focus:ring-neutral-500 sm:text-sm",+ !/^#[0-9A-F]{6}$/i.test(hexInputValue) &&+ "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500",+ )}+ placeholder="#000000"+ />+ <input+ type="hidden"+ {...register("color", { required: "Please select a color" })}+ />+ {errors.color && (+ <p className="mt-1 text-xs text-red-600">+ {errors.color.message}+ </p>+ )}+ </label>++ <label className="block">+ <span className="mb-1 block text-sm font-medium text-neutral-700">+ Color name+ </span>+ <input+ id="name"+ type="text"+ className={cn(+ "block w-full rounded-md border-neutral-300 shadow-sm focus:border-neutral-500 focus:ring-neutral-500 sm:text-sm",+ errors.name &&+ "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500",+ )}+ {...register("name", { required: "Color name is required" })}+ />+ {errors.name && (+ <p className="mt-1 text-xs text-red-600">+ {errors.name.message}+ </p>+ )}+ </label>+ </div>+ </div>++ <div className="flex items-center justify-end gap-2 border-t border-neutral-200 bg-neutral-50 px-4 py-5 sm:px-6">+ <Button+ onClick={() => setShowAddColorModal(false)}+ variant="secondary"+ text="Cancel"+ className="h-8 w-fit px-3"+ type="button"+ />+ <Button+ type="submit"+ autoFocus+ loading={isSubmitting || isSubmitSuccessful}+ text="Add Color"+ className="h-8 w-fit px-3"+ />+ </div>+ </form>+ </>+ );+}++export function useAddColorModal() {+ const [showAddColorModal, setShowAddColorModal] = useState(false);++ const AddColorModalCallback = useCallback(() => {+ return (+ <AddColorModal+ showAddColorModal={showAddColorModal}+ setShowAddColorModal={setShowAddColorModal}+ />+ );+ }, [showAddColorModal, setShowAddColorModal]);++ return useMemo(+ () => ({+ setShowAddColorModal,+ AddColorModal: AddColorModalCallback,+ }),+ [setShowAddColorModal, AddColorModalCallback],+ );+}diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsxindex 81242f810a..09d3356d5d 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/resources/page-client.tsx@@ -17,6 +17,7 @@ import { useAction } from "next-safe-action/hooks";import { useParams } from "next/navigation";import { PropsWithChildren, ReactNode, useState } from "react";import { toast } from "sonner";+import { useAddColorModal } from "./add-color-modal";import { useAddLogoModal } from "./add-logo-modal";export function ProgramResourcesPageClient() {@@ -25,6 +26,7 @@ export function ProgramResourcesPageClient() {const { resources, mutate } = useProgramResources();const { setShowAddLogoModal, AddLogoModal } = useAddLogoModal();+ const { setShowAddColorModal, AddColorModal } = useAddColorModal();const { executeAsync } = useAction(deleteProgramResourceAction, {onSuccess: ({ input }) => {@@ -55,6 +57,7 @@ export function ProgramResourcesPageClient() {return (<><AddLogoModal />+ <AddColorModal /><div className="flex flex-col gap-10"><Sectionresource="logo"@@ -85,7 +88,24 @@ export function ProgramResourcesPageClient() {resource="color"title="Colors"description="Provide affiliates with official colors"- ></Section>+ onAdd={() => setShowAddColorModal(true)}+ >+ {resources?.colors?.map((color) => (+ <ResourceCard+ key={color.id}+ resourceType="color"+ icon={+ <div+ className="size-full"+ style={{ backgroundColor: color.color }}+ />+ }+ title={color.name || "Color"}+ description={color.color.toUpperCase()}+ onDelete={() => handleDelete("color", color.id)}+ />+ ))}+ </Section><Sectionresource="file"title="Additional files"@@ -172,7 +192,7 @@ function ResourceCard({return (<div className="flex w-full items-center justify-between gap-4 rounded-lg border border-neutral-200 p-4 shadow-sm"><div className="flex min-w-0 items-center gap-4">- <div className="flex size-10 shrink-0 items-center justify-center rounded-md border border-neutral-200">+ <div className="flex size-10 shrink-0 items-center justify-center overflow-hidden rounded-md border border-neutral-200">{icon}</div><div className="flex min-w-0 flex-col">