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.

CPA Indicator (#3828)

Marcus Farrell29f4385a8aac
diff --git a/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/rewards/form.tsx b/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/rewards/form.tsx
index 57a5d3837a..896cfd8f0d 100644
--- a/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/rewards/form.tsx
+++ b/apps/web/app/(ee)/app.dub.co/(new-program)/[slug]/program/new/rewards/form.tsx
@@ -6,11 +6,13 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { ProgramData } from "@/lib/types";
import { RECURRING_MAX_DURATIONS } from "@/lib/zod/schemas/misc";
import { COMMISSION_TYPES } from "@/lib/zod/schemas/rewards";
+import { RewardQualityFieldIndicator } from "@/ui/partners/rewards/reward-quality";
+import type { EventType } from "@dub/prisma/client";
import { Button, CircleCheckFill } from "@dub/ui";
import { cn } from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
import { useRouter } from "next/navigation";
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { useFormContext } from "react-hook-form";
import { toast } from "sonner";
@@ -62,20 +64,37 @@ export function Form() {
formState: { isSubmitting },
} = useFormContext<ProgramData>();
- const [type, defaultRewardType, maxDuration] = watch([
+ const [
+ type,
+ defaultRewardType,
+ maxDuration,
+ amountInCents,
+ amountInPercentage,
+ ] = watch([
"type",
"defaultRewardType",
"maxDuration",
+ "amountInCents",
+ "amountInPercentage",
]);
const [commissionStructure, setCommissionStructure] = useState<
"one-off" | "recurring"
>("recurring");
+ const lastSaleMaxDurationRef = useRef<number | null | undefined>(
+ defaultRewardType === "sale" ? maxDuration : null,
+ );
useEffect(() => {
setCommissionStructure(maxDuration === 0 ? "one-off" : "recurring");
}, [maxDuration]);
+ useEffect(() => {
+ if (defaultRewardType === "sale") {
+ lastSaleMaxDurationRef.current = maxDuration;
+ }
+ }, [defaultRewardType, maxDuration]);
+
const { executeAsync, isPending } = useAction(onboardProgramAction, {
onSuccess: () => {
router.push(`/${workspaceSlug}/program/new/partners`);
@@ -158,9 +177,18 @@ export function Form() {
setValue("type", "percentage", {
shouldDirty: true,
});
+ setValue(
+ "maxDuration",
+ lastSaleMaxDurationRef.current ?? null,
+ {
+ shouldDirty: true,
+ shouldValidate: true,
+ },
+ );
}
if (key === "lead") {
+ lastSaleMaxDurationRef.current = maxDuration;
setValue("type", "flat", { shouldDirty: true });
setValue("maxDuration", 0, { shouldDirty: true });
}
@@ -372,10 +400,25 @@ export function Form() {
)}
<div>
- <label className="text-sm font-medium text-neutral-800">
- {type === "percentage" ? "Percentage" : "Amount"} per{" "}
- {defaultRewardType}
- </label>
+ <div className="flex items-center justify-between gap-3">
+ <label className="text-sm font-medium text-neutral-800">
+ {type === "percentage" ? "Percentage" : "Amount"} per{" "}
+ {defaultRewardType}
+ </label>
+ <RewardQualityFieldIndicator
+ event={defaultRewardType as EventType}
+ type={type}
+ amountInCents={
+ type === "flat" && amountInCents != null
+ ? amountInCents * 100
+ : null
+ }
+ amountInPercentage={
+ type === "percentage" ? amountInPercentage : null
+ }
+ maxDuration={maxDuration}
+ />
+ </div>
<div className="relative mt-2 rounded-md shadow-sm">
<span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
{type === "flat" && "$"}
diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/program/reward/form.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/program/reward/form.tsx
index fb21f94e5c..e46357c975 100644
--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/program/reward/form.tsx
+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/program/reward/form.tsx
@@ -6,11 +6,13 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { ProgramData } from "@/lib/types";
import { RECURRING_MAX_DURATIONS } from "@/lib/zod/schemas/misc";
import { COMMISSION_TYPES } from "@/lib/zod/schemas/rewards";
+import { RewardQualityFieldIndicator } from "@/ui/partners/rewards/reward-quality";
+import type { EventType } from "@dub/prisma/client";
import { AnimatedSizeContainer, Button, CircleCheckFill } from "@dub/ui";
import { capitalize, cn } from "@dub/utils";
import { usePlausible } from "next-plausible";
import { useAction } from "next-safe-action/hooks";
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { useFormContext } from "react-hook-form";
import { toast } from "sonner";
import { useOnboardingProgress } from "../../../use-onboarding-progress";
@@ -59,21 +61,38 @@ export function Form() {
formState: { isSubmitting },
} = useFormContext<ProgramData>();
- const [type, defaultRewardType, maxDuration] = watch([
+ const [
+ type,
+ defaultRewardType,
+ maxDuration,
+ amountInCents,
+ amountInPercentage,
+ ] = watch([
"type",
"defaultRewardType",
"maxDuration",
+ "amountInCents",
+ "amountInPercentage",
]);
const [commissionStructure, setCommissionStructure] = useState<
"one-off" | "recurring"
>("recurring");
+ const lastSaleMaxDurationRef = useRef<number | null | undefined>(
+ defaultRewardType === "sale" ? maxDuration : null,
+ );
useEffect(
() => setCommissionStructure(maxDuration === 0 ? "one-off" : "recurring"),
[maxDuration],
);
+ useEffect(() => {
+ if (defaultRewardType === "sale") {
+ lastSaleMaxDurationRef.current = maxDuration;
+ }
+ }, [defaultRewardType, maxDuration]);
+
const plausible = usePlausible();
const { executeAsync, isPending } = useAction(onboardProgramAction, {
onSuccess: () => {
@@ -161,9 +180,18 @@ export function Form() {
setValue("type", "percentage", {
shouldDirty: true,
});
+ setValue(
+ "maxDuration",
+ lastSaleMaxDurationRef.current ?? null,
+ {
+ shouldDirty: true,
+ shouldValidate: true,
+ },
+ );
}
if (key === "lead") {
+ lastSaleMaxDurationRef.current = maxDuration;
setValue("type", "flat", { shouldDirty: true });
setValue("maxDuration", 0, { shouldDirty: true });
}
@@ -377,10 +405,25 @@ export function Form() {
)}
<label className="space-y-2">
- <span className="text-content-emphasis block text-sm font-semibold">
- {type === "percentage" ? "Percentage" : "Amount"} per{" "}
- {defaultRewardType}
- </span>
+ <div className="flex items-center justify-between gap-3">
+ <span className="text-content-emphasis block text-sm font-semibold">
+ {type === "percentage" ? "Percentage" : "Amount"} per{" "}
+ {defaultRewardType}
+ </span>
+ <RewardQualityFieldIndicator
+ event={defaultRewardType as EventType}
+ type={type}
+ amountInCents={
+ type === "flat" && amountInCents != null
+ ? amountInCents * 100
+ : null
+ }
+ amountInPercentage={
+ type === "percentage" ? amountInPercentage : null
+ }
+ maxDuration={maxDuration}
+ />
+ </div>
<div className="relative rounded-md shadow-sm">
<span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
{type === "flat" && "$"}
diff --git a/apps/web/ui/partners/rewards/reward-preview-card.tsx b/apps/web/ui/partners/rewards/reward-preview-card.tsx
index 5d97408602..63af696d59 100644
--- a/apps/web/ui/partners/rewards/reward-preview-card.tsx
+++ b/apps/web/ui/partners/rewards/reward-preview-card.tsx
@@ -11,6 +11,7 @@ import {
getRewardPayload,
useAddEditRewardForm,
} from "./add-edit-reward-sheet";
+import { getRewardQuality, RewardQualityIndicator } from "./reward-quality";
export function RewardPreviewCard() {
const { control } = useAddEditRewardForm();
@@ -25,13 +26,44 @@ export function RewardPreviewCard() {
}
const Icon = REWARD_EVENTS[reward.event].icon;
+ const type = data.type ?? reward.type;
+ const hasConditions = !!data.modifiers?.length;
+ const quality = getRewardQuality({
+ event: reward.event,
+ type,
+ amountInCents:
+ type === "flat" &&
+ data.amountInCents != null &&
+ !Number.isNaN(data.amountInCents)
+ ? data.amountInCents * 100
+ : null,
+ amountInPercentage:
+ type === "percentage" &&
+ data.amountInPercentage != null &&
+ !Number.isNaN(data.amountInPercentage)
+ ? data.amountInPercentage
+ : null,
+ maxDuration: data.maxDuration,
+ });
return (
<div className="border-border-subtle bg-bg-muted rounded-xl border shadow-sm">
- <div className="px-4 py-2.5">
+ <div className="flex items-center justify-between gap-3 px-4 py-2.5">
<span className="text-content-emphasis flex items-center gap-2.5 text-sm font-semibold">
Reward preview
</span>
+ <div className="flex items-center gap-2">
+ <span className="text-xs font-semibold text-neutral-500">
+ {hasConditions ? "Base reward quality" : "Reward quality"}
+ </span>
+ <RewardQualityIndicator
+ event={reward.event}
+ quality={quality}
+ tooltipFooter={
+ hasConditions ? "Based on default reward only" : undefined
+ }
+ />
+ </div>
</div>
<div className="border-border-subtle bg-bg-default -mx-px rounded-xl border-x border-t p-4">
diff --git a/apps/web/ui/partners/rewards/reward-quality.tsx b/apps/web/ui/partners/rewards/reward-quality.tsx
new file mode 100644
index 0000000000..8e234a1222
--- /dev/null
+++ b/apps/web/ui/partners/rewards/reward-quality.tsx
@@ -0,0 +1,369 @@
+"use client";
+
+import type { EventType, RewardStructure } from "@dub/prisma/client";
+import { Tooltip } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { AnimatePresence, motion } from "motion/react";
+
+export type RewardQuality = "low" | "good" | "strong" | "limited";
+
+export const getRewardQuality = ({
+ event,
+ type,
+ amountInCents,
+ amountInPercentage,
+ maxDuration,
+}: {
+ event: EventType;
+ type: RewardStructure | null | undefined;
+ amountInCents?: number | null;
+ amountInPercentage?: number | null;
+ maxDuration?: number | null;
+}): RewardQuality | null => {
+ if (event === "click" || event === "lead") {
+ if (amountInCents == null || Number.isNaN(amountInCents)) {
+ return null;
+ }
+
+ const amount = amountInCents / 100;
+
+ if (event === "click") {
+ if (amount < 0.03) return "low";
+ if (amount <= 0.05) return "good";
+ return "strong";
+ }
+
+ if (amount < 1) return "low";
+ if (amount <= 10) return "good";
+ return "strong";
+ }
+
+ if (event !== "sale" || !type) {
+ return null;
+ }
+
+ const isOneOff = maxDuration === 0;
+ const isLifetime = maxDuration == null || maxDuration === Infinity;
+ const months = isLifetime ? Infinity : maxDuration;
+
+ if (type === "percentage") {
+ if (amountInPercentage == null || Number.isNaN(amountInPercentage)) {
+ return null;
+ }
+
+ if (isOneOff) {
+ return "limited";
+ }
+
+ if (amountInPercentage < 10 || (months ?? 0) < 6) {
+ return "low";
+ }
+
+ if (
+ amountInPercentage > 20 &&
+ (isLifetime || (months != null && months >= 12))
+ ) {
+ return "strong";
+ }
+
+ return "good";
+ }
+
+ if (amountInCents == null || Number.isNaN(amountInCents)) {
+ return null;
+ }
+
+ if (isOneOff) {
+ if (amountInCents >= 100_00) {
+ return "strong";
+ }
+
+ if (amountInCents >= 50_00) {
+ return "good";
+ }
+
+ return "limited";
+ }
+
+ const totalCents = isLifetime ? Infinity : amountInCents * (months ?? 0);
+
+ if (totalCents < 50_00 || (months ?? 0) < 6) {
+ return "low";
+ }
+
+ if (totalCents > 100_00 && (isLifetime || (months != null && months >= 12))) {
+ return "strong";
+ }
+
+ return "good";
+};
+
+const QUALITY_STYLES: Record<
+ RewardQuality,
+ {
+ label: string;
+ bgClassName: string;
+ textClassName: string;
+ iconClassName: string;
+ activeBars: number;
+ }
+> = {
+ low: {
+ label: "Low",
+ bgClassName: "bg-orange-100",
+ textClassName: "text-orange-700",
+ iconClassName: "bg-orange-500",
+ activeBars: 1,
+ },
+ good: {
+ label: "Good",
+ bgClassName: "bg-cyan-200",
+ textClassName: "text-cyan-700",
+ iconClassName: "bg-cyan-500",
+ activeBars: 2,
+ },
+ strong: {
+ label: "Strong",
+ bgClassName: "bg-green-200",
+ textClassName: "text-green-700",
+ iconClassName: "bg-green-500",
+ activeBars: 3,
+ },
+ limited: {
+ label: "Limited",
+ bgClassName: "bg-orange-100",
+ textClassName: "text-orange-700",
+ iconClassName: "bg-orange-500",
+ activeBars: 1,
+ },
+};
+
+function QualityBars({
+ quality,
+ className,
+}: {
+ quality: RewardQuality | null;
+ className?: string;
+}) {
+ const styles = quality ? QUALITY_STYLES[quality] : null;
+ const isEmpty = !quality;
+
+ return (
+ <span
+ className={cn(
+ "flex h-3 w-3 items-center justify-center gap-0.5",
+ className,
+ )}
+ >
+ {[0, 1, 2].map((index) => (
+ <span
+ key={index}
+ style={{ transitionDelay: `${index * 35}ms` }}
+ className={cn(
+ "h-2 w-0.5 rounded-full transition-[background-color,opacity] duration-300 ease-out",
+ styles ? styles.iconClassName : "bg-neutral-700",
+ styles && index >= styles.activeBars && "opacity-30",
+ !styles && "opacity-30",
+ )}
+ />
+ ))}
+ </span>
+ );
+}
+
+function RewardQualityTooltipContent({
+ event,
+ quality,
+ footer,
+}: {
+ event?: EventType;
+ quality: RewardQuality | null;
+ footer?: string;
+}) {
+ const styles = quality ? QUALITY_STYLES[quality] : null;
+ const content = getRewardQualityTooltipCopy({ event, quality });
+
+ return (
+ <div className="w-[220px] overflow-hidden rounded-xl bg-white text-left">
+ <div
+ className={cn(
+ "m-1 flex h-5 items-center rounded-md p-1 px-2",
+ styles?.bgClassName ?? "bg-neutral-200",
+ )}
+ >
+ <QualityBars quality={quality} />
+ {styles && (
+ <span
+ className={cn("ml-1.5 text-xs font-semibold", styles.textClassName)}
+ >
+ {styles.label}
+ </span>
+ )}
+ </div>
+
+ <div className="px-2.5 pb-2 pt-1 text-xs">
+ <div className="font-semibold text-neutral-900">{content.title}</div>
+ {content.description && (
+ <div className="mt-2 text-neutral-600">{content.description}</div>
+ )}
+ {content.bullets?.length ? (
+ <ul className="mt-2 list-disc pl-4 text-neutral-600">
+ {content.bullets.map((bullet) => (
+ <li key={bullet}>{bullet}</li>
+ ))}
+ </ul>
+ ) : null}
+ </div>
+ {footer && (
+ <div className="mx-1.5 mb-1.5 rounded-md bg-neutral-100 px-3 py-1.5 text-center text-xs font-medium text-neutral-500">
+ {footer}
+ </div>
+ )}
+ </div>
+ );
+}
+
+function getRewardQualityTooltipCopy({
+ event,
+ quality,
+}: {
+ event?: EventType;
+ quality: RewardQuality | null;
+}) {
+ if (!quality) {
+ return {
+ title: "Reward quality",
+ description:
+ event === "click" || event === "lead"
+ ? "Set amount to evaluate this reward"
+ : "Set amount and duration to evaluate this reward",
+ };
+ }
+
+ if (quality === "low") {
+ return {
+ title: "May struggle to attract partners",
+ bullets: ["Below typical ranges", "Limited earning potential"],
+ };
+ }
+
+ if (quality === "limited") {
+ return {
+ title: "May struggle to attract partners",
+ bullets: ["One time reward only", "No long term earning potential"],
+ };
+ }
+
+ if (quality === "good") {
+ return {
+ title: "Solid reward with room to grow",
+ bullets: [
+ "Meets minimum partner expectations",
+ "Opportunity to increase earning potential",
+ ],
+ };
+ }
+
+ return {
+ title: "High earning potential for partners",
+ bullets:
+ event === "sale"
+ ? ["Competitive reward level", "Continued earning after the first sale"]
+ : ["Competitive reward level", "Consistent earning potential"],
+ };
+}
+
+export function RewardQualityIndicator({
+ quality,
+ event,
+ tooltipFooter,
+ showLabel = true,
+ className,
+}: {
+ quality: RewardQuality | null;
+ event?: EventType;
+ tooltipFooter?: string;
+ showLabel?: boolean;
+ className?: string;
+}) {
+ const styles = quality ? QUALITY_STYLES[quality] : null;
+ const isEmpty = !quality;
+
+ return (
+ <Tooltip
+ content={
+ <RewardQualityTooltipContent
+ event={event}
+ footer={tooltipFooter}
+ quality={quality}
+ />
+ }
+ side="top"
+ contentClassName="p-0"
+ >
+ <button
+ type="button"
+ className={cn(
+ "flex h-5 cursor-help items-center justify-center gap-1.5 rounded-md px-1.5 text-xs font-semibold leading-4 outline-none transition-colors",
+ "duration-300 ease-out",
+ isEmpty && "w-5 bg-neutral-200 p-0 text-neutral-400",
+ !isEmpty && styles?.bgClassName,
+ !isEmpty && styles?.textClassName,
+ className,
+ )}
+ aria-label={
+ quality
+ ? `Reward quality: ${styles?.label}`
+ : "Reward quality thresholds"
+ }
+ >
+ <QualityBars quality={quality} />
+ <AnimatePresence mode="popLayout" initial={false}>
+ {showLabel && styles?.label && (
+ <motion.span
+ key={styles.label}
+ initial={{ opacity: 0, y: 2 }}
+ animate={{ opacity: 1, y: 0 }}
+ exit={{ opacity: 0, y: -2 }}
+ transition={{ duration: 0.28, ease: "easeOut" }}
+ className="inline-block"
+ >
+ {styles.label}
+ </motion.span>
+ )}
+ </AnimatePresence>
+ </button>
+ </Tooltip>
+ );
+}
+
+export function RewardQualityFieldIndicator({
+ event,
+ type,
+ amountInCents,
+ amountInPercentage,
+ maxDuration,
+}: {
+ event: EventType;
+ type: RewardStructure | null | undefined;
+ amountInCents?: number | null;
+ amountInPercentage?: number | null;
+ maxDuration?: number | null;
+}) {
+ const quality = getRewardQuality({
+ event,
+ type,
+ amountInCents,
+ amountInPercentage,
+ maxDuration,
+ });
+
+ return (
+ <div className="flex items-center gap-2">
+ <span className="text-xs font-semibold text-neutral-500">
+ Reward quality
+ </span>
+ <RewardQualityIndicator event={event} quality={quality} />
+ </div>
+ );
+}