Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: copy to clipboard (#39)

Marc Seitz4c3f16405753
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index ff2681ba..32e5dff6 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -9,10 +9,9 @@ import {
import { useState } from "react";
import { useRouter } from "next/router";
import { put, type PutBlobResult } from "@vercel/blob";
-import { toast } from "sonner";
import DocumentUpload from "@/components/document-upload";
import { pdfjs } from "react-pdf";
-import { getExtension } from "@/lib/utils";
+import { copyToClipboard, getExtension } from "@/lib/utils";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
@@ -51,15 +50,7 @@ export function AddDocumentModal({children}: {children: React.ReactNode}) {
if (response) {
const document = await response.json();
- navigator.clipboard
- .writeText(
- `${process.env.NEXT_PUBLIC_BASE_URL}/view/${document.links[0].id}`
- )
- .catch((error) => {
- console.log("Failed to copy text to clipboard", error);
- });
-
- toast.success("Document uploaded and link copied to clipboard. Redirecting to document page...")
+ copyToClipboard(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${document.links[0].id}`, "Document uploaded and link copied to clipboard. Redirecting to document page...")
setTimeout(() => {
router.push("/documents/" + document.id);
diff --git a/components/documents/document-card.tsx b/components/documents/document-card.tsx
index f568a279..cc6ce0a9 100644
--- a/components/documents/document-card.tsx
+++ b/components/documents/document-card.tsx
@@ -1,7 +1,6 @@
-import { getExtension, nFormatter, timeAgo } from "@/lib/utils";
+import { copyToClipboard, getExtension, nFormatter, timeAgo } from "@/lib/utils";
import Link from "next/link";
import { DocumentWithLinksAndLinkCountAndViewCount } from "@/lib/types";
-import { toast } from "sonner";
import Copy from "@/components/shared/icons/copy";
import BarChart from "@/components/shared/icons/bar-chart";
import Image from "next/image";
@@ -11,14 +10,9 @@ export default function DocumentsCard({
}: {
document: DocumentWithLinksAndLinkCountAndViewCount;
}) {
- function handleCopyToClipboard(id: string) {
- navigator.clipboard
- .writeText(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`)
- .catch((error) => {
- console.log("Failed to copy text to clipboard", error);
- });
- toast.success("Link copied to clipboard.");
+ function handleCopyToClipboard(id: string) {
+ copyToClipboard(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`, "Link copied to clipboard.");
}
diff --git a/components/links/links-table.tsx b/components/links/links-table.tsx
index 0f4e517d..1f273546 100644
--- a/components/links/links-table.tsx
+++ b/components/links/links-table.tsx
@@ -23,9 +23,8 @@ import {
import { Button } from "@/components/ui/button";
import { useDocumentLinks } from "@/lib/swr/use-document";
-import { toast } from "sonner";
import BarChart from "../shared/icons/bar-chart";
-import { nFormatter, timeAgo } from "@/lib/utils";
+import { copyToClipboard, nFormatter, timeAgo } from "@/lib/utils";
import MoreHorizontal from "../shared/icons/more-horizontal";
import { Skeleton } from "../ui/skeleton";
import LinksVisitors from "./links-visitors";
@@ -36,13 +35,7 @@ export default function LinksTable() {
const { links } = useDocumentLinks();
const handleCopyToClipboard = (id: string) => {
- navigator.clipboard
- .writeText(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`)
- .catch((error) => {
- console.log("Failed to copy text to clipboard", error);
- });
-
- toast.success("Link copied to clipboard.");
+ copyToClipboard(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`, "Link copied to clipboard.");
};
return (
diff --git a/components/welcome/upload.tsx b/components/welcome/upload.tsx
index ab934fe4..f8cd0e9b 100644
--- a/components/welcome/upload.tsx
+++ b/components/welcome/upload.tsx
@@ -8,7 +8,7 @@ import { toast } from "sonner";
import Skeleton from "../Skeleton";
import { STAGGER_CHILD_VARIANTS } from "@/lib/contants";
import { pdfjs } from "react-pdf";
-import { getExtension } from "@/lib/utils";
+import { copyToClipboard, getExtension } from "@/lib/utils";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
@@ -100,15 +100,7 @@ export default function Upload() {
}
const handleCopyToClipboard = (id: string) => {
- navigator.clipboard
- .writeText(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`)
- .catch((error) => {
- console.log("Failed to copy text to clipboard", error);
- });
-
- setCopiedLink(true);
-
- toast.success("Document uploaded and link copied to clipboard.");
+ copyToClipboard(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`, "Document uploaded and link copied to clipboard.");
};
return (
diff --git a/lib/utils.ts b/lib/utils.ts
index 024dfd02..3ed4f626 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -2,6 +2,7 @@ import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import ms from "ms"
import bcrypt from "bcryptjs";
+import { toast } from "sonner";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
@@ -181,4 +182,15 @@ export async function checkPassword(
): Promise<boolean> {
const match = await bcrypt.compare(password, hashedPassword);
return match;
+}
+
+export function copyToClipboard(text: string, message: string): void {
+ navigator.clipboard
+ .writeText(text)
+ .then(() => {
+ toast.success(message);
+ })
+ .catch((error) => {
+ toast.error("Failed to copy. Please try again.")
+ });
}
\ No newline at end of file