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.

Add product label reward modifiers

Kiran Kb9e7cf9231b1
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index efc3f8b2b6..e019792f47 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -52,6 +52,10 @@ export const rewardConditionSchema = z.object({
z.array(z.string()),
z.array(z.number()),
]),
+ label: z
+ .string()
+ .nullish()
+ .describe("Product name used for display purposes in the UI."),
});
export const rewardConditionsSchema = z.object({
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index ee33765cb4..ce143873d2 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -26,6 +26,7 @@ import {
} from "@dub/ui";
import { capitalize, cn, COUNTRIES, pluralize, truncate } from "@dub/utils";
import { Command } from "cmdk";
+import { Package } from "lucide-react";
import { Fragment, useContext, useState } from "react";
import { useFieldArray, useWatch } from "react-hook-form";
import { useAddEditRewardForm } from "./add-edit-reward-sheet";
@@ -263,180 +264,203 @@ function ConditionLogic({
condition.operator && ["in", "not_in"].includes(condition.operator);
return (
- <>
- <RewardIconSquare icon={icon} />
- <span className="text-content-emphasis font-medium leading-relaxed">
- {conditionIndex === 0 ? "If" : capitalize(operator.toLowerCase())}{" "}
- <InlineBadgePopover
- text={capitalize(condition.entity) || "Select item"}
- invalid={!condition.entity}
- >
- <InlineBadgePopoverMenu
- selectedValue={condition.entity}
- onSelect={(value) =>
- setValue(
- conditionKey,
- { entity: value as keyof typeof ENTITIES },
- {
- shouldDirty: true,
- },
- )
- }
- items={Object.keys(ENTITIES)
- .filter((e) =>
- EVENT_ENTITIES[event]?.includes(e as keyof typeof ENTITIES),
- )
- .map((entity) => ({
- text: capitalize(entity) || entity,
- value: entity,
- }))}
- />
- </InlineBadgePopover>{" "}
- {condition.entity && (
- <>
- <InlineBadgePopover
- text={
- condition.attribute
- ? ATTRIBUTE_LABELS?.[condition.attribute] ||
- capitalize(condition.attribute)
- : "Detail"
- }
- invalid={!condition.attribute}
- >
- <InlineBadgePopoverMenu
- selectedValue={condition.attribute}
- onSelect={(value) =>
- setValue(
- conditionKey,
- {
- entity: condition.entity,
- attribute: value as (typeof CONDITION_ATTRIBUTES)[number],
- },
- {
- shouldDirty: true,
- },
- )
- }
- items={ENTITIES[condition.entity].attributes.map(
- (attribute) => ({
- text:
- ATTRIBUTE_LABELS?.[attribute] ||
- capitalize(attribute) ||
- attribute,
- value: attribute,
- }),
- )}
- />
- </InlineBadgePopover>{" "}
- <InlineBadgePopover
- text={
- condition.operator
- ? CONDITION_OPERATOR_LABELS[condition.operator]
- : "Condition"
+ <div className="flex w-full flex-col">
+ <div className="flex items-center gap-1.5">
+ <RewardIconSquare icon={icon} />
+ <span className="text-content-emphasis font-medium leading-relaxed">
+ {conditionIndex === 0 ? "If" : capitalize(operator.toLowerCase())}{" "}
+ <InlineBadgePopover
+ text={capitalize(condition.entity) || "Select item"}
+ invalid={!condition.entity}
+ >
+ <InlineBadgePopoverMenu
+ selectedValue={condition.entity}
+ onSelect={(value) =>
+ setValue(
+ conditionKey,
+ { entity: value as keyof typeof ENTITIES },
+ {
+ shouldDirty: true,
+ },
+ )
}
- invalid={!condition.operator}
- >
- <InlineBadgePopoverMenu
- selectedValue={condition.operator}
- onSelect={(value) =>
- setValue(
- conditionKey,
- {
- ...condition,
- operator: value as (typeof CONDITION_OPERATORS)[number],
- // Update value to array / string if needed
- ...(["in", "not_in"].includes(value)
- ? !Array.isArray(condition.value)
- ? { value: [] }
- : null
- : typeof condition.value !== "string"
- ? { value: "" }
- : null),
- },
- {
- shouldDirty: true,
- },
- )
- }
- items={CONDITION_OPERATORS.map((operator) => ({
- text: CONDITION_OPERATOR_LABELS[operator],
- value: operator,
+ items={Object.keys(ENTITIES)
+ .filter((e) =>
+ EVENT_ENTITIES[event]?.includes(e as keyof typeof ENTITIES),
+ )
+ .map((entity) => ({
+ text: capitalize(entity) || entity,
+ value: entity,
}))}
- />
- </InlineBadgePopover>{" "}
- {condition.operator && (
+ />
+ </InlineBadgePopover>{" "}
+ {condition.entity && (
+ <>
<InlineBadgePopover
- text={formatValue(condition.value)}
- invalid={
- Array.isArray(condition.value)
- ? condition.value.filter(Boolean).length === 0
- : !condition.value
+ text={
+ condition.attribute
+ ? ATTRIBUTE_LABELS?.[condition.attribute] ||
+ capitalize(condition.attribute)
+ : "Detail"
}
+ invalid={!condition.attribute}
>
- {/* Country selection */}
- {condition.attribute === "country" &&
- !["starts_with", "ends_with"].includes(condition.operator) ? (
- <InlineBadgePopoverMenu
- search
- selectedValue={
- (condition.value as string[] | undefined) ??
- (isArrayValue ? [] : undefined)
- }
- items={Object.entries(COUNTRIES).map(([key, name]) => ({
- text: name,
- value: key,
- icon: (
- <img
- alt={`${key} flag`}
- src={`https://hatscripts.github.io/circle-flags/flags/${key.toLowerCase()}.svg`}
- className="size-3 shrink-0"
- />
- ),
- }))}
- onSelect={(value) => {
- setValue(conditionKey, {
+ <InlineBadgePopoverMenu
+ selectedValue={condition.attribute}
+ onSelect={(value) =>
+ setValue(
+ conditionKey,
+ {
+ entity: condition.entity,
+ attribute:
+ value as (typeof CONDITION_ATTRIBUTES)[number],
+ },
+ {
+ shouldDirty: true,
+ },
+ )
+ }
+ items={ENTITIES[condition.entity].attributes.map(
+ (attribute) => ({
+ text:
+ ATTRIBUTE_LABELS?.[attribute] ||
+ capitalize(attribute) ||
+ attribute,
+ value: attribute,
+ }),
+ )}
+ />
+ </InlineBadgePopover>{" "}
+ <InlineBadgePopover
+ text={
+ condition.operator
+ ? CONDITION_OPERATOR_LABELS[condition.operator]
+ : "Condition"
+ }
+ invalid={!condition.operator}
+ >
+ <InlineBadgePopoverMenu
+ selectedValue={condition.operator}
+ onSelect={(value) =>
+ setValue(
+ conditionKey,
+ {
...condition,
- value: isArrayValue
+ operator: value as (typeof CONDITION_OPERATORS)[number],
+ // Update value to array / string if needed
+ ...(["in", "not_in"].includes(value)
+ ? !Array.isArray(condition.value)
+ ? { value: [] }
+ : null
+ : typeof condition.value !== "string"
+ ? { value: "" }
+ : null),
+ },
+ {
+ shouldDirty: true,
+ },
+ )
+ }
+ items={CONDITION_OPERATORS.map((operator) => ({
+ text: CONDITION_OPERATOR_LABELS[operator],
+ value: operator,
+ }))}
+ />
+ </InlineBadgePopover>{" "}
+ {condition.operator && (
+ <InlineBadgePopover
+ text={formatValue(condition.value)}
+ invalid={
+ Array.isArray(condition.value)
+ ? condition.value.filter(Boolean).length === 0
+ : !condition.value
+ }
+ >
+ {/* Country selection */}
+ {condition.attribute === "country" &&
+ !["starts_with", "ends_with"].includes(condition.operator) ? (
+ <InlineBadgePopoverMenu
+ search
+ selectedValue={
+ (condition.value as string[] | undefined) ??
+ (isArrayValue ? [] : undefined)
+ }
+ items={Object.entries(COUNTRIES).map(([key, name]) => ({
+ text: name,
+ value: key,
+ icon: (
+ <img
+ alt={`${key} flag`}
+ src={`https://hatscripts.github.io/circle-flags/flags/${key.toLowerCase()}.svg`}
+ className="size-3 shrink-0"
+ />
+ ),
+ }))}
+ onSelect={(value) => {
+ setValue(conditionKey, {
+ ...condition,
+ value: isArrayValue
+ ? Array.isArray(condition.value)
+ ? (condition.value as string[]).includes(value)
+ ? (condition.value.filter(
+ (v) => v !== value,
+ ) as string[])
+ : ([...condition.value, value] as string[])
+ : [value]
+ : value,
+ });
+ }}
+ />
+ ) : isArrayValue ? (
+ // String array input
+ <InlineBadgePopoverInputs
+ values={
+ condition.value
? Array.isArray(condition.value)
- ? (condition.value as string[]).includes(value)
- ? (condition.value.filter(
- (v) => v !== value,
- ) as string[])
- : ([...condition.value, value] as string[])
- : [value]
- : value,
- });
- }}
- />
- ) : isArrayValue ? (
- // String array input
- <InlineBadgePopoverInputs
- values={
- condition.value
- ? Array.isArray(condition.value)
- ? condition.value.map(String)
- : [condition.value.toString()]
- : [""]
- }
- onChange={(values) => {
- setValue(conditionKey, {
- ...condition,
- value: values,
- });
- }}
- />
- ) : (
- // String input
- <InlineBadgePopoverInput
- {...register(`${conditionKey}.value`, { required: true })}
- />
- )}
+ ? condition.value.map(String)
+ : [condition.value.toString()]
+ : [""]
+ }
+ onChange={(values) => {
+ setValue(conditionKey, {
+ ...condition,
+ value: values,
+ });
+ }}
+ />
+ ) : (
+ // String input
+ <InlineBadgePopoverInput
+ {...register(`${conditionKey}.value`, { required: true })}
+ />
+ )}
+ </InlineBadgePopover>
+ )}
+ </>
+ )}
+ </span>
+ </div>
+
+ {/* Product name input - only show for sale productId conditions with a value */}
+ {condition.entity === "sale" &&
+ condition.attribute === "productId" &&
+ condition.value && (
+ <div className="flex items-center gap-1.5">
+ <RewardIconSquare icon={Package} />
+ <span className="text-content-emphasis font-medium leading-relaxed">
+ Shown as{" "}
+ <InlineBadgePopover
+ text={condition.label || "Product name"}
+ invalid={!condition.label}
+ >
+ <InlineBadgePopoverInput
+ {...register(`${conditionKey}.label`)}
+ />
</InlineBadgePopover>
- )}
- </>
+ </span>
+ </div>
)}
- </span>
- </>
+ </div>
);
}