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.

payout matches base

Pedro Ladeirafce185c79950
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index 8e9df62891..6bc1da08c3 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -3,7 +3,10 @@
import { constructRewardAmount } from "@/lib/api/sales/construct-reward-amount";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { getCustomerSourceAvailability } from "@/lib/rewards/get-customer-source-availability";
-import { suggestionTouchesField } from "@/lib/rewards/validate-tooltip-suggestion";
+import {
+ isRewardConditionComplete,
+ suggestionTouchesField,
+} from "@/lib/rewards/validate-tooltip-suggestion";
import useIntegrations from "@/lib/swr/use-integrations";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
@@ -66,6 +69,7 @@ import { useAddEditRewardForm } from "./add-edit-reward-sheet";
import { RewardIconSquare } from "./reward-icon-square";
import {
SuggestedFixBadge,
+ SuggestedFixNoticeBadge,
SuggestedFixPopoverHost,
} from "./suggested-fix-popover";
import { useRewardTooltipConsistencyContext } from "./use-reward-tooltip-consistency";
@@ -1067,6 +1071,58 @@ function OperatorDropdown({ modifierIndex }: { modifierIndex: number }) {
);
}
+function payoutMatchesBase({
+ event,
+ type,
+ amountInCents,
+ amountInPercentage,
+ maxDuration,
+ parentType,
+ parentAmountInCents,
+ parentAmountInPercentage,
+ parentMaxDuration,
+}: {
+ event?: string | null;
+ type?: string | null;
+ amountInCents?: number | null;
+ amountInPercentage?: number | null;
+ maxDuration?: number | null;
+ parentType?: string | null;
+ parentAmountInCents?: number | null;
+ parentAmountInPercentage?: number | null;
+ parentMaxDuration?: number | null;
+}) {
+ const displayType =
+ type === "flat" || type === "percentage" ? type : parentType;
+
+ if (displayType !== "flat" && displayType !== "percentage") return false;
+ if (displayType !== parentType) return false;
+
+ const amount =
+ displayType === "percentage" ? amountInPercentage : amountInCents;
+ const parentAmount =
+ parentType === "percentage"
+ ? parentAmountInPercentage
+ : parentAmountInCents;
+
+ if (typeof amount !== "number" || !Number.isFinite(amount)) return false;
+ if (typeof parentAmount !== "number" || !Number.isFinite(parentAmount)) {
+ return false;
+ }
+ if (amount !== parentAmount) return false;
+ if (event !== "sale") return true;
+
+ const duration = maxDuration !== undefined ? maxDuration : parentMaxDuration;
+ const currentDuration =
+ duration == null || !Number.isFinite(duration) ? null : duration;
+ const baseDuration =
+ parentMaxDuration == null || !Number.isFinite(parentMaxDuration)
+ ? null
+ : parentMaxDuration;
+
+ return currentDuration === baseDuration;
+}
+
function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
const modifierKey = `modifiers.${modifierIndex}` as const;
@@ -1078,7 +1134,10 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
maxDuration,
event,
parentType,
+ parentAmountInCents,
+ parentAmountInPercentage,
parentMaxDuration,
+ conditions,
] = useWatch({
control,
name: [
@@ -1088,9 +1147,15 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
`${modifierKey}.maxDuration`,
"event",
"type",
+ "amountInCents",
+ "amountInPercentage",
"maxDuration",
+ `${modifierKey}.conditions`,
],
});
+ const [dismissedRedundantPayout, setDismissedRedundantPayout] =
+ useState(false);
+ const [flagPayout, setFlagPayout] = useState(false);
// Use parent values as fallbacks if modifier doesn't have type or maxDuration
const displayType = type || parentType;
@@ -1098,6 +1163,66 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
maxDuration !== undefined ? maxDuration : parentMaxDuration;
const amount = displayType === "flat" ? amountInCents : amountInPercentage;
+ const redundantPayout = payoutMatchesBase({
+ event,
+ type,
+ amountInCents,
+ amountInPercentage,
+ maxDuration,
+ parentType,
+ parentAmountInCents,
+ parentAmountInPercentage,
+ parentMaxDuration,
+ });
+ const conditionsComplete =
+ !!event &&
+ !!conditions?.length &&
+ conditions.every((condition) =>
+ isRewardConditionComplete({ event, condition }),
+ );
+ const flagKey =
+ redundantPayout && conditionsComplete && !dismissedRedundantPayout
+ ? JSON.stringify({
+ amountInCents,
+ amountInPercentage,
+ type,
+ maxDuration,
+ parentAmountInCents,
+ parentAmountInPercentage,
+ parentType,
+ parentMaxDuration,
+ conditions,
+ })
+ : null;
+
+ useEffect(() => {
+ if (!redundantPayout) setDismissedRedundantPayout(false);
+ }, [redundantPayout]);
+
+ useEffect(() => {
+ setFlagPayout(false);
+
+ if (!flagKey) return;
+
+ const timeout = window.setTimeout(() => setFlagPayout(true), 5000);
+
+ return () => window.clearTimeout(timeout);
+ }, [flagKey]);
+ const amountLabel =
+ amount != null && !isNaN(amount)
+ ? constructRewardAmount({
+ type: displayType,
+ amountInCents: displayType === "flat" ? amount * 100 : undefined,
+ amountInPercentage: displayType === "percentage" ? amount : undefined,
+ maxDuration: displayMaxDuration,
+ })
+ : "amount";
+ const durationLabel =
+ displayMaxDuration === 0
+ ? "one time"
+ : displayMaxDuration === Infinity
+ ? "for the customer's lifetime"
+ : `for ${displayMaxDuration} ${pluralize("month", Number(displayMaxDuration))}`;
return (
<span className="leading-relaxed">
@@ -1119,36 +1244,25 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
{displayType === "percentage" && "of "}
</>
)}
- <InlineBadgePopover
- text={
- amount != null && !isNaN(amount)
- ? constructRewardAmount({
- type: displayType,
- amountInCents:
- displayType === "flat" ? amount * 100 : undefined,
- amountInPercentage:
- displayType === "percentage" ? amount : undefined,
- maxDuration: displayMaxDuration,
- })
- : "amount"
- }
- invalid={amount == null || isNaN(amount)}
- >
- <ResultAmountInput modifierKey={modifierKey} />
- </InlineBadgePopover>{" "}
+ {flagPayout ? (
+ <SuggestedFixNoticeBadge
+ text={amountLabel}
+ message="This pays the same as the default reward, so it doesn't change what partners earn."
+ onDiscard={() => setDismissedRedundantPayout(true)}
+ />
+ ) : (
+ <InlineBadgePopover
+ text={amountLabel}
+ invalid={amount == null || isNaN(amount)}
+ >
+ <ResultAmountInput modifierKey={modifierKey} />
+ </InlineBadgePopover>
+ )}{" "}
per {event}
{event === "sale" && (
<>
{" "}
- <InlineBadgePopover
- text={
- displayMaxDuration === 0
- ? "one time"
- : displayMaxDuration === Infinity
- ? "for the customer's lifetime"
- : `for ${displayMaxDuration} ${pluralize("month", Number(displayMaxDuration))}`
- }
- >
+ <InlineBadgePopover text={durationLabel}>
<DurationPopoverContent
value={displayMaxDuration ?? undefined}
onChange={(value) =>
diff --git a/apps/web/ui/partners/rewards/suggested-fix-popover.tsx b/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
index 59a1dd0315..4bd80c3e06 100644
--- a/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
+++ b/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
@@ -284,6 +284,115 @@ export function ReviewingSuggestedFixBadge({ text }: { text: string }) {
);
}
+export function SuggestedFixNoticeBadge({
+ text,
+ message,
+ onDiscard,
+}: {
+ text: string;
+ message: string;
+ onDiscard: () => void;
+}) {
+ const { isMobile } = useMediaQuery();
+ const closeTimerRef = useRef<number | undefined>(undefined);
+ const contentRef = useRef<HTMLDivElement>(null);
+ const [open, setOpen] = useState(false);
+
+ const cancelHide = () => {
+ if (closeTimerRef.current) {
+ window.clearTimeout(closeTimerRef.current);
+ closeTimerRef.current = undefined;
+ }
+ };
+
+ const scheduleHide = () => {
+ cancelHide();
+ closeTimerRef.current = window.setTimeout(() => {
+ if (contentRef.current?.contains(document.activeElement)) return;
+ setOpen(false);
+ }, 300);
+ };
+
+ return (
+ <Popover
+ openPopover={open}
+ setOpenPopover={setOpen}
+ align="start"
+ sideOffset={8}
+ onOpenAutoFocus={(event) => event.preventDefault()}
+ popoverContentClassName={POPOVER_CONTENT_CLASS_NAME}
+ content={
+ <div
+ ref={contentRef}
+ onMouseEnter={cancelHide}
+ onMouseLeave={scheduleHide}
+ >
+ <div className="flex items-center gap-1.5 border-b border-neutral-200 p-3 text-sm font-medium text-neutral-900">
+ <Sparkle3 variant="fill" className="size-3.5" />
+ Suggested fix
+ </div>
+ <div className="p-3">
+ <p className="text-sm leading-relaxed text-neutral-600">
+ {message}
+ </p>
+ </div>
+ <div className="flex items-center justify-end gap-2 border-t border-neutral-200 px-3 py-2">
+ <Button
+ type="button"
+ variant="secondary"
+ text="Discard"
+ className="h-7 w-fit rounded-lg px-3 py-2"
+ onClick={() => {
+ onDiscard();
+ setOpen(false);
+ }}
+ />
+ </div>
+ </div>
+ }
+ >
+ <button
+ type="button"
+ onMouseEnter={() => {
+ if (isMobile) return;
+ cancelHide();
+ setOpen(true);
+ }}
+ onMouseLeave={() => {
+ if (!isMobile) scheduleHide();
+ }}
+ onClick={(event) => {
+ const fromKeyboard = event.detail === 0;
+
+ if (!isMobile && !fromKeyboard) return;
+
+ if (open && !fromKeyboard) {
+ setOpen(false);
+ return;
+ }
+
+ setOpen(true);
+ if (fromKeyboard) {
+ window.requestAnimationFrame(() => {
+ window.requestAnimationFrame(() => {
+ contentRef.current
+ ?.querySelector<HTMLElement>("button")
+ ?.focus();
+ });
+ });
+ }
+ }}
+ aria-haspopup="dialog"
+ aria-expanded={open}
+ className={SUGGESTED_FIX_BADGE_CLASS_NAME}
+ >
+ <Sparkle3 variant="fill" className="size-3 shrink-0 text-[#E17100]" />
+ <span className="min-w-0 truncate">{text}</span>
+ </button>
+ </Popover>
+ );
+}
+
function PayoutFixPreview({ fixes }: { fixes: PayoutFix[] }) {
const { control } = useAddEditRewardForm();
const [