diff --git a/src/app/actions.ts b/src/app/actions.ts index 82416e1..e1b17e7 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -63,32 +63,3 @@ export async function getPaperlessDocuments(query: string) { return data; } - -export async function getPaperlessDocument( - documentId: number, -): Promise { - const userData = await getUserData(); - if (!userData) { - console.error("Error getting user data"); - return null; - } - try { - const url = `${userData.paperlessURL}/api/documents/${documentId}/download/`; - const response = await fetch(url, { - headers: { - Authorization: `Token ${userData.paperlessToken}`, - }, - }); - if (response.ok) { - const blob = await response.blob(); - const objectUrl = URL.createObjectURL(blob); - return objectUrl; - } else { - console.error("Failed to fetch PDF"); - return null; - } - } catch (error) { - console.error("Error fetching PDF:", error); - return null; - } -} diff --git a/src/components/document-viewer.tsx b/src/components/document-viewer.tsx index b6dad69..853e555 100644 --- a/src/components/document-viewer.tsx +++ b/src/components/document-viewer.tsx @@ -1,10 +1,39 @@ "use client"; import { useState, useEffect } from "react"; -import { getPaperlessDocument } from "@/app/actions"; import LoadingSpinner from "@/components/loading-spinner"; import { Button } from "./ui/button"; import { useRouter } from "next/navigation"; +import { getUserData } from "@/app/actions"; + +export async function getPaperlessDocument( + documentId: number, +): Promise { + const userData = await getUserData(); + if (!userData) { + console.error("Error getting user data"); + return null; + } + try { + const url = `${userData.paperlessURL}/api/documents/${documentId}/download/`; + const response = await fetch(url, { + headers: { + Authorization: `Token ${userData.paperlessToken}`, + }, + }); + if (response.ok) { + const blob = await response.blob(); + const objectUrl = URL.createObjectURL(blob); + return objectUrl; + } else { + console.error("Failed to fetch PDF"); + return null; + } + } catch (error) { + console.error("Error fetching PDF:", error); + return null; + } +} export default function DocumentViewer(props: { id: number }) { const router = useRouter(); @@ -19,10 +48,10 @@ export default function DocumentViewer(props: { id: number }) { const objectUrl = await getPaperlessDocument(props.id); if (objectUrl) { setPdfUrl(objectUrl); + setLoading(false); // Cleanup function to revoke URL when component unmounts or pdfUrl changes return () => URL.revokeObjectURL(objectUrl); } else { - setLoading(false); } };