The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add other workflow conditions
Kiran K2b5d48606d87
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsxindex 63b602bdda..82581d70ae 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx@@ -1,22 +1,33 @@+import {+ isCurrencyAttribute,+ isDaysAttribute,+} from "@/lib/api/workflows/utils";+import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";import {ALLOWED_ATTRIBUTE_VALUES_IN_DAYS,- CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS,+ CAMPAIGN_WORKFLOW_ATTRIBUTES,} from "@/lib/zod/schemas/campaigns";+import { WORKFLOW_ATTRIBUTE_LABELS } from "@/lib/zod/schemas/workflows";import {InlineBadgePopover,+ InlineBadgePopoverContext,InlineBadgePopoverMenu,} from "@/ui/shared/inline-badge-popover";-import { pluralize } from "@dub/utils";+import { cn, currencyFormatter, pluralize } from "@dub/utils";+import { useContext } from "react";import { Controller } from "react-hook-form";import { useCampaignFormContext } from "./campaign-form-context";export function TransactionalCampaignLogic() {- const { control } = useCampaignFormContext();+ const { control, watch } = useCampaignFormContext();++ const attribute = watch("triggerCondition.attribute");+ const value = watch("triggerCondition.value");return (<div className="flex h-8 w-full items-center px-2"><span className="text-content-default flex gap-1 text-sm font-medium leading-relaxed">- When partner has+ When partner's<div className="inline-flex items-center gap-1"><Controllercontrol={control}@@ -25,59 +36,142 @@ export function TransactionalCampaignLogic() {<InlineBadgePopovertext={field.value- ? CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS[field.value]- : CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS.partnerEnrolledDays+ ? WORKFLOW_ATTRIBUTE_LABELS[field.value].toLowerCase()+ : "activity"}invalid={!field.value}><InlineBadgePopoverMenuselectedValue={field.value}onSelect={field.onChange}- items={Object.entries(CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS).map(- ([key, value]) => ({- text: value,- value: key,- }),- )}+ items={CAMPAIGN_WORKFLOW_ATTRIBUTES.map((attr) => ({+ text: WORKFLOW_ATTRIBUTE_LABELS[attr].toLowerCase(),+ value: attr,+ }))}/></InlineBadgePopover>)}/>- <Controller- control={control}- name="triggerCondition.value"- render={({ field }) => (- <>- <InlineBadgePopover- text={- field.value !== undefined && field.value !== null- ? String(field.value)- : "1"- }- invalid={field.value === undefined || field.value === null}- >- <InlineBadgePopoverMenu- selectedValue={- field.value !== undefined && field.value !== null- ? String(field.value)- : "1"+ {attribute && (+ <>+ {isDaysAttribute(attribute) ? (+ <Controller+ control={control}+ name="triggerCondition.value"+ render={({ field }) => (+ <>+ <InlineBadgePopover+ text={+ field.value !== undefined && field.value !== null+ ? String(field.value)+ : "1"+ }+ invalid={+ field.value === undefined || field.value === null+ }+ >+ <InlineBadgePopoverMenu+ selectedValue={+ field.value !== undefined && field.value !== null+ ? String(field.value)+ : "1"+ }+ onSelect={(val) => field.onChange(Number(val))}+ items={ALLOWED_ATTRIBUTE_VALUES_IN_DAYS.filter(+ (days) => days !== 0,+ ).map((days) => ({+ text: String(days),+ value: String(days),+ }))}+ />+ </InlineBadgePopover>+ {pluralize("day", field.value || 1)}+ </>+ )}+ />+ ) : (+ <>+ is at least{" "}+ <InlineBadgePopover+ text={+ value+ ? isCurrencyAttribute(attribute)+ ? currencyFormatter(value / 100)+ : value+ : "amount"}- onSelect={(val) => field.onChange(Number(val))}- items={ALLOWED_ATTRIBUTE_VALUES_IN_DAYS.filter(- (days) => days !== 0,- ).map((days) => ({- text: String(days),- value: String(days),- }))}- />- </InlineBadgePopover>- {pluralize("day", field.value || 1)}- </>- )}- />+ invalid={!value}+ >+ <ValueInput />+ </InlineBadgePopover>+ </>+ )}+ </>+ )}</div></span></div>);}++function ValueInput() {+ const { watch, register, setValue } = useCampaignFormContext();+ const { setIsOpen } = useContext(InlineBadgePopoverContext);++ const attribute = watch("triggerCondition.attribute");+ const storedValue = watch("triggerCondition.value");+ const isCurrency = isCurrencyAttribute(attribute);++ // Display value in dollars for currency, but store in cents+ const displayValue =+ isCurrency && storedValue ? storedValue / 100 : storedValue;++ return (+ <div className="relative rounded-md shadow-sm">+ {isCurrency && (+ <span className="absolute inset-y-0 left-0 flex items-center pl-1.5 text-sm text-neutral-400">+ $+ </span>+ )}+ <input+ className={cn(+ "block w-full rounded-md border-neutral-300 px-1.5 py-1 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:w-32 sm:text-sm",+ isCurrency ? "pl-4 pr-12" : "pr-7",+ )}+ value={displayValue ?? ""}+ onChange={(e) => {+ const value = e.target.value;+ if (value === "") {+ setValue("triggerCondition.value", null as any);+ } else {+ const numValue = +value;+ setValue(+ "triggerCondition.value",+ isCurrency ? Math.round(numValue * 100) : numValue,+ );+ }+ if (isCurrency) {+ handleMoneyInputChange(e);+ }+ }}+ onKeyDown={(e) => {+ if (e.key === "Enter") {+ e.preventDefault();+ setIsOpen(false);+ return;+ }++ if (isCurrency) {+ handleMoneyKeyDown(e);+ }+ }}+ />+ {isCurrency && (+ <span className="absolute inset-y-0 right-0 flex items-center pr-1.5 text-sm text-neutral-400">+ USD+ </span>+ )}+ </div>+ );+}diff --git a/apps/web/lib/zod/schemas/campaigns.ts b/apps/web/lib/zod/schemas/campaigns.tsindex 396e51c943..b4ea40a530 100644--- a/apps/web/lib/zod/schemas/campaigns.ts+++ b/apps/web/lib/zod/schemas/campaigns.ts@@ -13,13 +13,27 @@ export const EMAIL_TEMPLATE_VARIABLES = ["PartnerEmail",] as const;-export const CAMPAIGN_WORKFLOW_ATTRIBUTES = ["partnerEnrolledDays"] as const;+export const CAMPAIGN_WORKFLOW_ATTRIBUTES = [+ "totalClicks",+ "totalLeads",+ "totalConversions",+ "totalSales",+ "totalSaleAmount",+ "totalCommissions",+ "partnerEnrolledDays",+] as const;export const CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS: Record<CampaignWorkflowAttribute,string> = {- partnerEnrolledDays: "been in the program for",+ totalClicks: "Clicks",+ totalLeads: "Leads",+ totalConversions: "Conversions",+ totalSales: "Sales",+ totalSaleAmount: "Revenue",+ totalCommissions: "Commissions",+ partnerEnrolledDays: "days since enrollment",} as const;export const CampaignSchema = z.object({diff --git a/apps/web/lib/zod/schemas/workflows.ts b/apps/web/lib/zod/schemas/workflows.tsindex 762f9ae0d0..bd7a7ba2bd 100644--- a/apps/web/lib/zod/schemas/workflows.ts+++ b/apps/web/lib/zod/schemas/workflows.ts@@ -7,8 +7,10 @@ import { WorkflowTrigger } from "@dub/prisma/client";import { z } from "zod";export const WORKFLOW_ATTRIBUTES = [+ "totalClicks","totalLeads","totalConversions",+ "totalSales","totalSaleAmount","totalCommissions","partnerEnrolledDays",@@ -18,19 +20,23 @@ export const WORKFLOW_ATTRIBUTE_LABELS: Record<WorkflowConditionAttribute,string> = {+ totalClicks: "Clicks",totalLeads: "Leads",totalConversions: "Conversions",+ totalSales: "Sales",totalSaleAmount: "Revenue",totalCommissions: "Commissions",- partnerEnrolledDays: "been in the program for",+ partnerEnrolledDays: "days since enrollment",} as const;export const WORKFLOW_ATTRIBUTE_TRIGGER: Record<WorkflowConditionAttribute,WorkflowTrigger> = {+ totalClicks: WorkflowTrigger.clickRecorded,totalLeads: WorkflowTrigger.leadRecorded,totalConversions: WorkflowTrigger.saleRecorded,+ totalSales: WorkflowTrigger.saleRecorded,totalSaleAmount: WorkflowTrigger.saleRecorded,totalCommissions: WorkflowTrigger.commissionEarned,partnerEnrolledDays: WorkflowTrigger.partnerEnrolled,