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 multi-values

Tim Wilsonbd2ddbfbdc87
diff --git a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
index b7ed65a08d..9caf79e4c9 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -292,16 +292,16 @@ function RewardSheetContent({
>
<div className="relative rounded-md shadow-sm">
{type === "flat" && (
- <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
+ <span className="absolute inset-y-0 left-0 flex items-center pl-1.5 text-sm text-neutral-400">
$
</span>
)}
<input
className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:w-32 sm:text-sm",
+ "block w-full rounded-md border-neutral-300 px-1.5 py-1 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:w-32 sm:text-sm",
errors.amount &&
"border-red-600 focus:border-red-500 focus:ring-red-600",
- type === "flat" ? "pl-6 pr-12" : "pr-7",
+ type === "flat" ? "pl-4 pr-12" : "pr-7",
)}
{...register("amount", {
required: true,
@@ -313,7 +313,7 @@ function RewardSheetContent({
})}
onKeyDown={handleMoneyKeyDown}
/>
- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
+ <span className="absolute inset-y-0 right-0 flex items-center pr-1.5 text-sm text-neutral-400">
{type === "flat" ? "USD" : "%"}
</span>
</div>
diff --git a/apps/web/ui/partners/rewards/inline-badge-popover.tsx b/apps/web/ui/partners/rewards/inline-badge-popover.tsx
index 4e38dfd06a..1e0a19222d 100644
--- a/apps/web/ui/partners/rewards/inline-badge-popover.tsx
+++ b/apps/web/ui/partners/rewards/inline-badge-popover.tsx
@@ -1,16 +1,21 @@
"use client";
-import { Check2, Popover, useScrollProgress } from "@dub/ui";
+import { X } from "@/ui/shared/icons";
+import { Button, Check2, Plus, Popover, useScrollProgress } from "@dub/ui";
import { cn } from "@dub/utils";
import { Command } from "cmdk";
import {
createContext,
+ forwardRef,
+ HTMLProps,
PropsWithChildren,
ReactNode,
useContext,
+ useEffect,
useRef,
useState,
} from "react";
+import { v4 as uuid } from "uuid";
export const InlineBadgePopoverContext = createContext<{
isOpen: boolean;
@@ -108,3 +113,95 @@ export function InlineBadgePopoverMenu({
</div>
);
}
+
+export const InlineBadgePopoverInput = forwardRef<HTMLInputElement>(
+ (props: HTMLProps<HTMLInputElement>, ref) => {
+ 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",
+ )}
+ {...props}
+ />
+ </div>
+ );
+ },
+);
+
+export const InlineBadgePopoverInputs = ({
+ values: valuesProp,
+ onChange,
+ inputProps,
+}: {
+ values: string[];
+ onChange: (values: string[]) => void;
+ inputProps?: HTMLProps<HTMLInputElement>;
+}) => {
+ const [values, setValues] = useState<{ id: string; value: string }[]>(() =>
+ valuesProp.map((value) => ({ id: uuid(), value })),
+ );
+
+ useEffect(() => {
+ const currentValues = values.map((item) => item.value);
+ if (JSON.stringify(currentValues) !== JSON.stringify(valuesProp)) {
+ setValues(valuesProp.map((value) => ({ id: uuid(), value })));
+ }
+ }, [valuesProp, values]);
+
+ const handleUpdate = (id: string, newValue: string) => {
+ const updatedValues = values.map((item) =>
+ item.id === id ? { ...item, value: newValue } : item,
+ );
+ setValues(updatedValues);
+ onChange(updatedValues.map((item) => item.value));
+ };
+
+ const handleDelete = (id: string) => {
+ const updatedValues = values.filter((item) => item.id !== id);
+ setValues(updatedValues);
+ onChange(updatedValues.map((item) => item.value));
+ };
+
+ const handleAppend = () => {
+ const newItem = { id: uuid(), value: "" };
+ const updatedValues = [...values, newItem];
+ setValues(updatedValues);
+ onChange(updatedValues.map((item) => item.value));
+ };
+
+ return (
+ <div className="flex flex-col gap-1">
+ {values.map(({ id, value }, index) => (
+ <div className="flex items-center gap-1" key={id}>
+ <input
+ className={cn(
+ "relative block w-full rounded-md border-neutral-300 px-1.5 py-1 text-neutral-900 placeholder-neutral-400 shadow-sm sm:w-32 sm:text-sm",
+ "focus:border-neutral-500 focus:outline-none focus:ring-neutral-500",
+ )}
+ value={value}
+ onChange={(e) => handleUpdate(id, e.target.value)}
+ autoFocus={index === values.length - 1}
+ {...inputProps}
+ />
+ {values.length > 1 && (
+ <Button
+ variant="outline"
+ className="h-6 w-fit px-1"
+ icon={<X className="size-4" />}
+ onClick={() => handleDelete(id)}
+ />
+ )}
+ </div>
+ ))}
+ <Button
+ variant="outline"
+ className="h-4 w-full px-1"
+ icon={<Plus className="size-3" />}
+ onClick={handleAppend}
+ />
+ </div>
+ );
+};
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index 60e3f804b1..080acd48bb 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -20,13 +20,15 @@ import {
TooltipContent,
User,
} from "@dub/ui";
-import { capitalize, cn } from "@dub/utils";
+import { capitalize, cn, truncate } from "@dub/utils";
import { Command } from "cmdk";
import { Fragment, useState } from "react";
import { useFieldArray, useWatch } from "react-hook-form";
import { useAddEditRewardForm } from "./add-edit-reward-sheet";
import {
InlineBadgePopover,
+ InlineBadgePopoverInput,
+ InlineBadgePopoverInputs,
InlineBadgePopoverMenu,
} from "./inline-badge-popover";
import { RewardIconSquare } from "./reward-icon-square";
@@ -178,6 +180,10 @@ const ENTITIES = {
},
} as const;
+const ATTRIBUTE_LABELS = {
+ productId: "Product ID",
+};
+
const OPERATOR_LABELS = {
equals_to: "is",
not_equals: "is not",
@@ -187,6 +193,27 @@ const OPERATOR_LABELS = {
not_in: "is not one of",
} as const;
+const formatValue = (
+ value: string | number | string[] | number[] | undefined,
+) => {
+ if (!value) return "Value";
+
+ if (Array.isArray(value)) {
+ if (!value.filter(Boolean).length) return "Value";
+
+ const filtered = value.filter(Boolean);
+
+ return (
+ filtered
+ .map((v) => truncate(v.toString(), 16))
+ .slice(0, 2)
+ .join(", ") + (filtered.length > 2 ? ` +${filtered.length - 2}` : "")
+ );
+ }
+
+ return truncate(value.toString(), 20);
+};
+
function ConditionLogic({
modifierIndex,
conditionIndex,
@@ -197,7 +224,7 @@ function ConditionLogic({
const modifierKey = `modifiers.${modifierIndex}` as const;
const conditionKey = `${modifierKey}.conditions.${conditionIndex}` as const;
- const { control, setValue } = useAddEditRewardForm();
+ const { control, setValue, register } = useAddEditRewardForm();
const [condition, operator] = useWatch({
control,
name: [conditionKey, `${modifierKey}.operator`],
@@ -210,7 +237,7 @@ function ConditionLogic({
return (
<>
<RewardIconSquare icon={icon} />
- <span className="text-content-emphasis font-medium">
+ <span className="text-content-emphasis font-medium leading-relaxed">
{conditionIndex === 0 ? "If" : capitalize(operator.toLowerCase())}{" "}
<InlineBadgePopover
text={capitalize(condition.entity) || "Select item"}
@@ -236,7 +263,12 @@ function ConditionLogic({
{condition.entity && (
<>
<InlineBadgePopover
- text={capitalize(condition.attribute) || "Detail"}
+ text={
+ condition.attribute
+ ? ATTRIBUTE_LABELS?.[condition.attribute] ||
+ capitalize(condition.attribute)
+ : "Detail"
+ }
invalid={!condition.attribute}
>
<InlineBadgePopoverMenu
@@ -255,7 +287,10 @@ function ConditionLogic({
}
items={ENTITIES[condition.entity].attributes.map(
(attribute) => ({
- text: capitalize(attribute) || attribute,
+ text:
+ ATTRIBUTE_LABELS?.[attribute] ||
+ capitalize(attribute) ||
+ attribute,
value: attribute,
}),
)}
@@ -273,8 +308,19 @@ function ConditionLogic({
selectedValue={condition.operator}
onSelect={(value) =>
setValue(
- `${conditionKey}.operator`,
- value as (typeof CONDITION_OPERATORS)[number],
+ 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,
},
@@ -285,7 +331,39 @@ function ConditionLogic({
value: operator,
}))}
/>
- </InlineBadgePopover>
+ </InlineBadgePopover>{" "}
+ {condition.operator && (
+ <InlineBadgePopover
+ text={formatValue(condition.value)}
+ invalid={
+ Array.isArray(condition.value)
+ ? condition.value.length === 0
+ : !condition.value
+ }
+ >
+ {["in", "not_in"].includes(condition.operator) ? (
+ <InlineBadgePopoverInputs
+ values={
+ condition.value
+ ? Array.isArray(condition.value)
+ ? condition.value.map(String)
+ : [condition.value.toString()]
+ : [""]
+ }
+ onChange={(values) => {
+ setValue(conditionKey, {
+ ...condition,
+ value: values,
+ });
+ }}
+ />
+ ) : (
+ <InlineBadgePopoverInput
+ {...register(`${conditionKey}.value`, { required: true })}
+ />
+ )}
+ </InlineBadgePopover>
+ )}
</>
)}
</span>