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.

11.1 KB
Raw Blame
import useCurrentFolderId from "@/lib/swr/use-current-folder-id";
import { useCheckFolderPermission } from "@/lib/swr/use-folder-permissions";
import { ExpandedLinkProps } from "@/lib/types";
import { useArchiveLinkModal } from "@/ui/modals/archive-link-modal";
import { useDeleteLinkModal } from "@/ui/modals/delete-link-modal";
import {
  Button,
  PenWriting,
  Popover,
  useCopyToClipboard,
  useKeyboardShortcut,
} from "@dub/ui";
import {
  BoxArchive,
  CircleCheck,
  Copy,
  FolderBookmark,
  QRCode,
  Trash,
} from "@dub/ui/icons";
import { cn, isDubDomain } from "@dub/utils";
import { CopyPlus, FolderInput } from "lucide-react";
import { useParams, useRouter } from "next/navigation";
import { useCallback } from "react";
import { toast } from "sonner";
import { useLinkBuilder } from "../modals/link-builder";
import { useLinkQRModal } from "../modals/link-qr-modal";
import { useMoveLinkToFolderModal } from "../modals/move-link-to-folder-modal";
import { useTransferLinkModal } from "../modals/transfer-link-modal";
import { ThreeDots } from "../shared/icons";

const OPTIONS = {
  edit: "e",
  qr: "q",
  duplicate: "d",
  id: "i",
  move: "m",
  archive: "a",
  transfer: "t",
  delete: "x",
};

export function LinkControls({
  link,
  openPopover,
  setOpenPopover,
  shortcutsEnabled,
  options = Object.keys(OPTIONS),
  onMoveSuccess,
  onTransferSuccess,
  onDeleteSuccess,
  className,
  iconClassName,
}: {
  link: ExpandedLinkProps;
  openPopover: boolean;
  setOpenPopover: (open: boolean) => void;
  shortcutsEnabled: boolean;
  options?: string[];
  onMoveSuccess?: (folderId: string | null) => void;
  onTransferSuccess?: () => void;
  onDeleteSuccess?: () => void;
  className?: string;
  iconClassName?: string;
}) {
  const router = useRouter();
  const { slug } = useParams() as { slug?: string };

  const [copiedLinkId, copyToClipboard] = useCopyToClipboard();

  const copyLinkId = () => {
    toast.promise(copyToClipboard(link.id), {
      success: "Link ID copied!",
    });
  };

  const openLinkBuilder = useCallback(() => {
    router.push(`/${slug}/links/${link.domain}/${link.key}`);
  }, [router, slug, link.domain, link.key]);

  const { setShowArchiveLinkModal, ArchiveLinkModal } = useArchiveLinkModal({
    props: link,
  });
  const { setShowTransferLinkModal, TransferLinkModal } = useTransferLinkModal({
    props: link,
    onSuccess: onTransferSuccess,
  });
  const { setShowDeleteLinkModal, DeleteLinkModal } = useDeleteLinkModal({
    props: link,
    onSuccess: onDeleteSuccess,
  });
  const { setShowLinkQRModal, LinkQRModal } = useLinkQRModal({
    props: link,
  });
  const { setShowMoveLinkToFolderModal, MoveLinkToFolderModal } =
    useMoveLinkToFolderModal({ link, onSuccess: onMoveSuccess });

  const isRootLink = link.key === "_root";
  const { folderId: currentFolderId } = useCurrentFolderId();
  const folderId = link.folderId || currentFolderId;

  // Duplicate link Modal
  const {
    id: _,
    createdAt: __,
    updatedAt: ___,
    userId: ____, // don't duplicate userId since the current user can be different
    externalId: _____, // don't duplicate externalId since it should be unique
    ...propsToDuplicate
  } = link;
  const {
    setShowLinkBuilder: setShowDuplicateLinkModal,
    LinkBuilder: DuplicateLinkModal,
  } = useLinkBuilder({
    // @ts-expect-error
    duplicateProps: {
      ...propsToDuplicate,
      key: propsToDuplicate.key + "-copy",
      clicks: 0,
    },
  });

  const canManageLink = useCheckFolderPermission(
    folderId,
    "folders.links.write",
  );

  useKeyboardShortcut(
    options.map((o) => OPTIONS[o]),
    (_, key) => {
      setOpenPopover(false);
      switch (key) {
        case "e":
          canManageLink && openLinkBuilder();
          break;
        case "d":
          canManageLink && setShowDuplicateLinkModal(true);
          break;
        case "q":
          setShowLinkQRModal(true);
          break;
        case "m":
          canManageLink && setShowMoveLinkToFolderModal(true);
          break;
        case "a":
          canManageLink && setShowArchiveLinkModal(true);
          break;
        case "t":
          canManageLink &&
            isDubDomain(link.domain) &&
            setShowTransferLinkModal(true);
          break;
        case "i":
          copyLinkId();
          break;
        case "x":
          canManageLink && !isRootLink && setShowDeleteLinkModal(true);
          break;
      }
    },
    {
      enabled: shortcutsEnabled,
      priority: 1, // Take priority over display options
    },
  );

  return (
    <div className="flex justify-end">
      {options.includes("qr") && <LinkQRModal />}
      {options.includes("duplicate") && <DuplicateLinkModal />}
      {options.includes("archive") && <ArchiveLinkModal />}
      {options.includes("transfer") && <TransferLinkModal />}
      {options.includes("delete") && <DeleteLinkModal />}
      {options.includes("move") && <MoveLinkToFolderModal />}
      <Popover
        content={
          <div className="w-full sm:w-48">
            <div className="grid gap-px p-2">
              {options.includes("edit") && (
                <Button
                  text="Edit"
                  variant="outline"
                  onClick={() => {
                    setOpenPopover(false);
                    openLinkBuilder();
                  }}
                  icon={<PenWriting className="size-4" />}
                  shortcut="E"
                  className="h-9 px-2 font-medium"
                  disabledTooltip={
                    !canManageLink
                      ? "You don't have permission to update this link."
                      : undefined
                  }
                />
              )}
              {options.includes("qr") && (
                <Button
                  text="QR Code"
                  variant="outline"
                  onClick={() => {
                    setOpenPopover(false);
                    setShowLinkQRModal(true);
                  }}
                  icon={<QRCode className="size-4" />}
                  shortcut="Q"
                  className="h-9 px-2 font-medium"
                />
              )}
              {options.includes("id") && (
                <Button
                  text="Copy Link ID"
                  variant="outline"
                  onClick={() => copyLinkId()}
                  icon={
                    copiedLinkId ? (
                      <CircleCheck className="size-4" />
                    ) : (
                      <Copy className="size-4" />
                    )
                  }
                  shortcut="I"
                  className="h-9 px-2 font-medium"
                />
              )}
              {options.includes("duplicate") && (
                <Button
                  text="Duplicate"
                  variant="outline"
                  onClick={() => {
                    setOpenPopover(false);
                    setShowDuplicateLinkModal(true);
                  }}
                  icon={<CopyPlus className="size-4" />}
                  shortcut="D"
                  className="h-9 px-2 font-medium"
                  disabledTooltip={
                    !canManageLink
                      ? "You don't have permission to duplicate this link."
                      : undefined
                  }
                />
              )}
            </div>
            <div className="border-t border-neutral-200" />
            <div className="grid gap-px p-2">
              {options.includes("move") && (
                <Button
                  text="Move"
                  variant="outline"
                  shortcut="M"
                  className="h-9 px-2 font-medium"
                  icon={<FolderBookmark className="size-4 text-neutral-600" />}
                  onClick={() => {
                    setOpenPopover(false);
                    setShowMoveLinkToFolderModal(true);
                  }}
                  disabledTooltip={
                    !canManageLink
                      ? "You don't have permission to move this link to another folder."
                      : undefined
                  }
                />
              )}
              {options.includes("archive") && (
                <Button
                  text={link.archived ? "Unarchive" : "Archive"}
                  variant="outline"
                  onClick={() => {
                    setOpenPopover(false);
                    setShowArchiveLinkModal(true);
                  }}
                  icon={<BoxArchive className="size-4" />}
                  shortcut="A"
                  className="h-9 px-2 font-medium"
                  disabledTooltip={
                    !canManageLink
                      ? "You don't have permission to archive this link."
                      : undefined
                  }
                />
              )}
              {options.includes("transfer") && (
                <Button
                  text="Transfer"
                  variant="outline"
                  onClick={() => {
                    setOpenPopover(false);
                    setShowTransferLinkModal(true);
                  }}
                  icon={<FolderInput className="size-4" />}
                  shortcut="T"
                  className="h-9 px-2 font-medium"
                  disabledTooltip={
                    !isDubDomain(link.domain)
                      ? "Since this is a custom domain link, you can only transfer it to another workspace if you transfer the domain as well. [Learn more.](https://dub.co/help/article/how-to-transfer-domains)"
                      : !canManageLink
                        ? "You don't have permission to transfer this link."
                        : undefined
                  }
                />
              )}
              {options.includes("delete") && (
                <Button
                  text="Delete"
                  variant="danger-outline"
                  onClick={() => {
                    setOpenPopover(false);
                    setShowDeleteLinkModal(true);
                  }}
                  icon={<Trash className="size-4" />}
                  shortcut="X"
                  className="h-9 px-2 font-medium"
                  disabled={isRootLink}
                  disabledTooltip={
                    !canManageLink
                      ? "You don't have permission to delete this link."
                      : isRootLink
                        ? "You can't delete a custom domain link. You can delete the domain instead."
                        : undefined
                  }
                />
              )}
            </div>
          </div>
        }
        align="end"
        openPopover={openPopover}
        setOpenPopover={setOpenPopover}
      >
        <Button
          variant="secondary"
          className={cn(
            "h-8 px-1.5 outline-none transition-all duration-200",
            "border-transparent data-[state=open]:border-neutral-500 sm:group-hover/card:data-[state=closed]:border-neutral-200",
            className,
          )}
          icon={<ThreeDots className={cn("size-5 shrink-0", iconClassName)} />}
          onClick={() => {
            setOpenPopover(!openPopover);
          }}
        />
      </Popover>
    </div>
  );
}