Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: Add delete link functionality and modal
Cursor Agentade32de40960
diff --git a/components/links/delete-link-modal.tsx b/components/links/delete-link-modal.tsxnew file mode 100644index 00000000..3453dd7a--- /dev/null+++ b/components/links/delete-link-modal.tsx@@ -0,0 +1,207 @@+import { useRouter } from "next/router";++import {+ Dispatch,+ SetStateAction,+ useCallback,+ useMemo,+ useState,+} from "react";++import { useTeam } from "@/context/team-context";+import { toast } from "sonner";+import { mutate } from "swr";++import { Button } from "@/components/ui/button";+import { DialogDescription, DialogTitle } from "@/components/ui/dialog";+import { Input } from "@/components/ui/input";+import { Modal } from "@/components/ui/modal";++import { useAnalytics } from "@/lib/analytics";+import { LinkWithViews } from "@/lib/types";++function DeleteLinkModal({+ showDeleteLinkModal,+ setShowDeleteLinkModal,+ link,+ targetType,+}: {+ showDeleteLinkModal: boolean;+ setShowDeleteLinkModal: Dispatch<SetStateAction<boolean>>;+ link: LinkWithViews | null;+ targetType: "DOCUMENT" | "DATAROOM";+}) {+ const router = useRouter();+ const teamInfo = useTeam();+ const analytics = useAnalytics();++ const [deleting, setDeleting] = useState(false);+ const [isValid, setIsValid] = useState(false);++ const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {+ const { value } = e.target;+ // Check if the input matches the pattern+ if (value === "permanently delete") {+ setIsValid(true);+ } else {+ setIsValid(false);+ }+ };++ async function deleteLink(linkId: string) {+ return new Promise(async (resolve, reject) => {+ setDeleting(true);++ try {+ const response = await fetch(`/api/links/${linkId}`, {+ method: "DELETE",+ headers: {+ "Content-Type": "application/json",+ },+ });++ if (!response.ok) {+ const error = await response.json();+ throw new Error(error.message || "Failed to delete link");+ }++ analytics.capture("Link Deleted", {+ team: teamInfo?.currentTeam?.id,+ linkId,+ linkType: link?.linkType,+ viewCount: link?._count.views || 0,+ });++ // Mutate the links cache based on target type+ const endpointTargetType = `${targetType.toLowerCase()}s`; // "documents" or "datarooms"+ const targetId = link?.documentId || link?.dataroomId;++ await mutate(+ `/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}/${encodeURIComponent(+ targetId ?? "",+ )}/links`,+ );++ // If this is a group link, also mutate the group-specific links cache+ if (link?.groupId) {+ await mutate(+ `/api/teams/${teamInfo?.currentTeam?.id}/${endpointTargetType}/${encodeURIComponent(+ targetId ?? "",+ )}/groups/${link.groupId}/links`,+ );+ }++ setDeleting(false);+ setShowDeleteLinkModal(false);+ resolve(null);+ } catch (error) {+ setDeleting(false);+ reject((error as Error).message);+ }+ });+ }++ if (!link) return null;++ const viewCount = link._count.views || 0;++ return (+ <Modal+ showModal={showDeleteLinkModal}+ setShowModal={setShowDeleteLinkModal}+ noBackdropBlur+ >+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-border bg-white px-4 py-4 pt-8 dark:border-gray-900 dark:bg-gray-900 sm:px-8">+ <DialogTitle className="text-2xl">Delete Link</DialogTitle>+ <DialogDescription className="space-y-2">+ <p>+ <strong>Warning</strong>: This will permanently delete the link{" "}+ <span className="font-semibold">+ {link.name || `Link #${link.id.slice(-5)}`}+ </span>{" "}+ and all associated data.+ </p>+ <p>+ This includes <strong>{viewCount} view{viewCount !== 1 ? "s" : ""}</strong> and all+ visitor analytics. This action cannot be undone.+ </p>+ </DialogDescription>+ </div>++ <form+ onSubmit={async (e) => {+ e.preventDefault();+ toast.promise(deleteLink(link.id), {+ loading: "Deleting link...",+ success: "Link deleted successfully!",+ error: (err) => err,+ });+ }}+ className="flex flex-col space-y-6 bg-muted px-4 py-8 text-left dark:bg-gray-900 sm:px-8"+ >+ <div>+ <label+ htmlFor="verification"+ className="block text-sm text-muted-foreground"+ >+ To confirm deletion, type{" "}+ <span className="font-semibold text-foreground">+ permanently delete+ </span>{" "}+ below+ </label>+ <div className="relative mt-1 rounded-md shadow-sm">+ <Input+ type="text"+ name="verification"+ id="verification"+ pattern="permanently delete"+ required+ autoComplete="off"+ className="bg-white dark:border-gray-500 dark:bg-gray-800 focus:dark:bg-transparent"+ onInput={handleInputChange}+ />+ </div>+ </div>++ <Button+ variant="destructive"+ type="submit"+ loading={deleting}+ disabled={!isValid}+ >+ Confirm delete link+ </Button>+ </form>+ </Modal>+ );+}++export function useDeleteLinkModal({+ link,+ targetType,+}: {+ link: LinkWithViews | null;+ targetType: "DOCUMENT" | "DATAROOM";+}) {+ const [showDeleteLinkModal, setShowDeleteLinkModal] = useState(false);++ const DeleteLinkModalCallback = useCallback(() => {+ return (+ <DeleteLinkModal+ showDeleteLinkModal={showDeleteLinkModal}+ setShowDeleteLinkModal={setShowDeleteLinkModal}+ link={link}+ targetType={targetType}+ />+ );+ }, [showDeleteLinkModal, setShowDeleteLinkModal, link, targetType]);++ return useMemo(+ () => ({+ setShowDeleteLinkModal,+ DeleteLinkModal: DeleteLinkModalCallback,+ }),+ [setShowDeleteLinkModal, DeleteLinkModalCallback],+ );+}diff --git a/components/links/links-table.tsx b/components/links/links-table.tsxindex 6bb3f252..92dad562 100644--- a/components/links/links-table.tsx+++ b/components/links/links-table.tsx@@ -14,6 +14,7 @@ import {FileSlidersIcon,LinkIcon,Settings2Icon,+ Trash2Icon,} from "lucide-react";import { useQueryState } from "nuqs";import { toast } from "sonner";@@ -63,6 +64,7 @@ import MoreHorizontal from "../shared/icons/more-horizontal";import { Badge } from "../ui/badge";import { Label } from "../ui/label";import { ButtonTooltip } from "../ui/tooltip";+import { useDeleteLinkModal } from "./delete-link-modal";import EmbedCodeModal from "./embed-code-modal";import LinkActiveControls, {countActiveSettings,@@ -171,6 +173,12 @@ export default function LinksTable({const [editPermissionLink, setEditPermissionLink] =useState<LinkWithViews | null>(null);+ const [linkToDelete, setLinkToDelete] = useState<LinkWithViews | null>(null);+ const { setShowDeleteLinkModal, DeleteLinkModal } = useDeleteLinkModal({+ link: linkToDelete,+ targetType,+ });+const handleCopyToClipboard = (linkString: string) => {copyToClipboard(`${linkString}`, "Link copied to clipboard.");};@@ -905,6 +913,17 @@ export default function LinksTable({<Code2Icon className="mr-2 h-4 w-4" />Get Embed Code</DropdownMenuItem>+ <DropdownMenuSeparator />+ <DropdownMenuItem+ onClick={() => {+ setLinkToDelete(link);+ setShowDeleteLinkModal(true);+ }}+ className="text-destructive focus:bg-destructive focus:text-destructive-foreground"+ >+ <Trash2Icon className="mr-2 h-4 w-4" />+ Delete Link+ </DropdownMenuItem></DropdownMenuContent></DropdownMenu></TableCell>@@ -983,6 +1002,8 @@ export default function LinksTable({linkName={selectedEmbedLink.name}/>)}++ <DeleteLinkModal /></div></>);diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.tsindex 7c24dc89..033cb364 100644--- a/pages/api/links/[id]/index.ts+++ b/pages/api/links/[id]/index.ts@@ -456,6 +456,7 @@ export default async function handle(return res.status(401).end("Unauthorized");}+ const userId = (session.user as CustomUser).id;const { id } = req.query as { id: string };try {@@ -469,6 +470,24 @@ export default async function handle(ownerId: true,},},+ dataroom: {+ select: {+ teamId: true,+ },+ },+ team: {+ select: {+ users: {+ where: {+ userId: userId,+ },+ select: {+ userId: true,+ role: true,+ },+ },+ },+ },},});@@ -476,10 +495,19 @@ export default async function handle(return res.status(404).json({ error: "Link not found" });}- if (- linkToBeDeleted.document!.ownerId !== (session.user as CustomUser).id- ) {- return res.status(401).end("Unauthorized to access the link");+ // Check authorization based on link type+ let isAuthorized = false;++ if (linkToBeDeleted.documentId && linkToBeDeleted.document) {+ // Document link - check if user owns the document+ isAuthorized = linkToBeDeleted.document.ownerId === userId;+ } else if (linkToBeDeleted.dataroomId && linkToBeDeleted.team) {+ // Dataroom link - check if user is a member of the team+ isAuthorized = linkToBeDeleted.team.users.length > 0;+ }++ if (!isAuthorized) {+ return res.status(401).end("Unauthorized to delete this link");}await prisma.link.delete({