pdf-preview.tsx


Overview

pdf-preview.tsx is a React functional component designed to render a PDF document with interactive text and area highlights. It leverages the third-party library react-pdf-highlighter to enable PDF loading, rendering, and user interactions such as highlighting text or areas in the PDF. The component also supports displaying popup comments on highlights and integrates with application-specific hooks and UI elements for error handling and loading states.

This file primarily serves as a PDF previewer with highlight visualization, allowing users to view a PDF document, see existing highlights, and interact with them by hovering to reveal comments. It is memoized for performance optimization, avoiding unnecessary re-renders.


Detailed Description

Interfaces

IProps

interface IProps {
  highlights: IHighlight[];
  setWidthAndHeight: (width: number, height: number) => void;
  url: string;
}

Components and Functions

HighlightPopup

const HighlightPopup = ({
  comment,
}: {
  comment: { text: string; emoji: string };
}) => JSX.Element | null

Example:

<HighlightPopup comment={{ text: "Important note", emoji: "📝" }} />

PdfPreview

const PdfPreview = ({ highlights: state, setWidthAndHeight, url }: IProps) => JSX.Element

Internal Implementation Details


Usage Example

import PdfPreview from './pdf-preview';

const highlights = [
  {
    position: { /* position data */ },
    content: { text: "Example highlight" },
    comment: { text: "Important section", emoji: "⭐" },
    // other IHighlight properties...
  }
];

const handleSetDimensions = (width: number, height: number) => {
  console.log("PDF page dimensions:", width, height);
};

function App() {
  return (
    <PdfPreview
      url="https://example.com/sample.pdf"
      highlights={highlights}
      setWidthAndHeight={handleSetDimensions}
    />
  );
}

Interaction with Other System Parts

This component is likely embedded inside a document viewer page or a previewer UI where PDF documents are rendered with annotations.


Important Implementation Notes


Mermaid Diagram

componentDiagram
    component PdfPreview {
        +highlights: IHighlight[]
        +setWidthAndHeight(width: number, height: number): void
        +url: string
        -ref: React.Ref<(highlight: IHighlight) => void>
        -error: any
        -resetHash(): void
    }
    component PdfLoader {
        +url: string
        +beforeLoad: JSX.Element
        +workerSrc: string
        +errorMessage: JSX.Element
    }
    component PdfHighlighter {
        +pdfDocument: PDFDocumentProxy
        +enableAreaSelection(event): boolean
        +onScrollChange(): void
        +scrollRef(callback): void
        +onSelectionFinished(): void
        +highlightTransform(...)
        +highlights: IHighlight[]
    }
    component HighlightPopup {
        +comment: { text: string, emoji: string }
    }
    PdfPreview --> PdfLoader: Loads PDF
    PdfLoader --> PdfHighlighter: Provides pdfDocument
    PdfHighlighter --> HighlightPopup: Displays comment popup

Summary

pdf-preview.tsx is a React component for rendering PDFs with interactive highlights and popups. It integrates PDF loading, error handling, and highlight rendering using react-pdf-highlighter. This component is an integral UI piece in a document viewer application, enabling users to view and interact with annotations on PDF files.