hooks.ts


Overview

The hooks.ts file provides several custom React hooks primarily aimed at enhancing document rendering and interaction within a React application. These hooks facilitate responsive document container sizing, dynamic text highlighting within documents, and retrieval of document URLs based on route parameters.

This file is designed to work with PDF-like documents or other text-heavy content rendered in a React environment, supporting features such as automatic container resizing and keyword highlighting for improved user experience.


Exports and Their Functionalities

1. useDocumentResizeObserver

A custom hook that tracks the width of a document container element and updates its value reactively as the container resizes.

Purpose:

To provide the current width of a container element and a reference setter to attach to that container, enabling responsive and adaptive UI components based on container size.

API:

const { containerWidth, setContainerRef } = useDocumentResizeObserver();

Implementation Details:

Usage Example:

const DocumentContainer = () => {
  const { containerWidth, setContainerRef } = useDocumentResizeObserver();

  return (
    <div ref={setContainerRef} style={{ width: '100%' }}>
      <p>Container width: {containerWidth}px</p>
      {/* Render document or PDF here, potentially adjusting layout based on containerWidth */}
    </div>
  );
};

2. useHighlightText

A custom hook that returns a text renderer function for highlighting search terms within a document's text.

Purpose:

To generate highlighted text snippets by wrapping matching text with HTML <mark> tags, enhancing search term visibility in rendered documents.

API:

const textRenderer: CustomTextRenderer = useHighlightText(searchText);

Implementation Details:

Usage Example:

import { useHighlightText } from './hooks';

const MyPdfViewer = ({ searchText }) => {
  const textRenderer = useHighlightText(searchText);

  return (
    <PDFViewer textRenderer={textRenderer} />
  );
};

3. useGetDocumentUrl

A custom hook that constructs and returns the full URL to retrieve a document based on route parameters.

Purpose:

To abstract the logic of forming the API endpoint URL for fetching a document, relying on the current route's documentId parameter.

API:

const url = useGetDocumentUrl();

Implementation Details:

Usage Example:

const DocumentDownloader = () => {
  const url = useGetDocumentUrl();

  useEffect(() => {
    fetch(url).then(...);
  }, [url]);

  return <a href={url} download>Download Document</a>;
};

Internal Helper Function: highlightPattern

A utility function for applying highlight markup to matching text strings.

function highlightPattern(text: string, pattern: string, pageNumber: number): string

Interaction with Other System Parts


Mermaid Diagram: Flowchart of Hook Relationships and Data Flow

flowchart TD
    A[useDocumentResizeObserver] --> B[containerRef (HTMLElement)]
    B --> C[useSize(containerRef)]
    C --> A
    A --> D[containerWidth (number)]

    E[useHighlightText(searchText)] --> F[textRenderer (CustomTextRenderer)]
    F --> G[highlightPattern(text, pattern, pageNumber)]

    H[useGetDocumentUrl] --> I[useGetKnowledgeSearchParams()]
    I --> J[documentId]
    J --> K[url = api_host + /document/get/ + documentId]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style E fill:#bbf,stroke:#333,stroke-width:2px
    style H fill:#bfb,stroke:#333,stroke-width:2px

Summary

The hooks.ts file encapsulates important UI and data-fetching utilities for document display and interaction in a React application:

These hooks are modular, reusable, and integrate with external utilities and routing logic, making them essential building blocks for document-centric features within the broader application.