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 link controls

Tim Wilson5908941d9c12
diff --git a/apps/web/ui/links/link-controls.tsx b/apps/web/ui/links/link-controls.tsx
new file mode 100644
index 0000000000..9485dbca51
--- /dev/null
+++ b/apps/web/ui/links/link-controls.tsx
@@ -0,0 +1,258 @@
+import { useAddEditLinkModal } from "@/ui/modals/add-edit-link-modal";
+import { useArchiveLinkModal } from "@/ui/modals/archive-link-modal";
+import { useDeleteLinkModal } from "@/ui/modals/delete-link-modal";
+import { useLinkQRModal } from "@/ui/modals/link-qr-modal";
+import {
+ Button,
+ IconMenu,
+ PenWriting,
+ Popover,
+ SimpleTooltipContent,
+ useIntersectionObserver,
+} from "@dub/ui";
+import { CircleCheck, Copy, QRCode } from "@dub/ui/src/icons";
+import { cn, isDubDomain, nanoid, punycode } from "@dub/utils";
+import { Archive, CopyPlus, Delete, FolderInput } from "lucide-react";
+import { useParams } from "next/navigation";
+import { useRef, useState } from "react";
+import { toast } from "sonner";
+import { mutate } from "swr";
+import { useTransferLinkModal } from "../modals/transfer-link-modal";
+import { ThreeDots } from "../shared/icons";
+import { ResponseLink } from "./links-container";
+
+export function LinkControls({ link }: { link: ResponseLink }) {
+ const { slug } = useParams() as { slug?: string };
+
+ const ref = useRef<HTMLDivElement>(null);
+ const entry = useIntersectionObserver(ref, {});
+ const isVisible = !!entry?.isIntersecting;
+
+ const [openPopover, setOpenPopover] = useState(false);
+ const [copiedLinkId, setCopiedLinkId] = useState(false);
+
+ const copyLinkId = () => {
+ navigator.clipboard.writeText(link.id);
+ setCopiedLinkId(true);
+ toast.success("Link ID copied!");
+ setTimeout(() => setCopiedLinkId(false), 3000);
+ };
+
+ const { setShowArchiveLinkModal, ArchiveLinkModal } = useArchiveLinkModal({
+ props: link,
+ });
+ const { setShowTransferLinkModal, TransferLinkModal } = useTransferLinkModal({
+ props: link,
+ });
+ const { setShowDeleteLinkModal, DeleteLinkModal } = useDeleteLinkModal({
+ props: link,
+ });
+ const { setShowLinkQRModal, LinkQRModal } = useLinkQRModal({
+ props: link,
+ });
+ const { setShowAddEditLinkModal, AddEditLinkModal } = useAddEditLinkModal({
+ props: link,
+ });
+
+ // Duplicate link Modal
+ const {
+ id: _,
+ createdAt: __,
+ updatedAt: ___,
+ userId: ____,
+ ...propsToDuplicate
+ } = link;
+ const {
+ setShowAddEditLinkModal: setShowDuplicateLinkModal,
+ AddEditLinkModal: DuplicateLinkModal,
+ } = useAddEditLinkModal({
+ // @ts-expect-error
+ duplicateProps: {
+ ...propsToDuplicate,
+ key: link.key === "_root" ? nanoid(7) : `${punycode(link.key)}-copy`,
+ clicks: 0,
+ },
+ });
+
+ return (
+ <div
+ ref={ref}
+ className={cn(
+ "flex items-center justify-end divide-x divide-gray-200 overflow-hidden rounded-md border border-gray-200",
+ "sm:divide-transparent sm:border-transparent sm:group-hover/row:divide-gray-200 sm:group-hover/row:border-gray-200", // Only show divider on mobile or when hovered
+ "w-[35px] transition-[width] duration-300 sm:group-hover/row:w-[100px]",
+ )}
+ >
+ {isVisible && (
+ <>
+ <LinkQRModal />
+ <AddEditLinkModal />
+ <DuplicateLinkModal />
+ <ArchiveLinkModal />
+ <TransferLinkModal />
+ <DeleteLinkModal />
+ </>
+ )}
+ <Button
+ icon={<PenWriting className={cn("h-4 w-4 shrink-0")} />}
+ variant="outline"
+ className="h-8 rounded-none border-0 px-2"
+ onClick={() => setShowAddEditLinkModal(true)}
+ />
+ <Button
+ icon={<QRCode className={cn("h-4 w-4 shrink-0")} />}
+ variant="outline"
+ className="h-8 rounded-none border-0 px-2"
+ onClick={() => setShowLinkQRModal(true)}
+ />
+ <Popover
+ content={
+ <div className="w-full sm:w-48">
+ <div className="grid gap-px p-2">
+ <Button
+ text="Edit"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowAddEditLinkModal(true);
+ }}
+ icon={<PenWriting className="h-4 w-4" />}
+ shortcut="E"
+ 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="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="Copy Link ID"
+ variant="outline"
+ onClick={() => copyLinkId()}
+ icon={
+ copiedLinkId ? (
+ <CircleCheck className="h-4 w-4" />
+ ) : (
+ <Copy className="h-4 w-4" />
+ )
+ }
+ shortcut="I"
+ className="h-9 px-2 font-medium"
+ />
+ </div>
+ <div className="border-t border-gray-200" />
+ <div className="grid gap-px p-2">
+ <Button
+ text={link.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(link.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"
+ />
+ ),
+ })}
+ />
+ {link.key !== "_root" && (
+ <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/ban?key=${link.key}`, {
+ method: "DELETE",
+ }).then(async () => {
+ await mutate(
+ (key) =>
+ typeof key === "string" &&
+ key.startsWith("/api/admin/links/ban"),
+ 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"
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ >
+ <Button
+ variant="outline"
+ className="h-8 rounded-none border-0 px-1.5 transition-[border-color] duration-200"
+ icon={<ThreeDots className="h-5 w-5 shrink-0" />}
+ onClick={() => {
+ setOpenPopover(!openPopover);
+ }}
+ />
+ </Popover>
+ </div>
+ );
+}
diff --git a/apps/web/ui/links/link-details-column.tsx b/apps/web/ui/links/link-details-column.tsx
index 62c77a7922..924923583d 100644
--- a/apps/web/ui/links/link-details-column.tsx
+++ b/apps/web/ui/links/link-details-column.tsx
@@ -7,22 +7,20 @@ import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { useMemo, useRef } from "react";
import useSWR from "swr";
+import { LinkControls } from "./link-controls";
import { ResponseLink } from "./links-container";
import TagBadge from "./tag-badge";
-export function LinkDetailsColumn({
- link: { id, domain, key, tags, trackConversion, ...link },
-}: {
- link: ResponseLink;
-}) {
- const { primaryTags, additionalTags } = useOrganizedTags(tags);
-
- const { id: workspaceId, slug, exceededClicks } = useWorkspace();
+export function LinkDetailsColumn({ link }: { link: ResponseLink }) {
+ const { id, domain, key, tags, trackConversion } = link;
const ref = useRef<HTMLDivElement>(null);
const entry = useIntersectionObserver(ref, {});
const isVisible = !!entry?.isIntersecting;
+ const { id: workspaceId, slug, exceededClicks } = useWorkspace();
+ const { primaryTags, additionalTags } = useOrganizedTags(tags);
+
const { data: totalEvents } = useSWR<{
[key in CompositeAnalyticsResponseOptions]?: number;
}>(
@@ -68,6 +66,9 @@ export function LinkDetailsColumn({
</div>
))}
</Link>
+ <div className="ml-2">
+ <LinkControls link={link} />
+ </div>
</div>
);
}
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index 8db61021b7..0ed3d86645 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -70,7 +70,6 @@ function LinksList({
<LinkTitleColumn link={getValue() as ResponseLink} />
),
enableHiding: false,
- size: 150,
},
{
id: "details",
@@ -78,7 +77,6 @@ function LinksList({
cell: ({ getValue }) => (
<LinkDetailsColumn link={getValue() as ResponseLink} />
),
- size: 50,
},
],
[],
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index 84401f2a7e..b0e94c52f1 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -83,7 +83,7 @@ const tableCellVariants = cva(
},
);
-const tableRowVariants = cva("", {
+const tableRowVariants = cva("group/row", {
variants: {
variant: {
...variantDefaults,