Skip to content

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

add swr to link views

Marc Seitza45f25ef0f53
diff --git a/lib/swr/use-link.ts b/lib/swr/use-link.ts
new file mode 100644
index 00000000..234d2b4e
--- /dev/null
+++ b/lib/swr/use-link.ts
@@ -0,0 +1,26 @@
+import { useRouter } from "next/router";
+import useSWR from "swr";
+import { fetcher } from "@/lib/utils";
+import { LinkWithDocument } from "../types";
+
+export function useLink() {
+ const router = useRouter();
+
+ const { linkId } = router.query as {
+ linkId: string;
+ };
+
+ const { data: link, error } = useSWR<LinkWithDocument>(
+ linkId && `/api/links/${encodeURIComponent(linkId)}`,
+ fetcher,
+ {
+ dedupingInterval: 10000,
+ }
+ );
+
+ return {
+ link,
+ loading: !error && !link,
+ error,
+ };
+}
diff --git a/lib/types.ts b/lib/types.ts
index b44a6b30..cddff898 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -18,3 +18,7 @@ export interface LinkWithViews extends Link {
};
views: View[];
}
+
+export interface LinkWithDocument extends Link {
+ document: Document;
+}
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
new file mode 100644
index 00000000..3e6a932c
--- /dev/null
+++ b/pages/api/links/[id]/index.ts
@@ -0,0 +1,35 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import prisma from "@/lib/prisma";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method === "GET") {
+ // GET /api/links/:id
+ const { id } = req.query as { id: string };
+
+ try {
+ const link = await prisma.link.findUnique({
+ where: {
+ id: id,
+ },
+ select: {
+ id: true,
+ document: { select: { id: true, name: true, file: true } },
+ },
+ });
+
+ res.status(200).json(link);
+ } catch (error) {
+ return res.status(500).end({
+ message: "Internal Server Error",
+ error: (error as Error).message,
+ });
+ }
+ } else {
+ // We only allow GET and POST requests
+ res.setHeader("Allow", ["GET"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}
diff --git a/pages/api/links.ts b/pages/api/links/index.ts
similarity index 100%
rename from pages/api/links.ts
rename to pages/api/links/index.ts
diff --git a/pages/view/[linkId].tsx b/pages/view/[linkId].tsx
index 00db4073..a14b4ac7 100644
--- a/pages/view/[linkId].tsx
+++ b/pages/view/[linkId].tsx
@@ -1,28 +1,29 @@
-import { GetServerSideProps } from "next";
import React, { useState } from "react";
-import prisma from "@/lib/prisma";
import EmailForm from "@/components/EmailForm";
import { getExtension } from "@/lib/utils";
+import { useLink } from "@/lib/swr/use-link";
+import ErrorPage from "next/error";
-const DocumentView = ({
- document,
- linkId,
-}: {
- document: { id: string; file: string; name: string };
- linkId: string;
-}) => {
+export default function DocumentView() {
+ const { link, error } = useLink();
const [email, setEmail] = useState("");
const [submitted, setSubmitted] = useState(false);
- // get the file extension
- const extension = getExtension(document.file);
+ if (error && error.status === 404) {
+ return <ErrorPage statusCode={404} />;
+ }
+
+ if (!link) {
+ return <div>Loading...</div>;
+ }
+ const { document } = link;
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
const response = await fetch("/api/views", {
method: "POST",
- body: JSON.stringify({ linkId, email, documentId: document.id }),
+ body: JSON.stringify({ linkId: link.id, email, documentId: document.id }),
headers: {
"Content-Type": "application/json",
},
@@ -39,6 +40,9 @@ const DocumentView = ({
return <EmailForm onSubmitHandler={handleSubmit} setEmail={setEmail} />;
}
+ // get the file extension
+ const extension = getExtension(document.file);
+
if (
extension.includes(".docx") ||
extension.includes(".pptx") ||
@@ -77,34 +81,4 @@ const DocumentView = ({
></iframe>
</div>
);
-};
-
-export const getServerSideProps: GetServerSideProps = async ({ params }) => {
- const linkId = params?.linkId as string;
-
- const link = await prisma.link.findUnique({
- where: { id: linkId },
- include: { document: true },
- });
-
- if (!link || !link.document) {
- return {
- notFound: true,
- };
- }
-
- const document = link.document;
-
- return {
- props: {
- document: {
- id: document.id,
- file: document.file,
- name: document.name,
- },
- linkId,
- },
- };
-};
-
-export default DocumentView;
+}