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.

address coderabbit feedback

Steven Tey6d6dc19be4fc
diff --git a/apps/web/lib/zod/schemas/misc.ts b/apps/web/lib/zod/schemas/misc.ts
index 6dabb7a47a..a9f88e3e7b 100644
--- a/apps/web/lib/zod/schemas/misc.ts
+++ b/apps/web/lib/zod/schemas/misc.ts
@@ -3,12 +3,15 @@ import { WorkspaceRole } from "@dub/prisma/client";
import * as z from "zod/v4";
export const RECURRING_MAX_DURATIONS = [0, 1, 3, 6, 12, 24];
+export const MAX_DURATION_LIMIT = 600;
export const maxDurationSchema = z.coerce
.number()
.int({ message: "Max duration must be an integer." })
.nonnegative({ message: "Max duration must be 0 or greater." })
- .max(600, { message: "Max duration must be 600 months (50 years) or less." })
+ .max(MAX_DURATION_LIMIT, {
+ message: "Max duration must be 600 months (50 years) or less.",
+ })
.nullish();
export const planSchema = z.enum(plans).describe("The plan of the workspace.");
diff --git a/apps/web/ui/partners/rewards/partner-referral-reward-builder.tsx b/apps/web/ui/partners/rewards/partner-referral-reward-builder.tsx
index 5ae621fcdb..e35db86540 100644
--- a/apps/web/ui/partners/rewards/partner-referral-reward-builder.tsx
+++ b/apps/web/ui/partners/rewards/partner-referral-reward-builder.tsx
@@ -18,6 +18,7 @@ import {
InlineBadgePopoverContext,
InlineBadgePopoverMenu,
} from "../../shared/inline-badge-popover";
+import { DurationPopoverContent } from "../../shared/duration-popover-content";
import { useAddEditRewardForm } from "./add-edit-reward-sheet";
type Trigger = (typeof PARTNER_REFERRAL_TRIGGER)[number];
@@ -34,21 +35,6 @@ const FLAT_TRIGGER_ITEMS = PARTNER_REFERRAL_FLAT_TRIGGERS.map((trigger) => ({
value: trigger,
}));
-const MAX_DURATION_MENU_ITEMS = [
- {
- text: "one time",
- value: "0",
- },
- ...RECURRING_MAX_DURATIONS.filter((v) => v !== 0 && v !== 1).map((v) => ({
- text: `for ${v} ${pluralize("month", Number(v))}`,
- value: v.toString(),
- })),
- {
- text: "for the customer's lifetime",
- value: "Infinity",
- },
-];
-
export function PartnerReferralRewardBuilder() {
const { watch, setValue } = useAddEditRewardForm();
@@ -152,6 +138,9 @@ export function PartnerReferralRewardBuilder() {
const maxDurationText = useMemo(() => {
if (maxDuration === 0) return "one time";
if (maxDuration === Infinity) return "for the customer's lifetime";
+ if (maxDuration == null || Number.isNaN(Number(maxDuration))) {
+ return "for the customer's lifetime";
+ }
return `for ${maxDuration} ${pluralize("month", Number(maxDuration))}`;
}, [maxDuration]);
@@ -194,14 +183,16 @@ export function PartnerReferralRewardBuilder() {
/>
</InlineBadgePopover>{" "}
<InlineBadgePopover text={maxDurationText}>
- <InlineBadgePopoverMenu
- selectedValue={maxDuration?.toString()}
- onSelect={(value) =>
- setValue("maxDuration", Number(value), {
+ <DurationPopoverContent
+ value={maxDuration ?? Infinity}
+ onChange={(value) =>
+ setValue("maxDuration", value, {
shouldDirty: true,
})
}
- items={MAX_DURATION_MENU_ITEMS}
+ presetDurations={RECURRING_MAX_DURATIONS.filter(
+ (v) => v !== 0 && v !== 1, // filter out one-time and 1-month intervals (we only use 1-month for discounts)
+ )}
/>
</InlineBadgePopover>
</>
diff --git a/apps/web/ui/shared/duration-popover-content.tsx b/apps/web/ui/shared/duration-popover-content.tsx
index 11d1fbe047..0dfe35340c 100644
--- a/apps/web/ui/shared/duration-popover-content.tsx
+++ b/apps/web/ui/shared/duration-popover-content.tsx
@@ -1,5 +1,6 @@
"use client";
+import { MAX_DURATION_LIMIT } from "@/lib/zod/schemas/misc";
import { pluralize } from "@dub/utils";
import { ChangeEvent, useContext, useEffect, useState } from "react";
import {
@@ -12,8 +13,7 @@ export function DurationPopoverContent({
onChange,
presetDurations,
}: DurationPopoverContentProps) {
- const { setIsOpen } = useContext(InlineBadgePopoverContext);
- const customInputMax = 600;
+ const { isOpen, setIsOpen } = useContext(InlineBadgePopoverContext);
const [customDurationInput, setCustomDurationInput] = useState<string>(() => {
if (
value !== null &&
@@ -29,6 +29,8 @@ export function DurationPopoverContent({
const [showCustomInput, setShowCustomInput] = useState(false);
useEffect(() => {
+ if (showCustomInput) return;
+
if (
value === null ||
value === undefined ||
@@ -37,12 +39,17 @@ export function DurationPopoverContent({
presetDurations.includes(Number(value))
) {
setCustomDurationInput("");
- setShowCustomInput(false);
return;
}
setCustomDurationInput(value.toString());
- }, [value, presetDurations]);
+ }, [value, presetDurations, showCustomInput]);
+
+ useEffect(() => {
+ if (!isOpen) return;
+ if (customDurationInput === "") return;
+ setShowCustomInput(true);
+ }, [isOpen, customDurationInput]);
const isPresetValue =
value === Infinity ||
@@ -63,7 +70,7 @@ export function DurationPopoverContent({
<input
type="number"
min="0"
- max={customInputMax.toString()}
+ max={MAX_DURATION_LIMIT.toString()}
step="1"
autoFocus
placeholder="e.g. 24"
@@ -72,7 +79,7 @@ export function DurationPopoverContent({
onChange={(e: ChangeEvent<HTMLInputElement>) => {
const raw = e.target.value;
const parsed = parseInt(raw, 10);
- const clamped = Math.min(parsed, customInputMax);
+ const clamped = Math.max(0, Math.min(parsed, MAX_DURATION_LIMIT));
const display = isNaN(parsed) ? raw : clamped.toString();
setCustomDurationInput(display);
if (!isNaN(parsed) && parsed >= 0) {
diff --git a/packages/prisma/schema/discount.prisma b/packages/prisma/schema/discount.prisma
index 07deddad5b..7704fc60c6 100644
--- a/packages/prisma/schema/discount.prisma
+++ b/packages/prisma/schema/discount.prisma
@@ -8,7 +8,7 @@ model Discount {
programId String
amount Int @default(0)
type RewardStructure @default(percentage)
- maxDuration Int? // in months (0 -> one-time purchase, 1, 3, 6, 12, 18, 24, 36, 48 -> months, null -> lifetime)
+ maxDuration Int? // in months (0 -> one-time purchase, 1, 3, 6, 12, 24 -> months, null -> lifetime)
description String?
couponId String?
couponTestId String?