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.

Clear support chat attachments and describe email variables (#4354)

Pedro Ladeira8f763dac5b27
diff --git a/apps/web/app/(ee)/api/campaigns/[campaignId]/preview/route.ts b/apps/web/app/(ee)/api/campaigns/[campaignId]/preview/route.ts
index 8c1ab4febe..e8b2fb0150 100644
--- a/apps/web/app/(ee)/api/campaigns/[campaignId]/preview/route.ts
+++ b/apps/web/app/(ee)/api/campaigns/[campaignId]/preview/route.ts
@@ -11,8 +11,10 @@ import {
} from "@/lib/email/parse-campaign-from-address";
import { TiptapNode } from "@/lib/types";
import {
- CampaignSchema,
campaignFromSchema,
+ CampaignSchema,
+ EMAIL_TEMPLATE_VARIABLE_INFO,
+ EMAIL_TEMPLATE_VARIABLES,
} from "@/lib/zod/schemas/campaigns";
import { sendBatchEmail } from "@dub/email";
import CampaignEmail from "@dub/email/templates/campaign-email";
@@ -102,16 +104,12 @@ export const POST = withWorkspace(
preview,
body: renderCampaignEmailHTML({
content: bodyJson as unknown as TiptapNode,
- variables: {
- PartnerName: "Partner",
- PartnerEmail: "[email protected]",
- PartnerLink: "https://acme.com/partner",
- SaleReward: "30% per sale for 6 months",
- LeadReward: "$10 per lead",
- ClickReward: "$0.50 per click",
- ReferralReward:
- "10% per referred partner's commission for 1 year",
- },
+ variables: Object.fromEntries(
+ EMAIL_TEMPLATE_VARIABLES.map((key) => [
+ key,
+ EMAIL_TEMPLATE_VARIABLE_INFO[key].example,
+ ]),
+ ),
}),
},
}),
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsx
index 182eb463bf..e6c8b34350 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsx
@@ -10,7 +10,10 @@ import { useEmailDomains } from "@/lib/swr/use-email-domains";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { Campaign, UpdateCampaignFormData } from "@/lib/types";
-import { EMAIL_TEMPLATE_VARIABLES } from "@/lib/zod/schemas/campaigns";
+import {
+ EMAIL_TEMPLATE_VARIABLE_INFO,
+ EMAIL_TEMPLATE_VARIABLES,
+} from "@/lib/zod/schemas/campaigns";
import { PageContentWithSidePanel } from "@/ui/layout/page-content/page-content-with-side-panel";
import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
import {
@@ -690,6 +693,7 @@ export function CampaignEditor({ campaign }: { campaign: Campaign }) {
initialValue={field.value}
onChange={(editor) => field.onChange(editor.getJSON())}
variables={[...EMAIL_TEMPLATE_VARIABLES]}
+ variableInfo={EMAIL_TEMPLATE_VARIABLE_INFO}
editable={!isLocked}
uploadImage={async (file) => {
try {
diff --git a/apps/web/lib/zod/schemas/campaigns.ts b/apps/web/lib/zod/schemas/campaigns.ts
index 6259c740a1..123e80ba7f 100644
--- a/apps/web/lib/zod/schemas/campaigns.ts
+++ b/apps/web/lib/zod/schemas/campaigns.ts
@@ -21,6 +21,41 @@ export const EMAIL_TEMPLATE_VARIABLES = [
"ReferralReward",
] as const;
+export const EMAIL_TEMPLATE_VARIABLE_INFO: Record<
+ (typeof EMAIL_TEMPLATE_VARIABLES)[number],
+ { description: string; example: string; hideExample?: boolean }
+> = {
+ PartnerName: {
+ description: "The partner's full name",
+ example: "John Doe",
+ },
+ PartnerEmail: {
+ description: "The partner's email address",
+ example: "[email protected]",
+ },
+ PartnerLink: {
+ description: "The partner's default referral link",
+ example: "refer.dub.co/john",
+ },
+ SaleReward: {
+ description: "The partner's sale commission",
+ example: "30% per sale for 6 months",
+ },
+ LeadReward: {
+ description: "The partner's lead commission",
+ example: "$10 per lead",
+ },
+ ClickReward: {
+ description: "The partner's click commission",
+ example: "$0.50 per click",
+ },
+ ReferralReward: {
+ description: "The partner's commission for referring another partner",
+ example: "10% per referred partner's commission for 1 year",
+ hideExample: true,
+ },
+};
+
export const CAMPAIGN_FROM_FORMAT_ERROR =
'From must be an email or "Name <[email protected]>" format.';
diff --git a/apps/web/ui/support/chat-interface.tsx b/apps/web/ui/support/chat-interface.tsx
index 346663ff15..eef0c8ad32 100644
--- a/apps/web/ui/support/chat-interface.tsx
+++ b/apps/web/ui/support/chat-interface.tsx
@@ -154,6 +154,7 @@ export function ChatInterface({
const last = current[current.length - 1];
return last.role === "user" ? current.slice(0, -1) : current;
});
+ setPendingImages(pendingSendRef.current);
pendingSendRef.current = null;
}
return;
@@ -244,6 +245,9 @@ export function ChatInterface({
pendingImagesRef.current.forEach((image) =>
URL.revokeObjectURL(image.previewUrl),
);
+ pendingSendRef.current?.forEach((image) =>
+ URL.revokeObjectURL(image.previewUrl),
+ );
};
}, []);
@@ -348,6 +352,7 @@ export function ChatInterface({
}
setInput("");
+ setPendingImages([]);
textareaRef.current?.focus();
} catch (err) {
sendingRef.current = false;
diff --git a/packages/ui/src/rich-text-area/rich-text-provider.tsx b/packages/ui/src/rich-text-area/rich-text-provider.tsx
index 575f6dd82e..665f2ecf7a 100644
--- a/packages/ui/src/rich-text-area/rich-text-provider.tsx
+++ b/packages/ui/src/rich-text-area/rich-text-provider.tsx
@@ -18,7 +18,7 @@ import {
useState,
} from "react";
import { configureCampaignEditorImage } from "./campaign-editor-image";
-import { suggestions } from "./variables";
+import { RichTextVariableInfo, suggestions } from "./variables";
export const PROSE_STYLES = {
default: "prose-p:my-2 prose-ul:my-2 prose-ol:my-2",
@@ -54,6 +54,7 @@ type RichTextProviderProps = PropsWithChildren<{
onChange?: (editor: Editor) => void;
uploadImage?: (file: File) => Promise<string | null>;
variables?: string[];
+ variableInfo?: Record<string, RichTextVariableInfo>;
editable?: boolean;
autoFocus?: boolean;
@@ -94,6 +95,7 @@ export const RichTextProvider = forwardRef<
editable,
autoFocus,
variables,
+ variableInfo,
initialValue,
onChange,
editorProps,
@@ -250,7 +252,7 @@ export const RichTextProvider = forwardRef<
: `{{${node.attrs.id}}}`;
},
}).configure({
- suggestion: suggestions(variables),
+ suggestion: suggestions(variables, variableInfo),
}),
]
: []),
diff --git a/packages/ui/src/rich-text-area/variables.tsx b/packages/ui/src/rich-text-area/variables.tsx
index ae58daea17..08483695ae 100644
--- a/packages/ui/src/rich-text-area/variables.tsx
+++ b/packages/ui/src/rich-text-area/variables.tsx
@@ -11,6 +11,12 @@ import {
import { Button } from "../button";
import { Input } from "../input";
+export type RichTextVariableInfo = {
+ description?: string;
+ example?: string;
+ hideExample?: boolean;
+};
+
const updatePosition = (editor: Editor, element: HTMLElement) => {
const virtualElement = {
getBoundingClientRect: () =>
@@ -33,7 +39,10 @@ const updatePosition = (editor: Editor, element: HTMLElement) => {
});
};
-export const suggestions = (variables: string[]) => ({
+export const suggestions = (
+ variables: string[],
+ variableInfo?: Record<string, RichTextVariableInfo>,
+) => ({
items: ({ query }: { query: string }) => {
const q = query.trim().toLowerCase();
if (!q) return variables.slice(0, 10);
@@ -60,7 +69,7 @@ export const suggestions = (variables: string[]) => ({
command?: (props: any) => void;
}) => {
component = new ReactRenderer(Menu, {
- props,
+ props: { ...props, variableInfo },
editor: props.editor,
});
@@ -74,7 +83,7 @@ export const suggestions = (variables: string[]) => ({
},
onUpdate(props: any) {
- component.updateProps(props);
+ component.updateProps({ ...props, variableInfo });
if (!props.clientRect) return;
@@ -102,7 +111,7 @@ export const suggestions = (variables: string[]) => ({
});
const menuItemClassName = cn(
- "flex cursor-pointer select-none items-center gap-2 whitespace-nowrap rounded-md px-2 py-1 font-mono text-sm text-neutral-950",
+ "flex cursor-pointer select-none items-center gap-2 whitespace-nowrap rounded-md px-2 py-1 text-sm text-neutral-950",
"data-[selected=true]:bg-neutral-100",
);
@@ -111,9 +120,11 @@ const Menu = forwardRef(
{
items,
command,
+ variableInfo,
}: {
items: string[];
command: (props: any) => void;
+ variableInfo?: Record<string, RichTextVariableInfo>;
},
ref,
) => {
@@ -241,22 +252,63 @@ const Menu = forwardRef(
);
}
+ const hasInfo = items.some(
+ (item) =>
+ variableInfo?.[item]?.description || variableInfo?.[item]?.example,
+ );
+
return (
- <div className="border-border-subtle flex flex-col rounded-lg border bg-white p-1 shadow-sm">
+ <div
+ className={cn(
+ "border-border-subtle flex flex-col rounded-lg border bg-white p-1 shadow-sm",
+ hasInfo && "w-96",
+ )}
+ >
{items.length ? (
- items.map((item, index) => (
- <button
- key={index}
- type="button"
- onMouseDown={(e) => e.preventDefault()}
- onClick={() => selectVar(item)}
- data-selected={selectedIndex === index}
- onPointerEnter={() => setSelectedIndex(index)}
- className={menuItemClassName}
- >
- {item}
- </button>
- ))
+ items.map((item, index) => {
+ const info = variableInfo?.[item];
+ const example = info?.hideExample ? undefined : info?.example;
+
+ return (
+ <button
+ key={index}
+ type="button"
+ onMouseDown={(e) => e.preventDefault()}
+ onClick={() => selectVar(item)}
+ data-selected={selectedIndex === index}
+ onPointerEnter={() => setSelectedIndex(index)}
+ className={cn(
+ menuItemClassName,
+ info ? "items-start whitespace-normal py-1" : "font-mono",
+ )}
+ >
+ {info ? (
+ <div className="flex min-w-0 flex-col text-left">
+ <span className="font-mono text-sm leading-5">{item}</span>
+ {info.description || example ? (
+ <span className="text-xs font-normal leading-4">
+ {info.description ? (
+ <span className="text-content-subtle">
+ {info.description}
+ </span>
+ ) : null}
+ {info.description && example ? (
+ <span className="text-content-muted"> · </span>
+ ) : null}
+ {example ? (
+ <span className="text-content-default">
+ {example}
+ </span>
+ ) : null}
+ </span>
+ ) : null}
+ </div>
+ ) : (
+ item
+ )}
+ </button>
+ );
+ })
) : (
<div
className={cn(menuItemClassName, "text-content-subtle font-sans")}