The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Merge branch 'main' into default-lead-reward
Kiran Kd42975773d3e
diff --cc apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/rewards/form.tsxindex fd0470523a,5a56ac2ce7..4eaa1ac582--- a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/rewards/form.tsx+++ b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/rewards/form.tsx@@@ -200,16 -191,17 +204,21 @@@ export function Form()}const NewProgramForm = ({ register, watch, setValue }: FormProps) => {- const [isRecurring, setIsRecurring] = useState(false);-+ const [type, maxDuration, defaultRewardType] = watch([+ "type",+ "maxDuration",+ "defaultRewardType",+ ]);+ const [commissionStructure, setCommissionStructure] = useState<+ "one-off" | "recurring"+ >("recurring");- const [type, maxDuration] = watch(["type", "maxDuration"]);useEffect(() => {- setIsRecurring(maxDuration !== 0);+ setCommissionStructure(maxDuration === 0 ? "one-off" : "recurring");++ if (maxDuration === null) {+ setValue("maxDuration", Infinity);+ }}, [maxDuration]);return (@@@ -270,117 -267,26 +279,117 @@@);})}</div>+ </div>- {commissionStructure === "recurring" && (+ {defaultRewardType === "sale" && (+ <div className="grid grid-cols-1 gap-6">+ <div>+ <h2 className="text-base font-medium text-neutral-900">+ Commission structure+ </h2>+ <p className="mt-1 text-sm font-normal text-neutral-600">+ Set how the affiliate will get rewarded+ </p>+ </div>++ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">+ {COMMISSION_TYPES.map(({ value, label, description }) => {- const isSelected = (value === "recurring") === isRecurring;++ const isSelected = value === commissionStructure;++ return (+ <label+ key={value}+ className={cn(+ "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",+ "transition-all duration-150",+ isSelected &&+ "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",+ )}+ >+ <input+ type="radio"+ value={value}+ className="hidden"+ checked={isSelected}+ onChange={(e) => {+ if (value === "one-off") {- setIsRecurring(false);++ setCommissionStructure("one-off");+ setValue("maxDuration", 0, { shouldValidate: true });+ }++ if (value === "recurring") {- setIsRecurring(true);++ setCommissionStructure("recurring");+ setValue("maxDuration", 3, {+ shouldValidate: true,+ });+ }+ }}+ />+ <div className="flex grow flex-col text-sm">+ <span className="text-sm font-semibold text-neutral-900">+ {label}+ </span>+ <span className="text-sm font-normal text-neutral-600">+ {description}+ </span>+ </div>+ <CircleCheckFill+ className={cn(+ "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",+ isSelected && "scale-100 opacity-100",+ )}+ />+ </label>+ );+ })}+ </div>+- {isRecurring && (++ {commissionStructure === "recurring" && (+ <div>+ <label className="text-sm font-medium text-neutral-800">+ Duration+ </label>+ <select+ {...register("maxDuration", { valueAsNumber: true })}+ className="mt-2 block w-full rounded-md border border-neutral-300 bg-white py-2 pl-3 pr-10 text-sm text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500"+ >+ {RECURRING_MAX_DURATIONS.filter((v) => v !== 0).map(+ (duration) => (+ <option+ key={duration}+ value={duration}+ selected={duration === 12}+ >+ {duration} {duration === 1 ? "month" : "months"}+ </option>+ ),+ )}+ <option value="">Lifetime</option>+ </select>+ </div>+ )}+ </div>+ )}++ <div className="grid grid-cols-1 gap-6">+ <div>+ <h2 className="text-base font-medium text-neutral-900">Payout</h2>+ <p className="mt-1 text-sm font-normal text-neutral-600">+ Set how much the affiliate will get rewarded+ </p>+ </div>++ {defaultRewardType === "sale" && (<div><label className="text-sm font-medium text-neutral-800">- Duration+ Payout model</label><select- {...register("maxDuration", { valueAsNumber: true })}+ {...register("type")}className="mt-2 block w-full rounded-md border border-neutral-300 bg-white py-2 pl-3 pr-10 text-sm text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500">- {RECURRING_MAX_DURATIONS.filter((v) => v !== 0).map(- (duration) => (- <option key={duration} value={duration}>- {duration} {duration === 1 ? "month" : "months"}- </option>- ),- )}- <option value={Infinity} selected>- Lifetime- </option>+ <option value="flat">Flat</option>+ <option value="percentage">Percentage</option></select></div>)}diff --cc apps/web/lib/zod/schemas/program-onboarding.tsindex fb96ad668a,a308050ffa..f8c9dc5c25--- a/apps/web/lib/zod/schemas/program-onboarding.ts+++ b/apps/web/lib/zod/schemas/program-onboarding.ts@@@ -30,15 -30,9 +30,10 @@@ export const programRewardSchema =}).merge(z.object({+ defaultRewardType: z.enum(["lead", "sale"]).default("lead"),type: z.nativeEnum(CommissionType).nullish(),amount: z.number().min(0).nullish(),- maxDuration: z.coerce- .number()- .refine((val) => RECURRING_MAX_DURATIONS.includes(val), {- message: `Max duration must be ${RECURRING_MAX_DURATIONS.join(", ")}`,- })- .nullish(),+ maxDuration: maxDurationSchema,}),);diff --cc apps/web/ui/partners/add-edit-reward-sheet.tsxindex ccc1d36a2d,62fda2a8ce..024ae434af--- a/apps/web/ui/partners/add-edit-reward-sheet.tsx+++ b/apps/web/ui/partners/add-edit-reward-sheet.tsx@@@ -95,17 -85,15 +103,15 @@@ function RewardSheetContent(} = useForm<FormData>({defaultValues: {event,- type: reward?.type || "flat",- type: reward?.type || "percentage",++ type: reward?.type || (event === "sale" ? "percentage" : "flat"),maxDuration: reward? reward.maxDuration === null? Infinity: reward.maxDuration- : 12,- amount:- reward?.type === "flat" && reward?.amount- ? reward.amount / 100- : reward?.amount,+ : Infinity,+ amount: reward?.type === "flat" ? reward.amount / 100 : reward?.amount,- partnerIds: null,+ maxAmount: reward?.maxAmount ? reward.maxAmount / 100 : null,+ partnerIds: null,},});@@@ -223,9 -218,9 +238,12 @@@amount: type === "flat" ? data.amount * 100 : data.amount,maxDuration:Infinity === Number(data.maxDuration) ? null : data.maxDuration,- partnerIds,+ maxAmount: data.maxAmount ? data.maxAmount * 100 : null,};++ console.log(payload);++ return;++if (!reward) {await createReward(payload);} else {@@@ -252,15 -247,25 +270,14 @@@});};-- const hasDefaultReward = !!program?.defaultRewardId;--const buttonDisabled =- isCreating ||- isUpdating ||amount == null ||(selectedPartnerType === "specific" &&(!partnerIds || partnerIds.length === 0));- const displayPartners =- (event === "sale" &&- hasDefaultReward &&- reward?.id !== program?.defaultRewardId) ||- (event !== "sale" && selectedPartnerType === "specific");-- const canDeleteReward =- (reward && program?.defaultRewardId !== reward.id) ||- isCreating ||- isUpdating;++ const hasDefaultReward = !!program?.defaultRewardId;+ const displayPartners = !isDefault && selectedPartnerType === "specific";+ const canDeleteReward = reward && program?.defaultRewardId !== reward.id;return (<>@@@ -337,178 -407,105 +354,178 @@@</div>)}- {event === "sale" && (- <>+ {!isDefault && (+ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">+ {PARTNER_TYPES.map((partnerType) => {+ const isSelected = selectedPartnerType === partnerType.key;++ const isDisabled =+ (partnerType.key === "all" &&+ ((selectedEvent === "click" &&+ hasProgramWideClickReward) ||+ (selectedEvent === "lead" &&+ hasProgramWideLeadReward) ||+ (selectedEvent === "sale" &&+ hasProgramWideSaleReward))) ||+ !!reward;++ const tooltipContent = isDisabled+ ? reward+ ? "Partner type cannot be changed for existing rewards"+ : `You can only have one program-wide ${selectedEvent} reward.`+ : undefined;++ const labelContent = (+ <label+ key={partnerType.label}+ className={cn(+ "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",+ "transition-all duration-150",+ isSelected &&+ "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",+ (isDisabled || !!reward) &&+ "cursor-not-allowed opacity-60 hover:bg-white",+ )}+ >+ <input+ type="radio"+ value={partnerType.label}+ className="hidden"+ checked={isSelected}+ disabled={isDisabled}+ onChange={(e) => {+ if (e.target.checked) {+ setSelectedPartnerType(partnerType.key);++ if (partnerType.key === "all") {+ setValue("partnerIds", null);+ }+ }+ }}+ />+ <div className="flex grow flex-col text-sm">+ <span className="font-medium">{partnerType.label}</span>+ <span>{partnerType.description}</span>+ </div>+ <CircleCheckFill+ className={cn(+ "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",+ isSelected && "scale-100 opacity-100",+ )}+ />+ </label>+ );++ return isDisabled ? (+ <Tooltip key={partnerType.label} content={tooltipContent}>+ {labelContent}+ </Tooltip>+ ) : (+ labelContent+ );+ })}+ </div>+ )}++ {selectedEvent === "sale" && (+ <div className="grid grid-cols-1 space-y-4"><div><label className="text-sm font-medium text-neutral-800">- Commission+ Commission structure</label>- <p className="mb-4 text-sm text-neutral-600">+ <p className="text-sm text-neutral-600">Set how the affiliate will get rewarded</p>- <div className="-m-1">- <AnimatedSizeContainer- height- transition={{ ease: "easeInOut", duration: 0.2 }}- >- <div className="p-1">- <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">- {COMMISSION_TYPES.map(- ({ label, description, value }) => {- const isSelected = value === commissionStructure;-- return (- <label- key={label}+ </div>-+ <div className="-m-1">+ <AnimatedSizeContainer+ height+ transition={{ ease: "easeInOut", duration: 0.2 }}+ >+ <div className="flex flex-col p-1">+ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">+ {COMMISSION_TYPES.map(+ ({ label, description, value }) => {- const isSelected =- (value === "recurring") === isRecurring;++ const isSelected = value === commissionStructure;++ return (+ <label+ key={label}+ className={cn(+ "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",+ "transition-all duration-150",+ isSelected &&+ "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",+ )}+ >+ <input+ type="radio"+ value={value}+ className="hidden"+ checked={isSelected}+ onChange={(e) => {+ if (e.target.checked) {- setIsRecurring(value === "recurring");++ setCommissionStructure(value);+ setValue(+ "maxDuration",+ value === "recurring"- ? reward?.maxDuration || 12++ ? reward?.maxDuration || Infinity+ : 0,+ );+ }+ }}+ />+ <div className="flex grow flex-col text-sm">+ <span className="font-medium">{label}</span>+ <span>{description}</span>+ </div>+ <CircleCheckFillclassName={cn(- "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",- "transition-all duration-150",- isSelected &&- "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",+ "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",+ isSelected && "scale-100 opacity-100",)}- >- <input- type="radio"- value={value}- className="hidden"- checked={isSelected}- onChange={(e) => {- if (e.target.checked) {- setCommissionStructure(value);- setValue(- "maxDuration",- value === "recurring"- ? reward?.maxDuration || Infinity- : 0,- );- }- }}- />- <div className="flex grow flex-col text-sm">- <span className="font-medium">{label}</span>- <span>{description}</span>- </div>- <CircleCheckFill- className={cn(- "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",- isSelected && "scale-100 opacity-100",- )}- />- </label>- );- },- )}- </div>+ />+ </label>+ );+ },+ )}+ </div>- <div- className={cn(- "transition-opacity duration-200",- commissionStructure === "recurring"- ? "h-auto"- : "h-0 opacity-0",- )}- aria-hidden={commissionStructure !== "recurring"}- {...{- inert: commissionStructure !== "recurring",- }}- >- <div className="pt-6">- <label- htmlFor="duration"- className="text-sm font-medium text-neutral-800"+ <div+ className={cn(+ "transition-opacity duration-200",- isRecurring ? "h-auto pt-4" : "h-0 opacity-0",++ commissionStructure === "recurring"++ ? "h-auto"++ : "h-0 opacity-0",+ )}- aria-hidden={!isRecurring}++ aria-hidden={commissionStructure !== "recurring"}+ {...{- inert: !isRecurring,++ inert: commissionStructure !== "recurring",+ }}+ >+ <div>+ <label+ htmlFor="duration"+ className="text-sm font-medium text-neutral-800"+ >+ Duration+ </label>+ <div className="relative mt-2 rounded-md shadow-sm">+ <select+ className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"+ {...register("maxDuration", {+ valueAsNumber: true,+ })}>- Duration- </label>- <div className="relative mt-2 rounded-md shadow-sm">- <select- className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"- {...register("maxDuration", {- valueAsNumber: true,- })}- >- {RECURRING_MAX_DURATIONS.filter(- (v) => v !== 0,- ).map((v) => (- <option value={v} key={v}>- {v} {pluralize("month", Number(v))}- </option>- ))}- <option value={Infinity}>Lifetime</option>- </select>- </div>+ {RECURRING_MAX_DURATIONS.filter(+ (v) => v !== 0,+ ).map((v) => (+ <option value={v} key={v}>+ {v} {pluralize("month", Number(v))}+ </option>+ ))}+ <option value={Infinity}>Lifetime</option>+ </select></div></div></div>