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 Wilson09ba2eeaebf9
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx
index 157b031fa2..e985518e3e 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/links/[...link]/page-client.tsx
@@ -25,10 +25,11 @@ import { QRCodePreview } from "@/ui/links/link-builder/qr-code-preview";
import { TagSelect } from "@/ui/links/link-builder/tag-select";
import { useLinkBuilderSubmit } from "@/ui/links/link-builder/use-link-builder-submit";
import { useMetatags } from "@/ui/links/link-builder/use-metatags";
+import { LinkControls } from "@/ui/links/link-controls";
import { useKeyboardShortcut, useMediaQuery } from "@dub/ui";
import { cn } from "@dub/utils";
import { notFound, useParams, useRouter } from "next/navigation";
-import { useEffect, useRef, useState } from "react";
+import { memo, useEffect, useRef, useState } from "react";
import { useFormContext, useFormState } from "react-hook-form";
export function LinkPageClient() {
@@ -154,6 +155,7 @@ function LinkBuilder({ link }: { link: ExpandedLinkProps }) {
<div className="shrink-0">
<LinkAnalyticsBadge link={link} />
</div>
+ <Controls link={link} />
</div>
</LinkBuilderHeader>
</div>
@@ -211,6 +213,30 @@ function LinkBuilder({ link }: { link: ExpandedLinkProps }) {
);
}
+const Controls = memo(({ link }: { link: ExpandedLinkProps }) => {
+ const router = useRouter();
+ const { slug } = useWorkspace();
+ const [openPopover, setOpenPopover] = useState(false);
+ const { setValue, getValues, reset } = useFormContext<LinkFormData>();
+
+ return (
+ <div className="">
+ <LinkControls
+ link={link}
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ shortcutsEnabled={openPopover}
+ options={["move", "id", "archive", "delete"]}
+ onMoveSuccess={(folderId) => {
+ setValue("folderId", folderId);
+ reset(getValues(), { keepValues: true, keepDirty: false });
+ }}
+ onDeleteSuccess={() => router.push(`/${slug}/links`)}
+ />
+ </div>
+ );
+});
+
function LoadingSkeleton() {
return (
<div className="flex min-h-[calc(100vh-8px)] flex-col rounded-t-[inherit] bg-white">
diff --git a/apps/web/ui/folders/move-link-form.tsx b/apps/web/ui/folders/move-link-form.tsx
index 6d3b45aff3..37c00d7ae7 100644
--- a/apps/web/ui/folders/move-link-form.tsx
+++ b/apps/web/ui/folders/move-link-form.tsx
@@ -9,7 +9,7 @@ import { FolderDropdown } from "./folder-dropdown";
interface MoveLinkFormProps {
links: ExpandedLinkProps[];
- onSuccess: () => void;
+ onSuccess: (folderId: string | null) => void;
onCancel: () => void;
}
@@ -61,7 +61,7 @@ export const MoveLinkForm = ({
);
toast.success("Link moved successfully!");
- onSuccess();
+ onSuccess(selectedFolderId === "unsorted" ? null : selectedFolderId);
};
return (
diff --git a/apps/web/ui/links/link-controls.tsx b/apps/web/ui/links/link-controls.tsx
index 4ec0a26739..ba2f6c0528 100644
--- a/apps/web/ui/links/link-controls.tsx
+++ b/apps/web/ui/links/link-controls.tsx
@@ -7,7 +7,6 @@ import { useArchiveLinkModal } from "@/ui/modals/archive-link-modal";
import { useDeleteLinkModal } from "@/ui/modals/delete-link-modal";
import {
Button,
- CardList,
IconMenu,
PenWriting,
Popover,
@@ -25,29 +24,49 @@ import {
import { cn, isDubDomain, nanoid, punycode } from "@dub/utils";
import { CopyPlus, Delete, FolderInput } from "lucide-react";
import { useParams, useRouter, useSearchParams } from "next/navigation";
-import { useCallback, useContext } from "react";
+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";
-import { LinksListContext } from "./links-container";
-export function LinkControls({ link }: { link: ExpandedLinkProps }) {
+const OPTIONS = {
+ edit: "e",
+ qr: "q",
+ duplicate: "d",
+ id: "i",
+ move: "m",
+ archive: "a",
+ transfer: "t",
+ delete: "x",
+ ban: "b",
+};
+
+export function LinkControls({
+ link,
+ openPopover,
+ setOpenPopover,
+ shortcutsEnabled,
+ options = Object.keys(OPTIONS),
+ onMoveSuccess,
+ onDeleteSuccess,
+}: {
+ link: ExpandedLinkProps;
+ openPopover: boolean;
+ setOpenPopover: (open: boolean) => void;
+ shortcutsEnabled: boolean;
+ options?: string[];
+ onMoveSuccess?: (folderId: string | null) => void;
+ onDeleteSuccess?: () => void;
+}) {
const { flags } = useWorkspace();
const router = useRouter();
const { slug } = useParams() as { slug?: string };
const { data: foldersCount } = useFoldersCount();
- const { hovered } = useContext(CardList.Card.Context);
const searchParams = useSearchParams();
- const { openMenuLinkId, setOpenMenuLinkId } = useContext(LinksListContext);
- const openPopover = openMenuLinkId === link.id;
- const setOpenPopover = (open: boolean) => {
- setOpenMenuLinkId(open ? link.id : null);
- };
-
const [copiedLinkId, copyToClipboard] = useCopyToClipboard();
const copyLinkId = () => {
@@ -68,12 +87,13 @@ export function LinkControls({ link }: { link: ExpandedLinkProps }) {
});
const { setShowDeleteLinkModal, DeleteLinkModal } = useDeleteLinkModal({
props: link,
+ onSuccess: onDeleteSuccess,
});
const { setShowLinkQRModal, LinkQRModal } = useLinkQRModal({
props: link,
});
const { setShowMoveLinkToFolderModal, MoveLinkToFolderModal } =
- useMoveLinkToFolderModal({ link });
+ useMoveLinkToFolderModal({ link, onSuccess: onMoveSuccess });
const isRootLink = link.key === "_root";
const isProgramLink = link.programId !== null;
@@ -125,7 +145,7 @@ export function LinkControls({ link }: { link: ExpandedLinkProps }) {
);
useKeyboardShortcut(
- ["e", "d", "q", "m", "a", "t", "i", "x", "b"],
+ options.map((o) => OPTIONS[o]),
(e) => {
setOpenPopover(false);
switch (e.key) {
@@ -164,175 +184,190 @@ export function LinkControls({ link }: { link: ExpandedLinkProps }) {
}
},
{
- enabled: openPopover || (hovered && openMenuLinkId === null),
+ enabled: shortcutsEnabled,
priority: 1, // Take priority over display options
},
);
return (
<div className="flex justify-end">
- <LinkQRModal />
- <DuplicateLinkModal />
- <ArchiveLinkModal />
- <TransferLinkModal />
- <DeleteLinkModal />
- <MoveLinkToFolderModal />
+ {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">
- <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
- }
- />
- <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"
- />
- <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
- }
- />
- <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("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("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
+ }
+ />
+ )}
+ {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"
+ />
+ )}
</div>
<div className="border-t border-neutral-200" />
<div className="grid gap-px p-2">
- {Boolean(flags?.linkFolders && foldersCount) && (
+ {options.includes("move") &&
+ Boolean(flags?.linkFolders && foldersCount) && (
+ <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="Move"
+ text={link.archived ? "Unarchive" : "Archive"}
variant="outline"
- shortcut="M"
- className="h-9 px-2 font-medium"
- icon={<FolderBookmark className="size-4 text-neutral-600" />}
onClick={() => {
setOpenPopover(false);
- setShowMoveLinkToFolderModal(true);
+ setShowArchiveLinkModal(true);
}}
+ icon={<BoxArchive className="size-4" />}
+ shortcut="A"
+ className="h-9 px-2 font-medium"
disabledTooltip={
!canManageLink
- ? "You don't have permission to move this link to another folder."
+ ? "You don't have permission to archive this link."
: undefined
}
/>
)}
-
- <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
- }
- />
-
- <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) ? (
- <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"
- />
- ) : !canManageLink ? (
- "You don't have permission to transfer this link."
- ) : undefined
- }
- />
-
- <Button
- text="Delete"
- variant="danger-outline"
- onClick={() => {
- setOpenPopover(false);
- setShowDeleteLinkModal(true);
- }}
- icon={<Delete className="size-4" />}
- shortcut="X"
- className="h-9 px-2 font-medium"
- disabled={isRootLink || isProgramLink}
- 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."
- : isProgramLink
- ? "You can't delete a link that's part of a program."
- : undefined
- }
- />
-
- {!slug && ( // this is only shown in admin mode (where there's no slug)
- <button
- onClick={() => handleBanLink()}
- 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="size-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>
+ {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) ? (
+ <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"
+ />
+ ) : !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={<Delete className="size-4" />}
+ shortcut="X"
+ className="h-9 px-2 font-medium"
+ disabled={isRootLink || isProgramLink}
+ 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."
+ : isProgramLink
+ ? "You can't delete a link that's part of a program."
+ : undefined
+ }
+ />
+ )}
+
+ {options.includes("ban") &&
+ !slug && ( // this is only shown in admin mode (where there's no slug)
+ <button
+ onClick={() => handleBanLink()}
+ 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="size-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>
}
diff --git a/apps/web/ui/links/link-details-column.tsx b/apps/web/ui/links/link-details-column.tsx
index 675a1d9329..0232137646 100644
--- a/apps/web/ui/links/link-details-column.tsx
+++ b/apps/web/ui/links/link-details-column.tsx
@@ -1,12 +1,19 @@
import { TagProps } from "@/lib/types";
-import { Tooltip, useRouterStuff } from "@dub/ui";
+import { CardList, Tooltip, useRouterStuff } from "@dub/ui";
import { cn } from "@dub/utils";
import { useSearchParams } from "next/navigation";
-import { memo, PropsWithChildren, useContext, useMemo, useRef } from "react";
+import {
+ memo,
+ PropsWithChildren,
+ useCallback,
+ useContext,
+ useMemo,
+ useRef,
+} from "react";
import { LinkAnalyticsBadge } from "./link-analytics-badge";
import { LinkControls } from "./link-controls";
import { useLinkSelection } from "./link-selection-provider";
-import { ResponseLink } from "./links-container";
+import { LinksListContext, ResponseLink } from "./links-container";
import { LinksDisplayContext } from "./links-display-provider";
import TagBadge from "./tag-badge";
@@ -61,10 +68,25 @@ export function LinkDetailsColumn({ link }: { link: ResponseLink }) {
const Controls = memo(({ link }: { link: ResponseLink }) => {
const { isSelectMode } = useLinkSelection();
+ const { hovered } = useContext(CardList.Card.Context);
+
+ const { openMenuLinkId, setOpenMenuLinkId } = useContext(LinksListContext);
+ const openPopover = openMenuLinkId === link.id;
+ const setOpenPopover = useCallback(
+ (open: boolean) => {
+ setOpenMenuLinkId(open ? link.id : null);
+ },
+ [link.id, setOpenMenuLinkId],
+ );
return (
<div className={cn(isSelectMode && "hidden sm:block")}>
- <LinkControls link={link} />
+ <LinkControls
+ link={link}
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ shortcutsEnabled={openPopover || (hovered && openMenuLinkId === null)}
+ />
</div>
);
});
diff --git a/apps/web/ui/modals/delete-link-modal.tsx b/apps/web/ui/modals/delete-link-modal.tsx
index 536e83ea60..cbd8233821 100644
--- a/apps/web/ui/modals/delete-link-modal.tsx
+++ b/apps/web/ui/modals/delete-link-modal.tsx
@@ -17,6 +17,7 @@ type DeleteLinkModalProps = {
showDeleteLinkModal: boolean;
setShowDeleteLinkModal: Dispatch<SetStateAction<boolean>>;
links: LinkProps[];
+ onSuccess?: () => void;
};
function DeleteLinkModal(props: DeleteLinkModalProps) {
@@ -33,6 +34,7 @@ function DeleteLinkModal(props: DeleteLinkModalProps) {
function DeleteLinkModalInner({
setShowDeleteLinkModal,
links,
+ onSuccess,
}: DeleteLinkModalProps) {
const { id } = useWorkspace();
const [deleting, setDeleting] = useState(false);
@@ -83,6 +85,7 @@ function DeleteLinkModalInner({
if (res.status === 200) {
await mutatePrefix("/api/links");
setShowDeleteLinkModal(false);
+ onSuccess?.();
toast.success(
`Successfully deleted ${pluralize("link", links.length)}!`,
);
@@ -138,8 +141,10 @@ function DeleteLinkModalInner({
export function useDeleteLinkModal({
props,
+ onSuccess,
}: {
props?: LinkProps | LinkProps[];
+ onSuccess?: () => void;
}) {
const [showDeleteLinkModal, setShowDeleteLinkModal] = useState(false);
@@ -149,6 +154,7 @@ export function useDeleteLinkModal({
showDeleteLinkModal={showDeleteLinkModal}
setShowDeleteLinkModal={setShowDeleteLinkModal}
links={Array.isArray(props) ? props : [props]}
+ onSuccess={onSuccess}
/>
) : null;
}, [showDeleteLinkModal, setShowDeleteLinkModal]);
diff --git a/apps/web/ui/modals/move-link-to-folder-modal.tsx b/apps/web/ui/modals/move-link-to-folder-modal.tsx
index 2aba08441a..d4d2257134 100644
--- a/apps/web/ui/modals/move-link-to-folder-modal.tsx
+++ b/apps/web/ui/modals/move-link-to-folder-modal.tsx
@@ -7,12 +7,14 @@ interface MoveLinkToFolderModalProps {
links: ExpandedLinkProps[];
showModal: boolean;
setShowModal: (showModal: boolean) => void;
+ onSuccess?: (folderId: string | null) => void;
}
const MoveLinkToFolderModal = ({
links,
showModal,
setShowModal,
+ onSuccess,
}: MoveLinkToFolderModalProps) => {
return (
<Modal
@@ -22,7 +24,10 @@ const MoveLinkToFolderModal = ({
>
<MoveLinkForm
links={links}
- onSuccess={() => setShowModal(false)}
+ onSuccess={(folderId) => {
+ setShowModal(false);
+ onSuccess?.(folderId);
+ }}
onCancel={() => setShowModal(false)}
/>
</Modal>
@@ -30,13 +35,14 @@ const MoveLinkToFolderModal = ({
};
export function useMoveLinkToFolderModal(
- props:
+ props: { onSuccess?: (folderId: string | null) => void } & (
| {
link: ExpandedLinkProps;
}
| {
links: ExpandedLinkProps[];
- },
+ }
+ ),
) {
const [showMoveLinkToFolderModal, setShowMoveLinkToFolderModal] =
useState(false);
@@ -47,6 +53,7 @@ export function useMoveLinkToFolderModal(
links={"link" in props ? [props.link] : props.links}
showModal={showMoveLinkToFolderModal}
setShowModal={setShowMoveLinkToFolderModal}
+ onSuccess={props.onSuccess}
/>
);
}, [showMoveLinkToFolderModal, setShowMoveLinkToFolderModal]);