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 type selector

Tim Wilson4abad5c49ca7
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/[emailId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/[emailId]/page-client.tsx
index d461051be9..8c28bbb4e9 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/[emailId]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/[emailId]/page-client.tsx
@@ -5,11 +5,17 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { programEmailSchema } from "@/lib/zod/schemas/program-emails";
import { PageContent } from "@/ui/layout/page-content";
import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
+import { EmailTypeSelector } from "@/ui/partners/emails/email-type-selector";
import { ChevronRight, PaperPlane, RichTextArea } from "@dub/ui";
import { useAction } from "next-safe-action/hooks";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
-import { Controller, useForm } from "react-hook-form";
+import {
+ Controller,
+ FormProvider,
+ useForm,
+ useFormContext,
+} from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
@@ -23,6 +29,8 @@ export function ProgramEmailPageClient({ emailId }: { emailId: string }) {
type ProgramEmailFormData = z.infer<typeof programEmailSchema>;
+const useProgramEmailFormContext = () => useFormContext<ProgramEmailFormData>();
+
const labelClassName = "text-sm font-medium text-content-muted";
const inputClassName =
"hover:border-border-subtle h-7 w-full rounded-md transition-colors duration-150 focus:border-black/75 border focus:ring-black/75 border-transparent px-1.5 py-0 text-sm text-content-default placeholder:text-content-muted";
@@ -50,104 +58,120 @@ function ProgramEmailForm() {
const type = watch("type");
return (
- <PageContent
- title={
- <div className="flex items-center gap-1.5">
- <Link
- href={`/${workspaceSlug}/program/emails`}
- className="bg-bg-subtle hover:bg-bg-emphasis flex size-8 shrink-0 items-center justify-center rounded-lg transition-[transform,background-color] duration-150 active:scale-95"
- >
- <PaperPlane className="text-content-default size-4" />
- </Link>
- <ChevronRight className="text-content-muted size-2.5 shrink-0 [&_*]:stroke-2" />
- <span>{`New ${type === "campaign" ? "email" : type}`}</span>
- </div>
- }
- controls={<></>}
- >
- <PageWidthWrapper className="mb-8 max-w-[600px]">
- <div className="grid grid-cols-[max-content_minmax(0,1fr)] items-center gap-x-6 gap-y-2">
- <label className="contents">
- <span className={labelClassName}>Name</span>
- <input
- type="text"
- placeholder="Enter a name..."
- className={inputClassName}
- {...register("name")}
- />
- </label>
-
- <label className="contents">
- <span className={labelClassName}>To</span>
- <div />
- </label>
-
- <label className="contents">
- <span className={labelClassName}>Subject</span>
- <input
- type="text"
- placeholder="Enter a subject..."
- className={inputClassName}
- {...register("subject")}
- />
- </label>
-
- <label className="contents">
- <span className={labelClassName}>Delivery</span>
- <div />
- </label>
- </div>
-
- <div className="mt-6">
- <Controller
- control={control}
- name="body"
- render={({ field }) => (
- <RichTextArea
- editorClassName="-m-2 min-h-[200px] p-2"
- initialValue={field.value}
- onChange={field.onChange}
- variables={["PartnerName", "PartnerEmail"]}
- uploadImage={async (file) => {
- try {
- const result = await executeImageUpload({
- workspaceId: workspaceId!,
- });
-
- if (!result?.data)
- throw new Error("Failed to get signed upload URL");
-
- const { signedUrl, destinationUrl } = result.data;
-
- const uploadResponse = await fetch(signedUrl, {
- method: "PUT",
- body: file,
- headers: {
- "Content-Type": file.type,
- "Content-Length": file.size.toString(),
- },
- });
-
- if (!uploadResponse.ok)
- throw new Error("Failed to upload to signed URL");
-
- return destinationUrl;
- } catch (e) {
- console.error("Failed to upload image", e);
- toast.error("Failed to upload image");
- }
-
- return null;
- }}
+ <FormProvider {...form}>
+ <PageContent
+ title={
+ <div className="flex items-center gap-1.5">
+ <Link
+ href={`/${workspaceSlug}/program/emails`}
+ className="bg-bg-subtle hover:bg-bg-emphasis flex size-8 shrink-0 items-center justify-center rounded-lg transition-[transform,background-color] duration-150 active:scale-95"
+ >
+ <PaperPlane className="text-content-default size-4" />
+ </Link>
+ <ChevronRight className="text-content-muted size-2.5 shrink-0 [&_*]:stroke-2" />
+ <span>{`New ${type === "campaign" ? "email" : type}`}</span>
+ </div>
+ }
+ controls={<></>}
+ >
+ <PageWidthWrapper className="mb-8 max-w-[600px]">
+ <div className="grid grid-cols-[max-content_minmax(0,1fr)] items-center gap-x-6 gap-y-2">
+ <label className="contents">
+ <span className={labelClassName}>Name</span>
+ <input
+ type="text"
+ placeholder="Enter a name..."
+ className={inputClassName}
+ {...register("name")}
/>
- )}
- />
- </div>
-
- <div className="border-border-subtle mt-4 w-full border-t pt-4 text-center text-xs font-medium text-neutral-300">
- End of email
- </div>
- </PageWidthWrapper>
- </PageContent>
+ </label>
+
+ <label className="contents">
+ <span className={labelClassName}>To</span>
+ <div />
+ </label>
+
+ <label className="contents">
+ <span className={labelClassName}>Subject</span>
+ <input
+ type="text"
+ placeholder="Enter a subject..."
+ className={inputClassName}
+ {...register("subject")}
+ />
+ </label>
+
+ <label className="contents">
+ <span className={labelClassName}>Type</span>
+ <Controller
+ control={control}
+ name="type"
+ render={({ field }) => (
+ <EmailTypeSelector
+ value={field.value}
+ onChange={field.onChange}
+ />
+ )}
+ />
+ </label>
+
+ <label className="contents">
+ <span className={labelClassName}>Delivery</span>
+ <div />
+ </label>
+ </div>
+
+ <div className="mt-6">
+ <Controller
+ control={control}
+ name="body"
+ render={({ field }) => (
+ <RichTextArea
+ editorClassName="-m-2 min-h-[200px] p-2"
+ initialValue={field.value}
+ onChange={field.onChange}
+ variables={["PartnerName", "PartnerEmail"]}
+ uploadImage={async (file) => {
+ try {
+ const result = await executeImageUpload({
+ workspaceId: workspaceId!,
+ });
+
+ if (!result?.data)
+ throw new Error("Failed to get signed upload URL");
+
+ const { signedUrl, destinationUrl } = result.data;
+
+ const uploadResponse = await fetch(signedUrl, {
+ method: "PUT",
+ body: file,
+ headers: {
+ "Content-Type": file.type,
+ "Content-Length": file.size.toString(),
+ },
+ });
+
+ if (!uploadResponse.ok)
+ throw new Error("Failed to upload to signed URL");
+
+ return destinationUrl;
+ } catch (e) {
+ console.error("Failed to upload image", e);
+ toast.error("Failed to upload image");
+ }
+
+ return null;
+ }}
+ />
+ )}
+ />
+ </div>
+
+ <div className="border-border-subtle mt-4 w-full border-t pt-4 text-center text-xs font-medium text-neutral-300">
+ End of email
+ </div>
+ </PageWidthWrapper>
+ </PageContent>
+ </FormProvider>
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/create-email-button.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/create-email-button.tsx
index 2978cd7383..ddbf57421b 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/create-email-button.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/emails/create-email-button.tsx
@@ -1,12 +1,7 @@
"use client";
import useWorkspace from "@/lib/swr/use-workspace";
-import {
- buttonVariants,
- Megaphone,
- useKeyboardShortcut,
- Workflow,
-} from "@dub/ui";
+import { buttonVariants, useKeyboardShortcut } from "@dub/ui";
import { cn } from "@dub/utils";
import Link from "next/link";
import { useRouter } from "next/navigation";
@@ -41,20 +36,3 @@ export function CreateEmailButton() {
</>
);
}
-
-const emailTypes = [
- {
- type: "campaign",
- icon: Megaphone,
- name: "Campaign",
- description: "Sent once manually",
- colorClassName: "text-blue-700 bg-blue-100",
- },
- {
- type: "automation",
- icon: Workflow,
- name: "Automation",
- description: "Triggered by an event",
- colorClassName: "text-green-700 bg-green-100",
- },
-];
diff --git a/apps/web/ui/partners/emails/email-type-selector.tsx b/apps/web/ui/partners/emails/email-type-selector.tsx
new file mode 100644
index 0000000000..e6581067c1
--- /dev/null
+++ b/apps/web/ui/partners/emails/email-type-selector.tsx
@@ -0,0 +1,100 @@
+"use client";
+
+import { Popover } from "@dub/ui";
+import { Megaphone, Workflow } from "@dub/ui/icons";
+import { cn } from "@dub/utils";
+import { Command } from "cmdk";
+import { memo, useState } from "react";
+
+export const EMAIL_TYPES = [
+ {
+ type: "campaign",
+ icon: Megaphone,
+ name: "Campaign",
+ description: "Sent once manually",
+ colorClassName: "text-green-700 bg-green-100",
+ },
+ {
+ type: "automation",
+ icon: Workflow,
+ name: "Automation",
+ description: "Triggered by an event",
+ colorClassName: "text-blue-700 bg-blue-100",
+ },
+];
+
+export const EmailTypeSelector = memo(function EmailTypeSelector({
+ value,
+ onChange,
+}: {
+ value: string;
+ onChange: (value: string) => void;
+}) {
+ const [isOpen, setIsOpen] = useState(false);
+
+ const selectedType = EMAIL_TYPES.find((type) => type.type === value);
+ if (!selectedType)
+ throw new Error("Invalid email type passed to TypeSelector");
+
+ return (
+ <Popover
+ openPopover={isOpen}
+ setOpenPopover={setIsOpen}
+ content={
+ <Command
+ className="p-1 focus:outline-none sm:min-w-[240px]"
+ tabIndex={0}
+ loop
+ >
+ <Command.List>
+ {EMAIL_TYPES.sort((a) =>
+ a.type === selectedType.type ? -1 : 1,
+ ).map(({ type, icon: Icon, name, description, colorClassName }) => (
+ <Command.Item
+ key={type}
+ onSelect={() => {
+ onChange(type);
+ setIsOpen(false);
+ }}
+ className="flex cursor-pointer items-center gap-2.5 rounded-md p-1 data-[selected=true]:bg-neutral-50"
+ >
+ <div
+ className={cn(
+ "flex size-10 shrink-0 items-center justify-center rounded-lg",
+ colorClassName,
+ )}
+ >
+ <Icon className="size-5" />
+ </div>
+ <div className="flex flex-col text-neutral-700">
+ <span className="text-sm font-semibold">{name}</span>
+ <p className="text-xs">{description}</p>
+ </div>
+ </Command.Item>
+ ))}
+ </Command.List>
+ </Command>
+ }
+ side="bottom"
+ align="start"
+ >
+ <button
+ type="button"
+ className={cn(
+ "text-content-default flex h-7 w-fit items-center gap-2 rounded-md border border-transparent bg-white px-1.5 text-sm",
+ "hover:border-border-subtle data-[state=open]:border-border-subtle transition-colors duration-150 data-[state=open]:bg-neutral-100",
+ )}
+ >
+ <div
+ className={cn(
+ selectedType.colorClassName,
+ "flex size-5 items-center justify-center rounded",
+ )}
+ >
+ <selectedType.icon className="size-3.5" />
+ </div>
+ {selectedType.name}
+ </button>
+ </Popover>
+ );
+});