Skip to content

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

add new link generation

Marc Seitzbd99b9865bd5
diff --git a/components/links/links-container.tsx b/components/links/links-container.tsx
index 1db64fa5..4158524b 100644
--- a/components/links/links-container.tsx
+++ b/components/links/links-container.tsx
@@ -4,10 +4,18 @@ import Notification from "../Notification";
import toast from "react-hot-toast";
import { useDocumentLinks } from "@/lib/swr/use-document";
import { formatDistance, parseISO } from "date-fns";
+import { useRouter } from "next/router";
+import { useState } from "react";
+import { mutate } from "swr";
export default function LinksContainer() {
const { links } = useDocumentLinks();
+ const router = useRouter();
+ const [isLoading, setIsLoading] = useState<boolean>(false);
+
+ const documentId = router.query.id as string;
+
const handleCopyToClipboard = (id: string) => {
navigator.clipboard.writeText(
`${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`
@@ -22,6 +30,34 @@ export default function LinksContainer() {
));
};
+ async function addNewLink() {
+ setIsLoading(true);
+ const response = await fetch("/api/links", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ documentId: documentId,
+ }),
+ });
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ const newLink = await response.json();
+
+ // Update local data with the new link
+ mutate(
+ `/api/documents/${encodeURIComponent(documentId)}/links`,
+ [...(links || []), newLink],
+ false
+ );
+
+ setIsLoading(false);
+ }
+
return (
<div className="border-t border-white/10 pt-11">
<h2 className="px-4 text-base font-semibold leading-7 text-white sm:px-6 lg:px-8">
@@ -113,7 +149,9 @@ export default function LinksContainer() {
{ addSuffix: true }
)}
</time>
- ) : null}
+ ) : (
+ "-"
+ )}
</td>
</tr>
))
@@ -131,17 +169,17 @@ export default function LinksContainer() {
</tr>
</>
)}
- {/* {isLoading && (
+ {isLoading && (
<tr>
<td className="py-4 pl-4 pr-8 sm:pl-6 lg:pl-8">
<Skeleton className="h-5 w-full" />
</td>
</tr>
- )} */}
+ )}
<tr>
<td className="py-4 pl-4 pr-8 sm:pl-6 lg:pl-8">
<button
- // onClick={() => addNewLink()}
+ onClick={() => addNewLink()}
className="text-gray-400 hover:text-gray-50"
>
Add another link
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 26738a0e..abbbb7c6 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -1,11 +1,18 @@
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
+import { getServerSession } from "next-auth/next";
+import { authOptions } from "../auth/[...nextauth]";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "POST") {
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end({ error: "Unauthorized" });
+ }
+
const { documentId } = req.body;
// Fetch the link and its related document from the database