diff --git a/src/components/document-details.tsx b/src/components/document-details.tsx index 9528d4c..ca2858a 100644 --- a/src/components/document-details.tsx +++ b/src/components/document-details.tsx @@ -13,7 +13,6 @@ import { AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { toast } from "sonner"; -import DocumentPreview, { getPaperlessDocument } from "./document-preview"; import { getUserData } from "@/app/actions"; import { Button, buttonVariants } from "./ui/button"; import { useRouter } from "next/navigation"; @@ -25,12 +24,38 @@ import { import type { UsersTableType } from "@/server/db/schema"; import LoadingSpinner from "./loading-spinner"; import OpenExternalLink from "./external-link"; -import { PaperlessDocumentType } from "@/types"; +import type { PaperlessDocumentType } from "@/types"; import React from "react"; import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; +import BodyMessage from "./body-message"; const queryClient = new QueryClient(); +export async function getPaperlessDocument( + documentId: number, + userData: UsersTableType, +): Promise { + 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; + } +} + async function deleteDocument(documentId: number) { const userData = await getUserData(); if (!userData) { @@ -92,18 +117,30 @@ function DocumentDetailsInner(props: { id: number }) { queryFn: fetchUserData, }); - const { data: documentData, isLoading: isdocumentDataLoading } = useQuery({ + const { data: pdfUrl, isLoading: isPdfUrlLoading } = useQuery({ queryKey: ["pdfUrl", props.id, userData], // Include id and paperlessURL in the query key + queryFn: async () => { + return await getPaperlessDocument(props.id, userData!); + }, + enabled: !!userData, + }); + + const { data: documentData, isLoading: isdocumentDataLoading } = useQuery({ + queryKey: ["pdfData", props.id, userData], // Include id and paperlessURL in the query key queryFn: async () => { return await getPaperlessDocumentData(props.id, userData!); }, enabled: !!userData, }); - if (isUserDataLoading ?? isdocumentDataLoading) { + if (isUserDataLoading || isdocumentDataLoading || isPdfUrlLoading) { return Loading...; } + if (!userData || !documentData) { + return Error; + } + return (
diff --git a/src/components/document-preview.tsx b/src/components/document-preview.tsx index a058b19..76fc8fd 100644 --- a/src/components/document-preview.tsx +++ b/src/components/document-preview.tsx @@ -9,15 +9,17 @@ import type { AdviceAPIType } from "@/types"; import OpenInternalLink from "./internal-link"; import type { UsersTableType } from "@/server/db/schema"; import { Button } from "./ui/button"; +import BodyMessage from "./body-message"; +import { buttonVariants } from "./ui/button"; const queryClient = new QueryClient(); -export async function getPaperlessDocument( +export async function getPaperlessThumbnail( documentId: number, userData: UsersTableType, ): Promise { try { - const url = `${userData.paperlessURL}/api/documents/${documentId}/download/`; + const url = `${userData.paperlessURL}/api/documents/${documentId}/thumb/`; const response = await fetch(url, { headers: { Authorization: `Token ${userData.paperlessToken}`, @@ -63,12 +65,6 @@ function SkeletonLoader() {
- {/* Button Skeleton */} -
- {Array.from({ length: 7 }, (_, index) => ( -
- ))} -
); } @@ -92,7 +88,7 @@ function Preview(props: { id: number }) { queryKey: ["pdfUrl", props.id, userData], // Include id and paperlessURL in the query key queryFn: async () => { console.log("fetching"); - return await getPaperlessDocument(props.id, userData!); + return await getPaperlessThumbnail(props.id, userData!); }, enabled: !!userData, }); @@ -102,35 +98,21 @@ function Preview(props: { id: number }) { } if (!pdfUrl || !userData) { - return ( -
-
-

- Failed to get document -

-
-
- ); + return Failed to get document; } - return ( - <> - -

- Your web browser doesn't have a PDF plugin. Instead you can - - click here to download the PDF file. - -

-
- - - ); + return Document Preview; } export default function DocumentPreview(props: { id: number }) { return ( + + Open full page + ); }