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.

Improve `customer` `source` reward condition

Steven Tey69b8e85f1e09
diff --git a/apps/web/lib/integrations/hubspot/track-lead.ts b/apps/web/lib/integrations/hubspot/track-lead.ts
index 26e794ab37..7808fbdaea 100644
--- a/apps/web/lib/integrations/hubspot/track-lead.ts
+++ b/apps/web/lib/integrations/hubspot/track-lead.ts
@@ -56,6 +56,7 @@ export const trackHubSpotLeadEvent = async ({
customerName,
mode: "deferred",
workspace,
+ source: "hubspot",
commissionSource: CommissionSource.hubspot,
});
@@ -157,6 +158,7 @@ export const trackHubSpotLeadEvent = async ({
customerEmail: properties.email,
mode: "async",
workspace,
+ source: "hubspot",
commissionSource: CommissionSource.hubspot,
});
@@ -230,6 +232,7 @@ const trackFinalLead = async ({
customerEmail: contactInfo.properties.email,
mode: "async",
workspace,
+ source: "hubspot",
commissionSource: CommissionSource.hubspot,
});
diff --git a/apps/web/lib/rewards/get-customer-source-availability.ts b/apps/web/lib/rewards/get-customer-source-availability.ts
new file mode 100644
index 0000000000..21f5a7039b
--- /dev/null
+++ b/apps/web/lib/rewards/get-customer-source-availability.ts
@@ -0,0 +1,41 @@
+import { SUBMITTED_LEADS_ENABLED_PROGRAM_IDS } from "@/lib/submitted-leads/constants";
+import { CustomerSource } from "@/lib/types";
+import { HUBSPOT_INTEGRATION_ID, STRIPE_INTEGRATION_ID } from "@dub/utils";
+
+type RequiredIntegration = { id: string; slug: string; name: string };
+
+const CUSTOMER_SOURCE_REQUIRED_INTEGRATIONS: Partial<
+ Record<CustomerSource, RequiredIntegration>
+> = {
+ trial: { id: STRIPE_INTEGRATION_ID, slug: "stripe", name: "Stripe" },
+ hubspot: { id: HUBSPOT_INTEGRATION_ID, slug: "hubspot", name: "HubSpot" },
+};
+
+export function getCustomerSourceAvailability({
+ source,
+ programId,
+ installedIntegrationIds,
+}: {
+ source: string;
+ programId?: string;
+ installedIntegrationIds?: string[];
+}): { hidden: boolean; missingIntegration?: RequiredIntegration } {
+ if (source === "submitted") {
+ return {
+ hidden:
+ !programId || !SUBMITTED_LEADS_ENABLED_PROGRAM_IDS.includes(programId),
+ };
+ }
+
+ const requiredIntegration =
+ CUSTOMER_SOURCE_REQUIRED_INTEGRATIONS[source as CustomerSource];
+
+ if (
+ requiredIntegration &&
+ !installedIntegrationIds?.includes(requiredIntegration.id)
+ ) {
+ return { hidden: false, missingIntegration: requiredIntegration };
+ }
+
+ return { hidden: false };
+}
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 1440cc8726..c06de3336c 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -1,4 +1,6 @@
+import { WEBHOOK_REQUEST_ACTORS_BY_PATH } from "@/lib/api-logs/constants";
import { PARTNER_REFERRAL_TRIGGER } from "@/lib/partner-referrals/constants";
+import { DUB_LOGO_SQUARE } from "@dub/utils";
import {
EventType,
RewardSpendLimitInterval,
@@ -36,6 +38,8 @@ export type RewardConditionEntityAttribute = {
options?: {
id: string;
label: string;
+ description?: string;
+ icon?: string;
}[];
};
@@ -132,14 +136,32 @@ export const REWARD_CONDITIONS: Record<
{
id: "tracked",
label: "tracked lead",
+ description:
+ "Leads tracked via [Dub's API](https://dub.co/docs/api-reference/track/lead)",
+ icon: DUB_LOGO_SQUARE,
},
{
id: "submitted",
label: "submitted lead",
+ description:
+ "Leads [submitted by partners](https://dub.co/help/article/submitted-leads)",
+ icon: DUB_LOGO_SQUARE,
},
{
id: "trial",
- label: "free trial",
+ label: "Stripe free trial",
+ description:
+ "Free trials recorded by the [Stripe integration](https://dub.co/docs/integrations/stripe#tracking-free-trials)",
+ icon: WEBHOOK_REQUEST_ACTORS_BY_PATH[
+ "/stripe/integration/webhook"
+ ].image,
+ },
+ {
+ id: "hubspot",
+ label: "HubSpot lead",
+ description:
+ "Leads recorded by the [HubSpot integration](https://dub.co/docs/integrations/hubspot)",
+ icon: WEBHOOK_REQUEST_ACTORS_BY_PATH["/hubspot/webhook"].image,
},
],
},
@@ -600,7 +622,12 @@ export const REWARD_EVENT_RELATION_MAPPING = Object.freeze({
custom: "customReward",
});
-export const CUSTOMER_SOURCES = ["tracked", "submitted", "trial"] as const;
+export const CUSTOMER_SOURCES = [
+ "tracked",
+ "submitted",
+ "trial",
+ "hubspot",
+] as const;
export const rewardContextSchema = z.object({
customer: z
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index 86b0bc52fd..b7de3a7df6 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -2,7 +2,8 @@
import { constructRewardAmount } from "@/lib/api/sales/construct-reward-amount";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
-import { SUBMITTED_LEADS_ENABLED_PROGRAM_IDS } from "@/lib/submitted-leads/constants";
+import { getCustomerSourceAvailability } from "@/lib/rewards/get-customer-source-availability";
+import useIntegrations from "@/lib/swr/use-integrations";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { RECURRING_MAX_DURATIONS } from "@/lib/zod/schemas/misc";
@@ -31,6 +32,7 @@ import {
InvoiceDollar,
MoneyBills2,
Popover,
+ TooltipContent,
User,
Users,
} from "@dub/ui";
@@ -376,7 +378,9 @@ function ConditionLogic({
conditionIndex: number;
onRemove?: () => void;
}) {
+ const { slug: workspaceSlug } = useWorkspace();
const { program } = useProgram();
+ const { integrations } = useIntegrations();
const modifierKey = `modifiers.${modifierIndex}` as const;
const conditionKey = `${modifierKey}.conditions.${conditionIndex}` as const;
@@ -714,6 +718,7 @@ function ConditionLogic({
isNaN(Number(condition.value))
: !condition.value
}
+ align="center"
buttonClassName={cn(
condition.attribute === "productId" &&
"rounded-r-none",
@@ -764,29 +769,55 @@ function ConditionLogic({
// Select option selector
<InlineBadgePopoverMenu
search={attribute.options.length > 4}
+ className={
+ isCustomerSourceCondition ? "max-w-80" : undefined
+ }
selectedValue={getConditionMenuSelectedValue(
condition.value,
isArrayValue,
)}
- items={attribute.options
- .filter(({ id }) => {
- if (
- isCustomerSourceCondition &&
- id === "submitted"
- ) {
- return (
- program &&
- SUBMITTED_LEADS_ENABLED_PROGRAM_IDS.includes(
- program.id,
- )
- );
- }
- return true;
- })
- .map(({ id, label }) => ({
- text: label,
- value: id,
- }))}
+ items={attribute.options.flatMap(
+ ({ id, label, description, icon }) => {
+ const { hidden, missingIntegration } =
+ isCustomerSourceCondition
+ ? getCustomerSourceAvailability({
+ source: id,
+ programId: program?.id,
+ installedIntegrationIds:
+ integrations?.map(({ id }) => id),
+ })
+ : {
+ hidden: false,
+ missingIntegration: undefined,
+ };
+
+ if (hidden) return [];
+
+ return {
+ text: label,
+ value: id,
+ description,
+ disabledTooltip: missingIntegration && (
+ <TooltipContent
+ title={`This option requires the ${missingIntegration.name} integration.`}
+ cta={`Install ${missingIntegration.name} integration`}
+ href={`/${workspaceSlug}/settings/integrations/${missingIntegration.slug}`}
+ target="_blank"
+ />
+ ),
+ icon: icon ? (
+ <img
+ src={icon}
+ alt=""
+ className={cn(
+ "size-4 shrink-0 rounded-full",
+ description && "mt-0.5",
+ )}
+ />
+ ) : undefined,
+ };
+ },
+ )}
onSelect={(value) => {
setValue(conditionKey, {
...condition,
diff --git a/apps/web/ui/shared/inline-badge-popover.tsx b/apps/web/ui/shared/inline-badge-popover.tsx
index 0633732ea9..a77135231e 100644
--- a/apps/web/ui/shared/inline-badge-popover.tsx
+++ b/apps/web/ui/shared/inline-badge-popover.tsx
@@ -1,9 +1,11 @@
import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
import { X } from "@/ui/shared/icons";
+import { MarkdownDescription } from "@/ui/shared/markdown-description";
import {
AnimatedSizeContainer,
Button,
Check2,
+ DynamicTooltipWrapper,
MarkdownIcon,
Plus,
Popover,
@@ -106,6 +108,7 @@ export type InlineBadgePopoverMenuItem<T> = {
onSelect?: () => void;
preventClose?: boolean;
disabled?: boolean;
+ disabledTooltip?: ReactNode;
};
export function InlineBadgePopoverMenu<T extends any>({
@@ -113,11 +116,13 @@ export function InlineBadgePopoverMenu<T extends any>({
onSelect,
selectedValue,
search,
+ className,
}: {
items: InlineBadgePopoverMenuItem<T>[];
onSelect?: (value: T) => void;
selectedValue?: T | T[];
search?: boolean;
+ className?: string;
}) {
const { setIsOpen, isOpen } = useContext(InlineBadgePopoverContext);
@@ -183,6 +188,7 @@ export function InlineBadgePopoverMenu<T extends any>({
className={cn(
"scrollbar-hide flex max-h-64 flex-col gap-1 overflow-y-auto transition-all",
hasDescriptions ? "max-w-72" : "max-w-52",
+ className,
)}
ref={scrollRef}
onScroll={updateScrollProgress}
@@ -208,57 +214,75 @@ export function InlineBadgePopoverMenu<T extends any>({
onSelect: itemOnSelect,
preventClose,
disabled,
+ disabledTooltip,
}) => (
- <Command.Item
+ <DynamicTooltipWrapper
key={String(value)}
- value={`${text} ${description ?? ""} ${value}`}
- disabled={disabled}
- onSelect={() => {
- if (disabled) return;
- itemOnSelect?.();
- onSelect?.(value);
- !isMultiSelect && !preventClose && setIsOpen(false);
- }}
- className={cn(
- "flex cursor-pointer justify-between rounded-md px-1.5 py-1 transition-colors duration-150 data-[selected=true]:bg-neutral-100",
- description ? "items-start gap-2 py-1.5" : "items-center",
- disabled &&
- "cursor-not-allowed opacity-50 data-[selected=true]:bg-transparent",
- )}
+ tooltipProps={
+ disabledTooltip
+ ? {
+ content: disabledTooltip,
+ side: "left",
+ delayDuration: 0,
+ }
+ : undefined
+ }
>
- <div
- className={cn(
- "flex min-w-0 gap-2",
- description ? "items-start" : "items-center",
- )}
- >
- {icon}
- {description ? (
- <div className="flex min-w-0 flex-col gap-0.5 pr-2">
- <span className="text-content-default text-left text-sm font-medium">
- {text}
- </span>
- <span className="text-content-subtle text-left text-xs font-normal leading-snug">
- {description}
- </span>
- </div>
- ) : (
- <span className="text-content-default pr-3 text-left text-sm font-medium">
- {text}
- </span>
- )}
- </div>
- {(Array.isArray(selectedValue)
- ? selectedValue.includes(value)
- : selectedValue === value) && (
- <Check2
+ {/* cmdk overrides onPointerMove/onClick on Command.Item, so the tooltip trigger needs its own element */}
+ <div>
+ <Command.Item
+ value={`${text} ${description ?? ""} ${value}`}
+ disabled={disabled || !!disabledTooltip}
+ onSelect={() => {
+ if (disabled || !!disabledTooltip) return;
+ itemOnSelect?.();
+ onSelect?.(value);
+ !isMultiSelect && !preventClose && setIsOpen(false);
+ }}
className={cn(
- "text-content-emphasis size-3.5 shrink-0",
- description && "mt-0.5",
+ "flex cursor-pointer justify-between rounded-md px-1.5 py-1 transition-colors duration-150 data-[selected=true]:bg-neutral-100",
+ description
+ ? "items-start gap-2 py-1.5"
+ : "items-center",
+ (disabled || !!disabledTooltip) &&
+ "cursor-not-allowed opacity-50 data-[selected=true]:bg-transparent",
+ )}
+ >
+ <div
+ className={cn(
+ "flex min-w-0 gap-2",
+ description ? "items-start" : "items-center",
+ )}
+ >
+ {icon}
+ {description ? (
+ <div className="flex min-w-0 flex-col gap-0.5 pr-2">
+ <span className="text-content-default text-left text-sm font-medium">
+ {text}
+ </span>
+ <MarkdownDescription className="text-content-subtle text-left text-xs font-normal leading-snug">
+ {description}
+ </MarkdownDescription>
+ </div>
+ ) : (
+ <span className="text-content-default pr-3 text-left text-sm font-medium">
+ {text}
+ </span>
+ )}
+ </div>
+ {(Array.isArray(selectedValue)
+ ? selectedValue.includes(value)
+ : selectedValue === value) && (
+ <Check2
+ className={cn(
+ "text-content-emphasis size-3.5 shrink-0",
+ description && "mt-0.5",
+ )}
+ />
)}
- />
- )}
- </Command.Item>
+ </Command.Item>
+ </div>
+ </DynamicTooltipWrapper>
),
)}
</Command.List>
diff --git a/apps/web/ui/shared/markdown-description.tsx b/apps/web/ui/shared/markdown-description.tsx
index 6691d30709..50932f83b2 100644
--- a/apps/web/ui/shared/markdown-description.tsx
+++ b/apps/web/ui/shared/markdown-description.tsx
@@ -19,7 +19,12 @@ export const MarkdownDescription = ({
a: ({ children, href }) => {
if (!href) return null;
return (
- <a href={href} target="_blank">
+ <a
+ href={href}
+ target="_blank"
+ rel="noopener noreferrer"
+ onClick={(e) => e.stopPropagation()}
+ >
{children}
</a>
);