From ae8eb084c261cd15b308a3453301252aa1df7b8d Mon Sep 17 00:00:00 2001 From: Richard Roberson Date: Sat, 18 Jan 2025 13:04:38 -0700 Subject: [PATCH] Update --- src/app/pdf/[id]/page.tsx | 29 +++--------------------- src/components/PDFSkeleton.tsx | 37 ++++++++++++++++++++++++++++++ src/components/PDFViewer.tsx | 41 ++++++---------------------------- 3 files changed, 47 insertions(+), 60 deletions(-) create mode 100644 src/components/PDFSkeleton.tsx diff --git a/src/app/pdf/[id]/page.tsx b/src/app/pdf/[id]/page.tsx index 3c5c2d9..be8405c 100644 --- a/src/app/pdf/[id]/page.tsx +++ b/src/app/pdf/[id]/page.tsx @@ -12,29 +12,6 @@ export default function PDFViewerPage() { const document = getDocument(id as string); - if (!document) { - return ( -
-
-
-

Document not found. Please select a document from the home page.

-
- - - - - Back to Documents - -
-
-
-
- ); - } - return ( <>
@@ -48,10 +25,10 @@ export default function PDFViewerPage() { Documents -

{document.name}

+

{document?.name}

- + - ); + ); } diff --git a/src/components/PDFSkeleton.tsx b/src/components/PDFSkeleton.tsx new file mode 100644 index 0000000..db2b6d0 --- /dev/null +++ b/src/components/PDFSkeleton.tsx @@ -0,0 +1,37 @@ +import { useEffect, useState } from 'react'; + +export function PDFSkeleton() { + const [showNotification, setShowNotification] = useState(false); + + useEffect(() => { + const timer = setTimeout(() => { + setShowNotification(true); + }, 3000); + + return () => clearTimeout(timer); + }, []); + + return ( +
+ {showNotification && ( +
+ There might be an issue with the file import. Please try again. +
+ )} +
+ {/* Show 3 skeleton pages by default */} + {[1, 2, 3].map((index) => ( +
+ {/* Page content skeleton */} +
+
+ {/* Approximate dimensions of a PDF page */} +
+
+
+
+ ))} +
+
+ ); +} \ No newline at end of file diff --git a/src/components/PDFViewer.tsx b/src/components/PDFViewer.tsx index 36fcf0d..f0a65b0 100644 --- a/src/components/PDFViewer.tsx +++ b/src/components/PDFViewer.tsx @@ -3,52 +3,30 @@ import { Document, Page, pdfjs } from 'react-pdf'; import 'react-pdf/dist/Page/AnnotationLayer.css'; import 'react-pdf/dist/Page/TextLayer.css'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; +import { PDFSkeleton } from './PDFSkeleton'; // Set worker from public directory pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.mjs'; interface PDFViewerProps { - pdfFile: string; - highlightText?: string; // Text to highlight in the PDF + pdfFile: string | undefined; } -export function PDFViewer({ pdfFile, highlightText }: PDFViewerProps) { +export function PDFViewer({ pdfFile }: PDFViewerProps) { const [numPages, setNumPages] = useState(); function onDocumentLoadSuccess({ numPages }: { numPages: number }): void { setNumPages(numPages); } - // Function to highlight text in the PDF - const highlightPattern = (text: string, pattern: string) => { - if (!pattern) return text; - const regex = new RegExp(`(${pattern})`, 'gi'); - return text.replace(regex, '$1'); - }; - - // Add styles for highlighted text - useEffect(() => { - const style = document.createElement('style'); - style.textContent = ` - .react-pdf__Page__textContent mark { - background-color: yellow; - border-radius: 2px; - padding: 0; - margin: 0; - } - `; - document.head.appendChild(style); - return () => { - document.head.removeChild(style); - }; - }, []); - return (
} + noData={} className="flex flex-col items-center" > {Array.from( @@ -65,13 +43,8 @@ export function PDFViewer({ pdfFile, highlightText }: PDFViewerProps) { pageNumber={index + 1} renderAnnotationLayer={true} renderTextLayer={true} - className="rounded-xl shadow-lg" + className="shadow-lg" scale={1.2} - customTextRenderer={(textItem) => - highlightText ? - highlightPattern(textItem.str, highlightText) : - textItem.str - } />