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 conversion CTA

Steven Teyd75ff45d957b
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index 67573c409a..ad26ecd4e5 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -13,6 +13,7 @@ import {
Button,
CopyButton,
IconMenu,
+ Magic,
NumberTooltip,
Popover,
SimpleTooltipContent,
@@ -51,6 +52,7 @@ import { useParams, useSearchParams } from "next/navigation";
import { useEffect, useMemo, useRef, useState } from "react";
import { toast } from "sonner";
import useSWR, { mutate } from "swr";
+import { useConvertLinkModal } from "../modals/convert-link-modal";
import { useTransferLinkModal } from "../modals/transfer-link-modal";
import LinkLogo from "./link-logo";
@@ -66,6 +68,7 @@ export default function LinkCard({
key,
domain,
url,
+ trackConversion,
rewrite,
password,
expiresAt,
@@ -157,6 +160,9 @@ export default function LinkCard({
const expired = expiresAt && new Date(expiresAt) < new Date();
+ const { setShowConvertLinkModal, ConvertLinkModal } = useConvertLinkModal({
+ props,
+ });
const { setShowArchiveLinkModal, ArchiveLinkModal } = useArchiveLinkModal({
props,
});
@@ -235,6 +241,9 @@ export default function LinkCard({
case "q":
setShowLinkQRModal(true);
break;
+ case "c":
+ setShowConvertLinkModal(true);
+ break;
case "a":
setShowArchiveLinkModal(true);
break;
@@ -272,6 +281,7 @@ export default function LinkCard({
<LinkQRModal />
<AddEditLinkModal />
<DuplicateLinkModal />
+ <ConvertLinkModal />
<ArchiveLinkModal />
<TransferLinkModal />
<DeleteLinkModal />
@@ -479,133 +489,150 @@ export default function LinkCard({
</NumberTooltip>
<Popover
content={
- <div className="grid w-full gap-px p-2 sm:w-48">
- <Button
- text="Edit"
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowAddEditLinkModal(true);
- }}
- icon={<Edit3 className="h-4 w-4" />}
- shortcut="E"
- className="h-9 px-2 font-medium"
- />
- <Button
- text="Duplicate"
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowDuplicateLinkModal(true);
- }}
- icon={<CopyPlus className="h-4 w-4" />}
- shortcut="D"
- className="h-9 px-2 font-medium"
- />
- <Button
- text="QR Code"
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowLinkQRModal(true);
- }}
- icon={<QrCode className="h-4 w-4" />}
- shortcut="Q"
- className="h-9 px-2 font-medium"
- />
- <Button
- text={archived ? "Unarchive" : "Archive"}
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowArchiveLinkModal(true);
- }}
- icon={<Archive className="h-4 w-4" />}
- shortcut="A"
- className="h-9 px-2 font-medium"
- />
- <Button
- text="Transfer"
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowTransferLinkModal(true);
- }}
- icon={<FolderInput className="h-4 w-4" />}
- shortcut="T"
- className="h-9 px-2 font-medium"
- {...(!isDubDomain(domain) && {
- disabledTooltip: (
- <SimpleTooltipContent
- title="Since this is a custom domain link, you can only transfer it to another workspace if you transfer the domain as well."
- cta="Learn more."
- href="https://dub.co/help/article/how-to-transfer-domains"
- />
- ),
- })}
- />
- <Button
- text="Copy Link ID"
- variant="outline"
- onClick={() => copyLinkId()}
- icon={
- copiedLinkId ? (
- <CopyCheck className="h-4 w-4" />
- ) : (
- <Copy className="h-4 w-4" />
- )
- }
- shortcut="I"
- className="h-9 px-2 font-medium"
- />
- <Button
- text="Delete"
- variant="danger-outline"
- onClick={() => {
- setOpenPopover(false);
- setShowDeleteLinkModal(true);
- }}
- icon={<Delete className="h-4 w-4" />}
- shortcut="X"
- className="h-9 px-2 font-medium"
- />
- {!slug && ( // this is only shown in admin mode (where there's no slug)
- <button
+ <div className="w-full sm:w-48">
+ <div className="grid gap-px p-2">
+ <Button
+ text="Edit"
+ variant="outline"
onClick={() => {
- window.confirm(
- "Are you sure you want to ban this link? It will blacklist the domain and prevent any links from that domain from being created.",
- ) &&
- (setOpenPopover(false),
- toast.promise(
- fetch(`/api/admin/links/${id}/ban`, {
- method: "DELETE",
- }).then(async () => {
- await mutate(
- (key) =>
- typeof key === "string" &&
- key.startsWith("/api/admin/links"),
- undefined,
- { revalidate: true },
- );
- }),
- {
- loading: "Banning link...",
- success: "Link banned!",
- error: "Error banning link.",
- },
- ));
+ setOpenPopover(false);
+ setShowAddEditLinkModal(true);
}}
- className="group flex w-full items-center justify-between rounded-md p-2 text-left text-sm font-medium text-red-600 transition-all duration-75 hover:bg-red-600 hover:text-white"
- >
- <IconMenu
- text="Ban"
- icon={<Delete className="h-4 w-4" />}
- />
- <kbd className="hidden rounded bg-red-100 px-2 py-0.5 text-xs font-light text-red-600 transition-all duration-75 group-hover:bg-red-500 group-hover:text-white sm:inline-block">
- B
- </kbd>
- </button>
- )}
+ icon={<Edit3 className="h-4 w-4" />}
+ shortcut="E"
+ className="h-9 px-2 font-medium"
+ />
+ <Button
+ text="Duplicate"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowDuplicateLinkModal(true);
+ }}
+ icon={<CopyPlus className="h-4 w-4" />}
+ shortcut="D"
+ className="h-9 px-2 font-medium"
+ />
+ <Button
+ text="QR Code"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowLinkQRModal(true);
+ }}
+ icon={<QrCode className="h-4 w-4" />}
+ shortcut="Q"
+ className="h-9 px-2 font-medium"
+ />
+ <Button
+ text="Copy Link ID"
+ variant="outline"
+ onClick={() => copyLinkId()}
+ icon={
+ copiedLinkId ? (
+ <CopyCheck className="h-4 w-4" />
+ ) : (
+ <Copy className="h-4 w-4" />
+ )
+ }
+ shortcut="I"
+ className="h-9 px-2 font-medium"
+ />
+ </div>
+ <div className="grid border-y border-gray-200 p-2">
+ <Button
+ text="Conversions"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowConvertLinkModal(true);
+ }}
+ icon={<Magic className="h-4 w-4" />}
+ shortcut="C"
+ className="h-9 px-2 font-medium"
+ />
+ </div>
+ <div className="grid gap-px p-2">
+ <Button
+ text={archived ? "Unarchive" : "Archive"}
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowArchiveLinkModal(true);
+ }}
+ icon={<Archive className="h-4 w-4" />}
+ shortcut="A"
+ className="h-9 px-2 font-medium"
+ />
+ <Button
+ text="Transfer"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowTransferLinkModal(true);
+ }}
+ icon={<FolderInput className="h-4 w-4" />}
+ shortcut="T"
+ className="h-9 px-2 font-medium"
+ {...(!isDubDomain(domain) && {
+ disabledTooltip: (
+ <SimpleTooltipContent
+ title="Since this is a custom domain link, you can only transfer it to another workspace if you transfer the domain as well."
+ cta="Learn more."
+ href="https://dub.co/help/article/how-to-transfer-domains"
+ />
+ ),
+ })}
+ />
+ <Button
+ text="Delete"
+ variant="danger-outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowDeleteLinkModal(true);
+ }}
+ icon={<Delete className="h-4 w-4" />}
+ shortcut="X"
+ className="h-9 px-2 font-medium"
+ />
+ {!slug && ( // this is only shown in admin mode (where there's no slug)
+ <button
+ onClick={() => {
+ window.confirm(
+ "Are you sure you want to ban this link? It will blacklist the domain and prevent any links from that domain from being created.",
+ ) &&
+ (setOpenPopover(false),
+ toast.promise(
+ fetch(`/api/admin/links/${id}/ban`, {
+ method: "DELETE",
+ }).then(async () => {
+ await mutate(
+ (key) =>
+ typeof key === "string" &&
+ key.startsWith("/api/admin/links"),
+ undefined,
+ { revalidate: true },
+ );
+ }),
+ {
+ loading: "Banning link...",
+ success: "Link banned!",
+ error: "Error banning link.",
+ },
+ ));
+ }}
+ className="group flex w-full items-center justify-between rounded-md p-2 text-left text-sm font-medium text-red-600 transition-all duration-75 hover:bg-red-600 hover:text-white"
+ >
+ <IconMenu
+ text="Ban"
+ icon={<Delete className="h-4 w-4" />}
+ />
+ <kbd className="hidden rounded bg-red-100 px-2 py-0.5 text-xs font-light text-red-600 transition-all duration-75 group-hover:bg-red-500 group-hover:text-white sm:inline-block">
+ B
+ </kbd>
+ </button>
+ )}
+ </div>
</div>
}
align="end"
diff --git a/apps/web/ui/modals/convert-link-modal.tsx b/apps/web/ui/modals/convert-link-modal.tsx
new file mode 100644
index 0000000000..03a1d63cfb
--- /dev/null
+++ b/apps/web/ui/modals/convert-link-modal.tsx
@@ -0,0 +1,162 @@
+import useWorkspace from "@/lib/swr/use-workspace";
+import { LinkProps } from "@/lib/types";
+import { Button, Modal, useToastWithUndo } from "@dub/ui";
+import { getApexDomain, linkConstructor } from "@dub/utils";
+import {
+ Dispatch,
+ MouseEvent,
+ SetStateAction,
+ useCallback,
+ useMemo,
+ useState,
+} from "react";
+import { toast } from "sonner";
+import { mutate } from "swr";
+import LinkLogo from "../links/link-logo";
+
+const sendConvertRequest = ({
+ linkId,
+ trackConversion,
+ workspaceId,
+}: {
+ linkId: string;
+ trackConversion: boolean;
+ workspaceId?: string;
+}) => {
+ return fetch(`/api/links/${linkId}?workspaceId=${workspaceId}`, {
+ method: "PATCH",
+ body: JSON.stringify({ trackConversion }),
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+};
+
+const revalidateLinks = () => {
+ return mutate(
+ (key) => typeof key === "string" && key.startsWith("/api/links"),
+ undefined,
+ { revalidate: true },
+ );
+};
+
+function ConvertLinkModal({
+ showConvertLinkModal,
+ setShowConvertLinkModal,
+ props,
+}: {
+ showConvertLinkModal: boolean;
+ setShowConvertLinkModal: Dispatch<SetStateAction<boolean>>;
+ props: LinkProps;
+}) {
+ const toastWithUndo = useToastWithUndo();
+
+ const { id: workspaceId } = useWorkspace();
+ const [archiving, setArchiving] = useState(false);
+ const apexDomain = getApexDomain(props.url);
+
+ const { key, domain } = props;
+
+ const shortlink = useMemo(() => {
+ return linkConstructor({
+ key,
+ domain,
+ pretty: true,
+ });
+ }, [key, domain]);
+
+ const handleConvertRequest = async (event: MouseEvent<HTMLButtonElement>) => {
+ event.preventDefault();
+
+ setArchiving(true);
+ const res = await sendConvertRequest({
+ linkId: props.id,
+ trackConversion: !props.trackConversion,
+ workspaceId,
+ });
+ setArchiving(false);
+
+ if (!res.ok) {
+ const { error } = await res.json();
+ toast.error(error.message);
+ return;
+ }
+
+ revalidateLinks();
+ setShowConvertLinkModal(false);
+ toastWithUndo({
+ id: "undo-convert-link-toast",
+ message: `Successfully ${props.trackConversion ? "disabled" : "enable"} conversion tracking for link!`,
+ undo: undoAction,
+ duration: 5000,
+ });
+ };
+
+ const undoAction = () => {
+ toast.promise(
+ sendConvertRequest({
+ linkId: props.id,
+ trackConversion: props.trackConversion,
+ workspaceId,
+ }),
+ {
+ loading: "Undo in progress...",
+ error: "Failed to roll back changes. An error occurred.",
+ success: () => {
+ revalidateLinks();
+ return "Undo successful! Changes reverted.";
+ },
+ },
+ );
+ };
+
+ return (
+ <Modal
+ showModal={showConvertLinkModal}
+ setShowModal={setShowConvertLinkModal}
+ >
+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
+ <LinkLogo apexDomain={apexDomain} />
+ <h3 className="text-lg font-medium">
+ {props.trackConversion ? "Disable" : "Enable"} Conversion Tracking
+ </h3>
+ <p className="text-sm text-gray-500">
+ {props.trackConversion
+ ? "By disabling conversion tracking, you will no longer be able to track link conversions."
+ : "By enabling conversion tracking, you will be able to track link conversions."}
+ </p>
+ </div>
+
+ <div className="flex flex-col space-y-6 bg-gray-50 px-4 py-8 text-left sm:px-16">
+ <Button
+ onClick={handleConvertRequest}
+ autoFocus
+ loading={archiving}
+ text={`Confirm ${props.trackConversion ? "Disable" : "Enable"}`}
+ />
+ </div>
+ </Modal>
+ );
+}
+
+export function useConvertLinkModal({ props }: { props: LinkProps }) {
+ const [showConvertLinkModal, setShowConvertLinkModal] = useState(false);
+
+ const ConvertLinkModalCallback = useCallback(() => {
+ return props ? (
+ <ConvertLinkModal
+ showConvertLinkModal={showConvertLinkModal}
+ setShowConvertLinkModal={setShowConvertLinkModal}
+ props={props}
+ />
+ ) : null;
+ }, [showConvertLinkModal, setShowConvertLinkModal]);
+
+ return useMemo(
+ () => ({
+ setShowConvertLinkModal,
+ ConvertLinkModal: ConvertLinkModalCallback,
+ }),
+ [setShowConvertLinkModal, ConvertLinkModalCallback],
+ );
+}
diff --git a/packages/ui/src/hooks/use-toast-with-undo.tsx b/packages/ui/src/hooks/use-toast-with-undo.tsx
index 4f4f0233b9..7d826e45d8 100644
--- a/packages/ui/src/hooks/use-toast-with-undo.tsx
+++ b/packages/ui/src/hooks/use-toast-with-undo.tsx
@@ -34,7 +34,7 @@ function ToastWithUndo({
<p className="font-medium">{message}</p>
</div>
<button
- className="rounded border border-black bg-black px-2 py-1 text-xs font-normal text-white transition-all duration-75 active:scale-95"
+ className="whitespace-nowrap rounded border border-black bg-black px-2 py-1 text-xs font-normal text-white transition-all duration-75 active:scale-95"
onClick={undoAndDismiss}
>
Undo