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.

Country selection

Tim Wilson81bb21b82cca
diff --git a/apps/web/ui/partners/rewards/inline-badge-popover.tsx b/apps/web/ui/partners/rewards/inline-badge-popover.tsx
index 1e0a19222d..d207833188 100644
--- a/apps/web/ui/partners/rewards/inline-badge-popover.tsx
+++ b/apps/web/ui/partners/rewards/inline-badge-popover.tsx
@@ -1,7 +1,14 @@
"use client";
import { X } from "@/ui/shared/icons";
-import { Button, Check2, Plus, Popover, useScrollProgress } from "@dub/ui";
+import {
+ AnimatedSizeContainer,
+ Button,
+ Check2,
+ Plus,
+ Popover,
+ useScrollProgress,
+} from "@dub/ui";
import { cn } from "@dub/utils";
import { Command } from "cmdk";
import {
@@ -64,58 +71,80 @@ export function InlineBadgePopover({
);
}
-export function InlineBadgePopoverMenu({
+export function InlineBadgePopoverMenu<T extends any>({
items,
onSelect,
selectedValue,
+ search,
}: {
- items: { text: string; value: string; onSelect?: () => void }[];
- onSelect?: (value: string) => void;
- selectedValue?: string;
+ items: { icon?: ReactNode; text: string; value: T; onSelect?: () => void }[];
+ onSelect?: (value: T) => void;
+ selectedValue?: T | T[];
+ search?: boolean;
}) {
+ const isMultiSelect = Array.isArray(selectedValue);
+
const { setIsOpen } = useContext(InlineBadgePopoverContext);
const scrollRef = useRef<HTMLDivElement>(null);
const { scrollProgress, updateScrollProgress } = useScrollProgress(scrollRef);
return (
- <div className="relative">
- <Command loop className="focus:outline-none">
- <Command.List
- className="scrollbar-hide flex max-h-64 flex-col gap-1 overflow-y-auto transition-all"
- ref={scrollRef}
- onScroll={updateScrollProgress}
- >
- {items.map(({ text, value, onSelect: itemOnSelect }) => (
- <Command.Item
- key={text}
- onSelect={() => {
- itemOnSelect?.();
- onSelect?.(value);
- setIsOpen(false);
- }}
- className="flex cursor-pointer items-center justify-between rounded-md px-1.5 py-1 transition-colors duration-150 hover:bg-neutral-100"
- >
- <span className="text-content-default pr-3 text-left text-sm font-medium">
- {text}
- </span>
- {selectedValue === value && (
- <Check2 className="text-content-emphasis size-3.5 shrink-0" />
- )}
- </Command.Item>
- ))}
- </Command.List>
- </Command>
- <div
- className="pointer-events-none absolute bottom-0 left-0 hidden h-12 w-full rounded-b-lg bg-gradient-to-t from-white sm:block"
- style={{ opacity: 1 - Math.pow(scrollProgress, 2) }}
- />
- </div>
+ <Command loop className="focus:outline-none">
+ {search && (
+ <div className="-mx-1 -mt-1 mb-1 flex items-center overflow-hidden rounded-t-lg border-b border-neutral-200">
+ <Command.Input
+ placeholder="Search"
+ className="border-0 bg-transparent py-2 pl-4 pr-2 outline-none placeholder:text-neutral-400 focus:ring-0 sm:text-sm"
+ />
+ </div>
+ )}
+ <AnimatedSizeContainer height>
+ <div className="relative">
+ <Command.List
+ className="scrollbar-hide flex max-h-64 max-w-48 flex-col gap-1 overflow-y-auto transition-all"
+ ref={scrollRef}
+ onScroll={updateScrollProgress}
+ >
+ {items.map(({ icon, text, value, onSelect: itemOnSelect }) => (
+ <Command.Item
+ key={text}
+ value={`${text} ${value}`}
+ onSelect={() => {
+ itemOnSelect?.();
+ onSelect?.(value);
+ !isMultiSelect && setIsOpen(false);
+ }}
+ className="flex cursor-pointer items-center justify-between rounded-md px-1.5 py-1 transition-colors duration-150 data-[selected=true]:bg-neutral-100"
+ >
+ <div className="flex items-center gap-2">
+ {icon}
+ <span className="text-content-default pr-3 text-left text-sm font-medium">
+ {text}
+ </span>
+ </div>
+ {(Array.isArray(selectedValue)
+ ? selectedValue.includes(value)
+ : selectedValue === value) && (
+ <Check2 className="text-content-emphasis size-3.5 shrink-0" />
+ )}
+ </Command.Item>
+ ))}
+ </Command.List>
+ <div
+ className="pointer-events-none absolute bottom-0 left-0 hidden h-12 w-full rounded-b-lg bg-gradient-to-t from-white sm:block"
+ style={{ opacity: 1 - Math.pow(scrollProgress, 2) }}
+ />
+ </div>
+ </AnimatedSizeContainer>
+ </Command>
);
}
export const InlineBadgePopoverInput = forwardRef<HTMLInputElement>(
(props: HTMLProps<HTMLInputElement>, ref) => {
+ const { setIsOpen } = useContext(InlineBadgePopoverContext);
+
return (
<div className="relative rounded-md shadow-sm">
<input
@@ -124,6 +153,7 @@ export const InlineBadgePopoverInput = forwardRef<HTMLInputElement>(
"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) => e.key === "Enter" && setIsOpen(false)}
{...props}
/>
</div>
@@ -144,6 +174,7 @@ export const InlineBadgePopoverInputs = ({
valuesProp.map((value) => ({ id: uuid(), value })),
);
+ // Kinda nasty but allows the component to only receive/return the values array without needing external IDs
useEffect(() => {
const currentValues = values.map((item) => item.value);
if (JSON.stringify(currentValues) !== JSON.stringify(valuesProp)) {
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index 9cb7b4267c..1fa67532e9 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -20,7 +20,7 @@ import {
TooltipContent,
User,
} from "@dub/ui";
-import { capitalize, cn, truncate } from "@dub/utils";
+import { capitalize, cn, COUNTRIES, truncate } from "@dub/utils";
import { Command } from "cmdk";
import { Fragment, useState } from "react";
import { useFieldArray, useWatch } from "react-hook-form";
@@ -234,6 +234,9 @@ function ConditionLogic({
? { customer: User, sale: InvoiceDollar }[condition.entity]
: ArrowTurnRight2;
+ const isArrayValue =
+ condition.operator && ["in", "not_in"].includes(condition.operator);
+
return (
<>
<RewardIconSquare icon={icon} />
@@ -341,7 +344,43 @@ function ConditionLogic({
: !condition.value
}
>
- {["in", "not_in"].includes(condition.operator) ? (
+ {/* 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
@@ -358,6 +397,7 @@ function ConditionLogic({
}}
/>
) : (
+ // String input
<InlineBadgePopoverInput
{...register(`${conditionKey}.value`, { required: true })}
/>