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.

Embed link preview (#3523)

Kiran Kd1490ed403af
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx
index 7988b3da32..54a19c986e 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx
@@ -10,10 +10,11 @@ import {
Users,
useTable,
} from "@dub/ui";
-import { Pen2, Plus2 } from "@dub/ui/icons";
+import { ArrowTurnRight2, Pen2, Plus2 } from "@dub/ui/icons";
import {
currencyFormatter,
fetcher,
+ getApexDomain,
getPrettyUrl,
nFormatter,
} from "@dub/utils";
@@ -79,12 +80,38 @@ export function ReferralsEmbedLinksList({
group,
link: row.original,
});
+
+ const destinationUrl = row.original.url;
+
return (
- <div className="flex items-center gap-2">
- <CopyButton value={partnerLink} />
- <span className="text-sm" title={partnerLink}>
- {getPrettyUrl(partnerLink)}
- </span>
+ <div className="flex min-w-0 items-center gap-2">
+ <div className="border-border-subtle has-[:hover]:bg-bg-muted rounded-md border bg-white transition-colors dark:bg-black">
+ <CopyButton
+ value={partnerLink}
+ variant="neutral"
+ className="flex h-7 w-7 shrink-0 items-center justify-center rounded-md p-0 hover:bg-transparent active:bg-transparent"
+ />
+ </div>
+
+ <div className="flex min-w-0 flex-col">
+ <span
+ className="min-w-0 truncate text-xs font-medium text-neutral-900"
+ title={partnerLink}
+ >
+ {getPrettyUrl(partnerLink)}
+ </span>
+ <div className="flex min-w-0 max-w-[300px] items-center gap-1">
+ <ArrowTurnRight2 className="size-3 shrink-0 text-neutral-400" />
+ <a
+ className="min-w-0 cursor-alias truncate text-xs font-normal text-neutral-600 decoration-dotted underline-offset-2 hover:underline"
+ href={destinationUrl}
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ {destinationUrl ? getApexDomain(destinationUrl) : ""}
+ </a>
+ </div>
+ </div>
</div>
);
},
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx
index 3fd83e53dc..4e553845f5 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx
@@ -13,6 +13,7 @@ import { Partner, Program } from "@dub/prisma/client";
import {
Button,
Check,
+ Combobox,
Copy,
Directions,
Popover,
@@ -21,7 +22,9 @@ import {
useLocalStorage,
Wordmark,
} from "@dub/ui";
-import { cn, getPrettyUrl } from "@dub/utils";
+import { ArrowTurnRight2 } from "@dub/ui/icons";
+import { cn, getApexDomain, getPrettyUrl } from "@dub/utils";
+import { ChevronDown } from "lucide-react";
import { AnimatePresence } from "motion/react";
import { useEffect, useMemo, useState } from "react";
import { ReferralsEmbedActivity } from "./activity";
@@ -108,21 +111,12 @@ export function ReferralsEmbedPageClient({
[showQuickstart, group.additionalLinks, programEmbedData, hasResources],
);
- const [copied, copyToClipboard] = useCopyToClipboard();
const [selectedTab, setSelectedTab] = useState(tabs[0]);
useEffect(() => {
if (!tabs.includes(selectedTab)) setSelectedTab(tabs[0]);
}, [tabs, selectedTab]);
- const partnerLink =
- links.length > 0
- ? constructPartnerLink({
- group,
- link: links[0],
- })
- : undefined;
-
return (
<div
style={{ backgroundColor: themeOptions.backgroundColor || "transparent" }}
@@ -131,60 +125,12 @@ export function ReferralsEmbedPageClient({
<div className="relative z-0 p-5">
<div className="border-border-default relative flex flex-col overflow-hidden rounded-lg border p-4 md:p-6">
<HeroBackground logo={group.logo} color={group.brandColor} embed />
- <span className="text-content-emphasis text-base font-semibold">
- Referral link
- </span>
- <div className="xs:flex-row xs:items-center relative mt-3 flex flex-col gap-2 sm:max-w-[50%]">
- <input
- type="text"
- readOnly
- value={
- partnerLink ? getPrettyUrl(partnerLink) : "No referral link"
- }
- className="border-border-default text-content-default focus:border-border-emphasis bg-bg-default h-10 min-w-0 shrink grow rounded-md border px-3 text-sm focus:outline-none focus:ring-neutral-500"
- />
- {partnerLink ? (
- <Button
- icon={
- <div className="relative size-4">
- <div
- className={cn(
- "absolute inset-0 transition-[transform,opacity]",
- copied && "translate-y-1 opacity-0",
- )}
- >
- <Copy className="size-4" />
- </div>
- <div
- className={cn(
- "absolute inset-0 transition-[transform,opacity]",
- !copied && "translate-y-1 opacity-0",
- )}
- >
- <Check className="size-4" />
- </div>
- </div>
- }
- text={copied ? "Copied link" : "Copy link"}
- className="xs:w-fit"
- onClick={() => {
- copyToClipboard(partnerLink);
- }}
- />
- ) : (
- <Button
- text="Create a link"
- onClick={() => {
- setSelectedTab("Links");
- }}
- className="xs:w-fit"
- />
- )}
- </div>
- {partnerLink && group.linkStructure === "query" && (
- <QueryLinkStructureHelpText link={links[0]} />
- )}
+ <ReferralLinkDisplay
+ links={links}
+ group={group}
+ onSelectTab={setSelectedTab}
+ />
<div className="mt-12 sm:max-w-[50%]">
<div className="flex items-end justify-between">
@@ -292,6 +238,180 @@ export function ReferralsEmbedPageClient({
);
}
+function ReferralLinkDisplay({
+ links,
+ group,
+ onSelectTab,
+}: {
+ links: ReferralsEmbedLink[];
+ group: Pick<
+ PartnerGroupProps,
+ | "id"
+ | "logo"
+ | "wordmark"
+ | "brandColor"
+ | "additionalLinks"
+ | "maxPartnerLinks"
+ | "linkStructure"
+ | "holdingPeriodDays"
+ >;
+ onSelectTab: (tab: string) => void;
+}) {
+ const [copied, copyToClipboard] = useCopyToClipboard();
+
+ const [selectedLinkId, setSelectedLinkId] = useState<string | null>(
+ links[0]?.id ?? null,
+ );
+
+ const selectedLink = useMemo(
+ () => links.find((l) => l.id === selectedLinkId) ?? links[0],
+ [links, selectedLinkId],
+ );
+
+ const partnerLink = selectedLink
+ ? constructPartnerLink({ group, link: selectedLink })
+ : undefined;
+
+ const options = useMemo(
+ () =>
+ links.map((link) => ({
+ value: link.id,
+ label: getPrettyUrl(constructPartnerLink({ group, link })),
+ meta: {
+ destination: link.url ? getApexDomain(link.url) : null,
+ },
+ })),
+ [links, group],
+ );
+
+ const selectedOption =
+ selectedLink && partnerLink
+ ? {
+ value: selectedLink.id,
+ label: getPrettyUrl(partnerLink),
+ meta: {
+ destination: selectedLink.url
+ ? getApexDomain(selectedLink.url)
+ : null,
+ },
+ }
+ : null;
+
+ let actionButton: React.ReactNode = null;
+
+ if (partnerLink) {
+ actionButton = (
+ <Button
+ icon={
+ <div className="relative size-4">
+ <div
+ className={cn(
+ "absolute inset-0 transition-[transform,opacity]",
+ copied && "translate-y-1 opacity-0",
+ )}
+ >
+ <Copy className="size-4" />
+ </div>
+ <div
+ className={cn(
+ "absolute inset-0 transition-[transform,opacity]",
+ !copied && "translate-y-1 opacity-0",
+ )}
+ >
+ <Check className="size-4" />
+ </div>
+ </div>
+ }
+ text={copied ? "Copied link" : "Copy link"}
+ className="xs:w-fit"
+ onClick={() => copyToClipboard(partnerLink)}
+ />
+ );
+ } else if (links.length === 0) {
+ actionButton = (
+ <Button
+ text="Create a link"
+ onClick={() => onSelectTab("Links")}
+ className="xs:w-fit"
+ />
+ );
+ }
+
+ return (
+ <>
+ <span className="text-content-emphasis text-base font-semibold">
+ Referral link
+ </span>
+ <div className="xs:flex-row xs:items-center relative mt-3 flex flex-col gap-2 sm:max-w-[50%]">
+ {links.length <= 1 ? (
+ <>
+ <input
+ type="text"
+ readOnly
+ value={
+ partnerLink ? getPrettyUrl(partnerLink) : "No referral link"
+ }
+ className="border-border-default text-content-default focus:border-border-emphasis bg-bg-default h-10 min-w-0 shrink grow rounded-md border px-3 text-sm focus:outline-none focus:ring-neutral-500"
+ />
+ {actionButton}
+ </>
+ ) : (
+ <>
+ <Combobox
+ selected={selectedOption}
+ setSelected={(option) => {
+ if (!option) return;
+
+ setSelectedLinkId(option.value);
+
+ const link = links.find((l) => l.id === option.value);
+
+ if (link) {
+ copyToClipboard(constructPartnerLink({ group, link }));
+ }
+ }}
+ options={options}
+ forceDropdown
+ matchTriggerWidth
+ placeholder="No referral link"
+ inputClassName="text-sm h-10"
+ optionDescription={(option) => (
+ <span className="flex min-w-0 items-center gap-1">
+ <ArrowTurnRight2 className="size-3 shrink-0 text-neutral-400" />
+ <span className="min-w-0 truncate text-xs text-neutral-600">
+ {option.meta.destination}
+ </span>
+ </span>
+ )}
+ popoverProps={{
+ contentClassName: "rounded-lg border border-neutral-200 p-1",
+ }}
+ trigger={
+ <button
+ type="button"
+ className="border-border-default text-content-default focus:border-border-emphasis bg-bg-default flex h-10 min-w-0 shrink grow items-center gap-2 rounded-md border px-3 text-left text-sm outline-none focus:ring-neutral-500"
+ >
+ <span className="min-w-0 shrink grow truncate">
+ {partnerLink
+ ? getPrettyUrl(partnerLink)
+ : "No referral link"}
+ </span>
+ <ChevronDown className="size-4 shrink-0 text-neutral-400" />
+ </button>
+ }
+ />
+ {actionButton}
+ </>
+ )}
+ </div>
+
+ {partnerLink && group.linkStructure === "query" && (
+ <QueryLinkStructureHelpText link={selectedLink} />
+ )}
+ </>
+ );
+}
+
function Menu({
showQuickstart,
setShowQuickstart,
diff --git a/packages/ui/src/combobox/index.tsx b/packages/ui/src/combobox/index.tsx
index ad0a4867fc..9ee39690f5 100644
--- a/packages/ui/src/combobox/index.tsx
+++ b/packages/ui/src/combobox/index.tsx
@@ -73,6 +73,7 @@ export type ComboboxProps<
inputClassName?: string;
optionRight?: (option: ComboboxOption) => ReactNode;
optionClassName?: string;
+ optionDescription?: (option: ComboboxOption<TMeta>) => ReactNode;
matchTriggerWidth?: boolean;
hideSearch?: boolean;
forceDropdown?: boolean;
@@ -115,6 +116,7 @@ export function Combobox({
inputClassName,
optionRight,
optionClassName,
+ optionDescription,
matchTriggerWidth,
hideSearch = false,
forceDropdown = false,
@@ -318,6 +320,7 @@ export function Combobox({
selected.length >= maxSelected,
)}
right={optionRight?.(option)}
+ description={optionDescription?.(option)}
className={optionClassName}
/>
);
@@ -410,6 +413,7 @@ function Option({
selected,
disabled,
right,
+ description,
className,
}: {
option: ComboboxOption;
@@ -418,14 +422,17 @@ function Option({
selected: boolean;
disabled?: boolean;
right?: ReactNode;
+ description?: ReactNode;
className?: string;
}) {
+ const hasDescription = Boolean(description);
return (
<>
<DisabledTooltip disabledTooltip={option.disabledTooltip}>
<Command.Item
className={cn(
- "flex cursor-pointer items-center gap-3 whitespace-nowrap rounded-md px-3 py-2 text-left text-sm",
+ "flex cursor-pointer items-center gap-3 rounded-md px-3 py-2 text-left text-sm",
+ hasDescription ? "whitespace-normal py-2.5" : "whitespace-nowrap",
"data-[selected=true]:bg-neutral-100",
Boolean(disabled || option.disabledTooltip) &&
"cursor-not-allowed opacity-50",
@@ -444,7 +451,12 @@ function Option({
)}
</div>
)}
- <div className="flex min-w-0 grow items-center gap-2">
+ <div
+ className={cn(
+ "flex min-w-0 grow items-center gap-2",
+ hasDescription && "flex-col items-start gap-0.5",
+ )}
+ >
{option.icon && (
<span className="shrink-0 text-neutral-600">
{isReactNode(option.icon) ? (
@@ -454,7 +466,19 @@ function Option({
)}
</span>
)}
- <span className="grow truncate">{option.label}</span>
+ <span
+ className={cn(
+ "grow",
+ hasDescription
+ ? "text-neutral-900"
+ : "truncate text-neutral-700",
+ )}
+ >
+ {option.label}
+ </span>
+ {hasDescription && (
+ <span className="text-sm text-neutral-500">{description}</span>
+ )}
</div>
{right}
{!multiple && selected && (