The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Custom reward description configuration
Tim Wilson6ca6e29fa3a8
diff --git a/apps/web/lib/actions/partners/create-reward.ts b/apps/web/lib/actions/partners/create-reward.tsindex 8b66dcf7fb..1fa1e30600 100644--- a/apps/web/lib/actions/partners/create-reward.ts+++ b/apps/web/lib/actions/partners/create-reward.ts@@ -18,8 +18,15 @@ export const createRewardAction = authActionClient.schema(createRewardSchema).action(async ({ parsedInput, ctx }) => {const { workspace, user } = ctx;- const { event, amount, type, maxDuration, modifiers, groupId } =- parsedInput;+ const {+ event,+ amount,+ type,+ maxDuration,+ description,+ modifiers,+ groupId,+ } = parsedInput;const programId = getDefaultProgramIdOrThrow(workspace);const { canUseAdvancedRewardLogic } = getPlanCapabilities(workspace.plan);@@ -52,6 +59,7 @@ export const createRewardAction = authActionClienttype,amount,maxDuration,+ description: description || null,modifiers: modifiers || Prisma.DbNull,},});diff --git a/apps/web/lib/actions/partners/update-reward.ts b/apps/web/lib/actions/partners/update-reward.tsindex b6d962844c..67f3f226c1 100644--- a/apps/web/lib/actions/partners/update-reward.ts+++ b/apps/web/lib/actions/partners/update-reward.ts@@ -15,7 +15,8 @@ export const updateRewardAction = authActionClient.schema(updateRewardSchema).action(async ({ parsedInput, ctx }) => {const { workspace, user } = ctx;- const { rewardId, amount, maxDuration, type, modifiers } = parsedInput;+ const { rewardId, amount, maxDuration, type, description, modifiers } =+ parsedInput;const programId = getDefaultProgramIdOrThrow(workspace);@@ -40,6 +41,7 @@ export const updateRewardAction = authActionClienttype,amount,maxDuration,+ description: description || null,modifiers: modifiers === null ? Prisma.DbNull : modifiers,},include: {diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.tsindex b5ec51540d..20186e4cb3 100644--- a/apps/web/lib/zod/schemas/rewards.ts+++ b/apps/web/lib/zod/schemas/rewards.ts@@ -150,6 +150,7 @@ export const createOrUpdateRewardSchema = z.object({amount: z.number().min(0),maxDuration: maxDurationSchema,modifiers: rewardConditionsArraySchema.nullish(),+ description: z.string().nullish(),groupId: z.string(),});diff --git a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsxindex f25e1d1acd..616ede5903 100644--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx@@ -22,12 +22,15 @@ import { X } from "@/ui/shared/icons";import { EventType, RewardStructure } from "@dub/prisma/client";import {Button,+ Gift,MoneyBills2,+ Pen2,Sheet,TooltipContent,useRouterStuff,} from "@dub/ui";import { capitalize, cn, pluralize } from "@dub/utils";+import { motion } from "framer-motion";import { useAction } from "next-safe-action/hooks";import {Dispatch,@@ -46,6 +49,7 @@ import { z } from "zod";import {InlineBadgePopover,InlineBadgePopoverContext,+ InlineBadgePopoverInput,InlineBadgePopoverMenu,} from "../../shared/inline-badge-popover";import { usePartnersUpgradeModal } from "../partners-upgrade-modal";@@ -103,6 +107,7 @@ function RewardSheetContent({defaultValuesSource?.type === "flat"? defaultValuesSource.amount / 100: defaultValuesSource?.amount,+ description: defaultValuesSource?.description ?? null,modifiers: defaultValuesSource?.modifiers?.map((m) => {const type = m.type === undefined ? defaultValuesSource?.type : m.type;const maxDuration =@@ -129,15 +134,17 @@ function RewardSheetContent({},});- const { handleSubmit, watch, setValue, setError } = form;+ const { handleSubmit, watch, setValue, register, setError } = form;- const [selectedEvent, amount, type, maxDuration, modifiers] = watch([- "event",- "amount",- "type",- "maxDuration",- "modifiers",- ]);+ const [selectedEvent, amount, type, maxDuration, description, modifiers] =+ watch([+ "event",+ "amount",+ "type",+ "maxDuration",+ "description",+ "modifiers",+ ]);const { executeAsync: createReward, isPending: isCreating } = useAction(createRewardAction,@@ -188,7 +195,7 @@ function RewardSheetContent({useEffect(() => {if (- modifiers?.length > 0 &&+ modifiers?.length &&!getPlanCapabilities(plan).canUseAdvancedRewardLogic) {setShowAdvancedUpsell(true);@@ -308,83 +315,139 @@ function RewardSheetContent({<div className="flex flex-1 flex-col overflow-y-auto p-6"><RewardSheetCardtitle={- <>- <RewardIconSquare icon={MoneyBills2} />- <span className="leading-relaxed">- Pay{" "}- {selectedEvent === "sale" && (- <>- a{" "}- <InlineBadgePopover text={capitalize(type)}>- <InlineBadgePopoverMenu- selectedValue={type}- onSelect={(value) =>- setValue("type", value as RewardStructure, {- shouldDirty: true,- })- }- items={REWARD_TYPES}- />- </InlineBadgePopover>{" "}- {type === "percentage" && "of "}- </>- )}- <InlineBadgePopover- text={- !isNaN(amount)- ? constructRewardAmount({- amount: type === "flat" ? amount * 100 : amount,- type,- maxDuration,- })- : "amount"- }- invalid={isNaN(amount)}- >- <AmountInput />- </InlineBadgePopover>{" "}- per {selectedEvent}- {selectedEvent === "sale" && (- <>- {" "}+ <div className="w-full">+ <div className="flex min-w-0 items-center justify-between">+ <div className="flex min-w-0 items-center gap-2.5">+ <RewardIconSquare icon={MoneyBills2} />+ <span className="leading-relaxed">+ Pay{" "}+ {selectedEvent === "sale" && (+ <>+ a{" "}+ <InlineBadgePopover text={capitalize(type)}>+ <InlineBadgePopoverMenu+ selectedValue={type}+ onSelect={(value) =>+ setValue("type", value as RewardStructure, {+ shouldDirty: true,+ })+ }+ items={REWARD_TYPES}+ />+ </InlineBadgePopover>{" "}+ {type === "percentage" && "of "}+ </>+ )}<InlineBadgePopovertext={- maxDuration === 0- ? "one time"- : maxDuration === Infinity- ? "for the customer's lifetime"- : `for ${maxDuration} ${pluralize("month", Number(maxDuration))}`+ !isNaN(amount)+ ? constructRewardAmount({+ amount: type === "flat" ? amount * 100 : amount,+ type,+ maxDuration,+ })+ : "amount"}+ invalid={isNaN(amount)}>- <InlineBadgePopoverMenu- selectedValue={maxDuration?.toString()}- onSelect={(value) =>- setValue("maxDuration", Number(value), {- shouldDirty: true,- })- }- items={[- {- text: "one time",- value: "0",- },- ...RECURRING_MAX_DURATIONS.filter(- (v) => v !== 0 && v !== 1, // filter out one-time and 1-month intervals (we only use 1-month for discounts)- ).map((v) => ({- text: `for ${v} ${pluralize("month", Number(v))}`,- value: v.toString(),- })),- {- text: "for the customer's lifetime",- value: "Infinity",- },- ]}- />- </InlineBadgePopover>- </>- )}- </span>- </>+ <AmountInput />+ </InlineBadgePopover>{" "}+ per {selectedEvent}+ {selectedEvent === "sale" && (+ <>+ {" "}+ <InlineBadgePopover+ text={+ maxDuration === 0+ ? "one time"+ : maxDuration === Infinity+ ? "for the customer's lifetime"+ : `for ${maxDuration} ${pluralize("month", Number(maxDuration))}`+ }+ >+ <InlineBadgePopoverMenu+ selectedValue={maxDuration?.toString()}+ onSelect={(value) =>+ setValue("maxDuration", Number(value), {+ shouldDirty: true,+ })+ }+ items={[+ {+ text: "one time",+ value: "0",+ },+ ...RECURRING_MAX_DURATIONS.filter(+ (v) => v !== 0 && v !== 1, // filter out one-time and 1-month intervals (we only use 1-month for discounts)+ ).map((v) => ({+ text: `for ${v} ${pluralize("month", Number(v))}`,+ value: v.toString(),+ })),+ {+ text: "for the customer's lifetime",+ value: "Infinity",+ },+ ]}+ />+ </InlineBadgePopover>+ </>+ )}+ </span>+ </div>+ <Button+ variant="secondary"+ className={cn(+ "size-7 p-0",+ description !== null && "text-blue-600",+ )}+ icon={<Pen2 className="size-3.5" />}+ onClick={() =>+ setValue(+ "description",+ description === null ? "" : null,+ { shouldDirty: true },+ )+ }+ />+ </div>+ <motion.div+ initial={false}+ transition={{ ease: "easeInOut", duration: 0.2 }}+ animate={{+ height: description !== null ? "auto" : 0,+ opacity: description !== null ? 1 : 0,+ }}+ className="-mx-2.5 overflow-hidden"+ >+ <div className="pt-2.5">+ <div className="border-border-subtle flex min-w-0 items-center gap-2.5 border-t px-2.5 pt-2.5">+ <RewardIconSquare icon={Gift} />+ <span className="leading-relaxed">+ Shown as{" "}+ <InlineBadgePopover+ text={description || "Reward description"}+ invalid={!description}+ >+ <InlineBadgePopoverInput+ value={description ?? ""}+ onChange={(e) =>+ setValue(+ "description",+ (e.target as HTMLInputElement).value,+ {+ shouldDirty: true,+ },+ )+ }+ className="sm:w-80"+ maxLength={100}+ />+ </InlineBadgePopover>+ </span>+ </div>+ </div>+ </motion.div>+ </div>}content={selectedEvent === "click" ? undefined : (diff --git a/apps/web/ui/shared/inline-badge-popover.tsx b/apps/web/ui/shared/inline-badge-popover.tsxindex 55454979fe..887fd46438 100644--- a/apps/web/ui/shared/inline-badge-popover.tsx+++ b/apps/web/ui/shared/inline-badge-popover.tsx@@ -62,14 +62,14 @@ export function InlineBadgePopover({<buttontype="button"className={cn(- "inline-block rounded px-1.5 text-sm font-semibold transition-colors",+ "inline-block rounded px-1.5 text-left text-sm font-semibold transition-colors",invalid? "bg-orange-50 text-orange-500 hover:bg-orange-100 data-[state=open]:bg-orange-100": "bg-blue-50 text-blue-700 hover:bg-blue-100 data-[state=open]:bg-blue-100",buttonClassName,)}>- {text}+ <span>{text}</span></button></Popover>);@@ -173,30 +173,46 @@ export function InlineBadgePopoverMenu<T extends any>({);}-export const InlineBadgePopoverInput = forwardRef<HTMLInputElement>(- (props: HTMLProps<HTMLInputElement>, ref) => {- const { setIsOpen } = useContext(InlineBadgePopoverContext);+export const InlineBadgePopoverInput = forwardRef<+ HTMLInputElement,+ HTMLProps<HTMLInputElement>+>(({ maxLength, className, ...rest }: HTMLProps<HTMLInputElement>, ref) => {+ const { setIsOpen } = useContext(InlineBadgePopoverContext);- return (- <div className="relative rounded-md shadow-sm">- <input- ref={ref}- className={cn(- "block w-full rounded-md border-neutral-300 px-1.5 py-1 text-neutral-900 placeholder-neutral-400 sm:w-32 sm:text-sm",- "focus:border-neutral-500 focus:outline-none focus:ring-neutral-500",- )}- onKeyDown={(e) => {- if (e.key === "Enter") {- e.preventDefault();- setIsOpen(false);- }- }}- {...props}- />- </div>- );- },-);+ return (+ <label+ className={cn(+ "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",+ className,+ )}+ >+ <input+ ref={ref}+ className={cn(+ "block grow rounded-md border-none px-1.5 py-1 text-neutral-900 placeholder-neutral-400 sm:text-sm",+ "focus:outline-none focus:ring-0",+ maxLength && "pr-0",+ )}+ onKeyDown={(e) => {+ if (e.key === "Enter") {+ e.preventDefault();+ setIsOpen(false);+ }+ }}+ maxLength={maxLength}+ {...rest}+ />+ {maxLength && (+ <span className="relative -ml-4 flex shrink-0 items-center pl-5 pr-1.5 text-xs text-neutral-500">+ <span className="absolute inset-y-0 left-0 block w-4 bg-gradient-to-l from-white" />+ <span>+ {rest.value?.toString().length || 0}/{maxLength}+ </span>+ </span>+ )}+ </label>+ );+});export const InlineBadgePopoverInputs = ({values: valuesProp,