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.

custom days option to transactional email campaign logic

Pedro Ladeirae94da149aacb
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.tsx
index 37729741c1..af8324e667 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,6 +1,7 @@
import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
import { CAMPAIGN_WORKFLOW_ATTRIBUTE_CONFIG } from "@/lib/zod/schemas/campaigns";
import { WORKFLOW_ATTRIBUTES } from "@/lib/zod/schemas/workflows";
+import { DurationPopoverContent } from "@/ui/shared/duration-popover-content";
import {
InlineBadgePopover,
InlineBadgePopoverContext,
@@ -108,17 +109,13 @@ function DropdownValueInput({
}
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={(config.dropdownValues || []).map((val) => ({
- text: String(val),
- value: String(val),
- }))}
+ <DurationPopoverContent
+ value={field.value ?? undefined}
+ onChange={field.onChange}
+ presetDurations={config.dropdownValues || []}
+ presetsOnly
+ unit="days"
+ minValue={1}
/>
</InlineBadgePopover>
{pluralize("day", field.value || 1)}
diff --git a/apps/web/ui/shared/duration-popover-content.tsx b/apps/web/ui/shared/duration-popover-content.tsx
index 6ccfca98b0..ecd758c0d5 100644
--- a/apps/web/ui/shared/duration-popover-content.tsx
+++ b/apps/web/ui/shared/duration-popover-content.tsx
@@ -13,6 +13,9 @@ type DurationPopoverContentProps = {
onChange: (value: number) => void;
presetDurations: number[];
partnerReferralReward?: boolean;
+ presetsOnly?: boolean;
+ unit?: "days" | "months";
+ minValue?: number;
};
export function DurationPopoverContent({
@@ -20,18 +23,32 @@ export function DurationPopoverContent({
onChange,
presetDurations,
partnerReferralReward,
+ presetsOnly = false,
+ unit = "months",
+ minValue = 0,
}: DurationPopoverContentProps) {
const { isOpen, setIsOpen } = useContext(InlineBadgePopoverContext);
const [customDurationInput, setCustomDurationInput] = useState<string>(() => {
if (
value !== null &&
value !== undefined &&
+ !presetsOnly &&
value !== Infinity &&
value !== 0 &&
!presetDurations.includes(Number(value))
) {
return value.toString();
}
+
+ if (
+ presetsOnly &&
+ value !== null &&
+ value !== undefined &&
+ !presetDurations.includes(Number(value))
+ ) {
+ return value.toString();
+ }
+
return "";
});
const [showCustomInput, setShowCustomInput] = useState(false);
@@ -39,6 +56,20 @@ export function DurationPopoverContent({
useEffect(() => {
if (showCustomInput) return;
+ if (presetsOnly) {
+ if (
+ value === null ||
+ value === undefined ||
+ presetDurations.includes(Number(value))
+ ) {
+ setCustomDurationInput("");
+ return;
+ }
+
+ setCustomDurationInput(value.toString());
+ return;
+ }
+
if (
value === null ||
value === undefined ||
@@ -51,7 +82,7 @@ export function DurationPopoverContent({
}
setCustomDurationInput(value.toString());
- }, [value, presetDurations, showCustomInput]);
+ }, [value, presetDurations, showCustomInput, presetsOnly]);
useEffect(() => {
if (!isOpen) return;
@@ -59,10 +90,14 @@ export function DurationPopoverContent({
setShowCustomInput(true);
}, [isOpen, customDurationInput]);
- const isPresetValue =
- value === Infinity ||
- value === 0 ||
- presetDurations.includes(Number(value));
+ const isPresetValue = presetsOnly
+ ? presetDurations.includes(Number(value))
+ : value === Infinity ||
+ value === 0 ||
+ presetDurations.includes(Number(value));
+
+ const clampDuration = (parsed: number) =>
+ Math.max(minValue, Math.min(parsed, MAX_DURATION_LIMIT));
if (showCustomInput) {
return (
@@ -77,20 +112,23 @@ export function DurationPopoverContent({
<label className="flex w-full rounded-md border border-neutral-300 shadow-sm focus-within:border-neutral-500 focus-within:ring-1 focus-within:ring-neutral-500 sm:w-32">
<input
type="number"
- min="0"
+ min={minValue.toString()}
max={MAX_DURATION_LIMIT.toString()}
step="1"
autoFocus
- placeholder="e.g. 24"
+ placeholder={unit === "days" ? "e.g. 21" : "e.g. 24"}
value={customDurationInput}
className="block min-w-0 grow rounded-md border-none py-1 pl-1.5 pr-0 text-neutral-900 placeholder-neutral-400 focus:outline-none focus:ring-0 sm:text-sm"
onChange={(e: ChangeEvent<HTMLInputElement>) => {
const raw = e.target.value;
const parsed = parseInt(raw, 10);
- const clamped = Math.max(0, Math.min(parsed, MAX_DURATION_LIMIT));
+ const clamped = Math.max(
+ minValue,
+ Math.min(parsed, MAX_DURATION_LIMIT),
+ );
const display = isNaN(parsed) ? raw : clamped.toString();
setCustomDurationInput(display);
- if (!isNaN(parsed) && parsed >= 0) {
+ if (!isNaN(parsed) && parsed >= minValue) {
onChange(clamped);
}
}}
@@ -107,7 +145,7 @@ export function DurationPopoverContent({
}
if (e.key === "Enter") {
e.preventDefault();
- if (customDurationInput === "0") {
+ if (!presetsOnly && customDurationInput === "0") {
onChange(0);
setCustomDurationInput("");
setShowCustomInput(false);
@@ -118,13 +156,25 @@ export function DurationPopoverContent({
}}
/>
<span className="flex shrink-0 items-center pr-1.5 text-sm text-neutral-400">
- months
+ {unit}
</span>
</label>
</div>
);
}
+ const presetItems = presetsOnly
+ ? presetDurations.map((duration) => ({
+ text: duration.toString(),
+ value: duration.toString(),
+ }))
+ : presetDurations
+ .filter((duration) => duration !== 0)
+ .map((duration) => ({
+ text: `for ${duration} ${pluralize("month", Number(duration))}`,
+ value: duration.toString(),
+ }));
+
return (
<InlineBadgePopoverMenu
selectedValue={isPresetValue ? value?.toString() : undefined}
@@ -140,17 +190,18 @@ export function DurationPopoverContent({
setCustomDurationInput("");
}}
items={[
- ...(!partnerReferralReward ? [{ text: "one time", value: "0" }] : []),
- ...presetDurations
- .filter((duration) => duration !== 0)
- .map((duration) => ({
- text: `for ${duration} ${pluralize("month", Number(duration))}`,
- value: duration.toString(),
- })),
- {
- text: `for the ${partnerReferralReward ? "referred partner's" : "customer's"} lifetime`,
- value: "Infinity",
- },
+ ...(!presetsOnly && !partnerReferralReward
+ ? [{ text: "one time", value: "0" }]
+ : []),
+ ...presetItems,
+ ...(!presetsOnly
+ ? [
+ {
+ text: `for the ${partnerReferralReward ? "referred partner's" : "customer's"} lifetime`,
+ value: "Infinity",
+ },
+ ]
+ : []),
{ text: "custom", value: "custom", preventClose: true },
]}
/>