The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add option to undo (un)archive with CMD + Z
unrenamedc7e0f2b1d8a8
diff --git a/apps/web/ui/modals/archive-link-modal.tsx b/apps/web/ui/modals/archive-link-modal.tsxindex f198126dd7..87b63d3fd8 100644--- a/apps/web/ui/modals/archive-link-modal.tsx+++ b/apps/web/ui/modals/archive-link-modal.tsx@@ -1,10 +1,11 @@import { BlurImage } from "@/ui/shared/blur-image";-import { Button, Modal } from "@dub/ui";+import { Button, Modal, useToastWithUndo } from "@dub/ui";import { GOOGLE_FAVICON_URL, getApexDomain, linkConstructor } from "@dub/utils";import { type Link as LinkProps } from "@prisma/client";import { useParams } from "next/navigation";import {Dispatch,+ MouseEvent,SetStateAction,useCallback,useMemo,@@ -43,6 +44,8 @@ function ArchiveLinkModal({props: LinkProps;archived: boolean;}) {+ const toastWithUndo = useToastWithUndo();+const params = useParams() as { slug?: string };const { slug } = params;const [archiving, setArchiving] = useState(false);@@ -58,7 +61,7 @@ function ArchiveLinkModal({});}, [key, domain]);- const handleArchiveRequest = async (event: MouseEvent) => {+ const handleArchiveRequest = async (event: MouseEvent<HTMLButtonElement>) => {event.preventDefault();setArchiving(true);@@ -72,23 +75,19 @@ function ArchiveLinkModal({revalidateLinks();setShowArchiveLinkModal(false);- toast.success(- `Successfully ${archived ? "archived" : "unarchived"} link!`,- {- action: {- label: "Undo",- onClick: undoAction,- },- duration: 5000,- },- );+ toastWithUndo({+ id: "link-archive-undo-toast",+ message: `Successfully ${archived ? "archived" : "unarchived"} link!`,+ undo: undoAction,+ duration: 5000,+ });};const undoAction = () => {toast.promise(sendArchiveRequest(!archived, props.id, slug), {loading: "Undo in progress...",error: "Failed to roll back changes. An error occurred.",- success: async () => {+ success: () => {revalidateLinks();return "Undo successful! Changes reverted.";},diff --git a/packages/ui/src/hooks/index.ts b/packages/ui/src/hooks/index.tsindex 02809d86b7..6cc0d98efd 100644--- a/packages/ui/src/hooks/index.ts+++ b/packages/ui/src/hooks/index.ts@@ -5,3 +5,4 @@ export { default as useLocalStorage } from "./use-local-storage";export { default as useMediaQuery } from "./use-media-query";export { default as useRouterStuff } from "./use-router-stuff";export { default as useScroll } from "./use-scroll";+export { default as useToastWithUndo } from "./use-toast-with-undo";diff --git a/packages/ui/src/hooks/use-toast-with-undo.tsx b/packages/ui/src/hooks/use-toast-with-undo.tsxnew file mode 100644index 0000000000..636b4e14ce--- /dev/null+++ b/packages/ui/src/hooks/use-toast-with-undo.tsx@@ -0,0 +1,65 @@+import { useEffect } from "react";+import { toast } from "sonner";+import CheckCircle from "../icons/check-circle";++function ToastWithUndo({+ id,+ message,+ undo,+}: {+ id: number | string;+ message: string;+ undo: () => void;+}) {+ const undoAndDismiss = () => {+ undo();+ toast.dismiss(id);+ };++ const handleKeyboardEvent = (event: KeyboardEvent) => {+ if (event.key === "z" && (event.ctrlKey || event.metaKey)) {+ undoAndDismiss();+ }+ };++ useEffect(() => {+ document.addEventListener("keydown", handleKeyboardEvent);+ return () => document.removeEventListener("keydown", handleKeyboardEvent);+ }, []);++ return (+ <div className="flex w-full items-center justify-between">+ <div className="flex items-center space-x-2">+ <CheckCircle />+ <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"+ onClick={undoAndDismiss}+ >+ Undo+ </button>+ </div>+ );+}++export default function useToastWithUndo() {+ const toastWithUndo = ({+ id,+ message,+ undo,+ duration,+ }: {+ id: number | string;+ message: string;+ undo: () => void;+ duration?: number;+ }) => {+ return toast(<ToastWithUndo id={id} message={message} undo={undo} />, {+ id,+ ...(duration && { duration }),+ });+ };++ return toastWithUndo;+}diff --git a/packages/ui/src/icons/check-circle.tsx b/packages/ui/src/icons/check-circle.tsxnew file mode 100644index 0000000000..e6e3e439ea--- /dev/null+++ b/packages/ui/src/icons/check-circle.tsx@@ -0,0 +1,18 @@+export default function CheckCircle({ className }: { className?: string }) {+ return (+ <svg+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 20 20"+ fill="currentColor"+ height="20"+ width="20"+ className={className}+ >+ <path+ fillRule="evenodd"+ d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z"+ clipRule="evenodd"+ />+ </svg>+ );+}diff --git a/packages/ui/src/icons/index.tsx b/packages/ui/src/icons/index.tsxindex f8a7688a4b..3f61b96f6c 100644--- a/packages/ui/src/icons/index.tsx+++ b/packages/ui/src/icons/index.tsx@@ -1,4 +1,5 @@// custom icons+export { default as CheckCircle } from "./check-circle";export { default as Copy } from "./copy";export { default as ExpandingArrow } from "./expanding-arrow";export { default as Magic } from "./magic";