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.

Partner codes

Marcus Farrell8ea737b0bca1
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/discount-codes.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/discount-codes.tsx
index e96f984929..0dfee414c4 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/discount-codes.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/discount-codes.tsx
@@ -358,7 +358,7 @@ function DiscountCodeCard({
innerClassName="flex items-center justify-between gap-4 px-3 py-2.5"
hoverStateEnabled={false}
>
- <div className="flex min-w-0 flex-1 items-center">
+ <div className="flex min-w-0 flex-1 items-center gap-3">
<div className="w-40 shrink-0">
<DiscountCodeBadge
code={discountCode.code}
diff --git a/apps/web/ui/modals/add-discount-code-modal.tsx b/apps/web/ui/modals/add-discount-code-modal.tsx
index 2877783d40..d3969f351e 100644
--- a/apps/web/ui/modals/add-discount-code-modal.tsx
+++ b/apps/web/ui/modals/add-discount-code-modal.tsx
@@ -10,6 +10,7 @@ import {
Modal,
useCopyToClipboard,
} from "@dub/ui";
+import { CircleWarning, TriangleWarning } from "@dub/ui/icons";
import { cn, getPrettyUrl } from "@dub/utils";
import { Tag } from "lucide-react";
import { useCallback, useMemo, useRef, useState } from "react";
@@ -24,6 +25,15 @@ import { UpgradeRequiredToast } from "../shared/upgrade-required-toast";
type FormData = z.infer<typeof createDiscountCodeSchema>;
+// Mirrors the createDiscountCodeSchema format: spaces become dashes,
+// anything other than letters, numbers, dashes, and underscores is dropped
+const sanitizeDiscountCode = (code: string) =>
+ code
+ .trim()
+ .replace(/\s+/g, "-")
+ .replace(/[^a-zA-Z0-9\-_]/g, "")
+ .replace(/-{2,}/g, "-");
+
interface AddDiscountCodeModalProps {
showModal: boolean;
setShowModal: (showModal: boolean) => void;
@@ -50,7 +60,10 @@ const AddDiscountCodeModal = ({
},
});
- const [linkId] = watch(["linkId"]);
+ const [linkId, code] = watch(["linkId", "code"]);
+
+ const sanitizedCode = sanitizeDiscountCode(code ?? "");
+ const codeWillChange = !!code && sanitizedCode !== code.trim();
// Get partner links for the dropdown
const partnerLinks = partner.links || [];
@@ -79,6 +92,7 @@ const AddDiscountCodeModal = ({
method: "POST",
body: {
...formData,
+ code: sanitizeDiscountCode(formData.code ?? ""),
partnerId: partner.id,
},
onSuccess: async (data) => {
@@ -206,6 +220,23 @@ const AddDiscountCodeModal = ({
placeholder={partner.name.split(" ")[0].toUpperCase()}
/>
</div>
+ {codeWillChange &&
+ (sanitizedCode ? (
+ <p className="flex items-center gap-1.5 text-xs text-neutral-500">
+ <TriangleWarning className="size-3.5 shrink-0 text-amber-500" />
+ <span className="min-w-0">
+ Will be created as{" "}
+ <code className="break-all rounded-md bg-neutral-100 px-1.5 py-0.5 font-mono text-neutral-800">
+ {sanitizedCode}
+ </code>
+ </span>
+ </p>
+ ) : (
+ <p className="flex items-center gap-1.5 text-xs text-red-600">
+ <CircleWarning className="size-3.5 shrink-0" />
+ Use letters, numbers, dashes, or underscores
+ </p>
+ ))}
<p className="text-xs text-neutral-500">
Discount codes cannot be edited after creation
</p>
@@ -226,7 +257,7 @@ const AddDiscountCodeModal = ({
}
className="h-8 w-fit pl-2.5 pr-1.5"
loading={isSubmitting}
- disabled={!linkId}
+ disabled={!linkId || (codeWillChange && !sanitizedCode)}
/>
</div>
</form>
diff --git a/apps/web/ui/partners/discounts/discount-code-badge.tsx b/apps/web/ui/partners/discounts/discount-code-badge.tsx
index e91310f4ae..4faa833e93 100644
--- a/apps/web/ui/partners/discounts/discount-code-badge.tsx
+++ b/apps/web/ui/partners/discounts/discount-code-badge.tsx
@@ -1,5 +1,6 @@
import { Tag, Tooltip, useCopyToClipboard } from "@dub/ui";
import { cn } from "@dub/utils";
+import { useRef, useState } from "react";
import { toast } from "sonner";
export function DiscountCodeBadge({
@@ -14,18 +15,27 @@ export function DiscountCodeBadge({
const [copied, copyToClipboard] = useCopyToClipboard();
const isDisabled = !!disabledAt;
+ // Only show the full code in a tooltip when it's actually truncated
+ const codeRef = useRef<HTMLDivElement>(null);
+ const [isClipped, setIsClipped] = useState(false);
+ const measureClipped = () => {
+ const el = codeRef.current;
+ setIsClipped(!!el && el.scrollWidth > el.clientWidth);
+ };
+
const content = (
<>
<Tag
className={cn(
- "size-3",
+ "size-3 shrink-0",
isDisabled ? "text-neutral-500" : "text-green-700",
)}
strokeWidth={1.5}
/>
<div
+ ref={codeRef}
className={cn(
- "text-xs font-medium",
+ "min-w-0 truncate text-xs font-medium",
isDisabled
? "text-neutral-500 line-through"
: "text-green-700 decoration-dotted underline-offset-2 transition-colors group-hover/discountcode:underline",
@@ -38,8 +48,16 @@ export function DiscountCodeBadge({
if (isDisabled) {
return (
- <Tooltip content={disabledTooltip}>
- <div className="flex w-fit cursor-help items-center gap-1 rounded-lg bg-neutral-100 px-2 py-1">
+ <Tooltip
+ content={
+ isClipped ? `\`${code}\`\n\n${disabledTooltip}` : disabledTooltip
+ }
+ >
+ <div
+ className="flex w-fit max-w-full cursor-help items-center gap-1 rounded-lg bg-neutral-100 px-2 py-1"
+ onPointerEnter={measureClipped}
+ onFocus={measureClipped}
+ >
{content}
</div>
</Tooltip>
@@ -47,22 +65,33 @@ export function DiscountCodeBadge({
}
return (
- <button
- type="button"
- className={cn(
- "group/discountcode relative flex w-fit cursor-copy items-center gap-1 rounded-lg bg-green-200 px-2 py-1",
- "transition-colors duration-150 hover:bg-green-300/80",
- copied && "cursor-default",
- )}
- onClick={() =>
- copyToClipboard(code, {
- onSuccess: () => {
- toast.success("Copied discount code to clipboard");
- },
- })
+ <Tooltip
+ disabled={!isClipped}
+ content={
+ <div className="max-w-xs break-all px-3 py-2 text-sm text-neutral-600">
+ {code}
+ </div>
}
>
- {content}
- </button>
+ <button
+ type="button"
+ className={cn(
+ "group/discountcode relative flex w-fit max-w-full cursor-copy items-center gap-1 rounded-lg bg-green-200 px-2 py-1",
+ "transition-colors duration-150 hover:bg-green-300/80",
+ copied && "cursor-default",
+ )}
+ onPointerEnter={measureClipped}
+ onFocus={measureClipped}
+ onClick={() =>
+ copyToClipboard(code, {
+ onSuccess: () => {
+ toast.success("Copied discount code to clipboard");
+ },
+ })
+ }
+ >
+ {content}
+ </button>
+ </Tooltip>
);
}