hooks.ts


Overview

The hooks.ts file contains several custom React hooks and utility functions designed to support document-related UI features in a React application. These hooks primarily handle dynamic resizing of document containers, text highlighting within PDF documents, and generating URLs for fetching document data from an API.

The file facilitates key functionalities such as:

This modular approach using hooks promotes reusability and clean separation of concerns in UI logic related to documents.


Exports and Functionality Details

1. useDocumentResizeObserver

Description

A custom React hook that observes the size of a document container element and provides the current container width. It leverages the useSize hook from the ahooks library, which listens for resize events on DOM elements.

Parameters

Returns

An object with:

Usage Example

const { containerWidth, setContainerRef } = useDocumentResizeObserver();

return (
  <div ref={setContainerRef}>
    <SomeDocumentComponent width={containerWidth} />
  </div>
);

Implementation Details


2. highlightPattern

Description

A utility function that wraps matching text patterns in HTML <mark> tags for highlighting purposes, primarily used for PDF text highlighting.

Parameters

Returns

Usage Notes


3. useHighlightText

Description

A custom hook that returns a CustomTextRenderer callback for PDF text rendering, which highlights occurrences of a search string within the document.

Parameters

Returns

Usage Example

const highlightTextRenderer = useHighlightText("react");

<PdfViewer textRenderer={highlightTextRenderer} />

Implementation Details


4. useGetDocumentUrl

Description

A custom hook that builds the API URL for fetching a document by extracting the documentId from route parameters.

Parameters

Returns

Usage Example

const url = useGetDocumentUrl();
// url might be "https://api.example.com/document/get/12345"

Implementation Details


Important Implementation Details


Interaction with Other System Components


Mermaid Diagram

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

    E[highlightPattern(text, pattern, pageNumber)] -->|returns| F[highlighted string]

    G[useHighlightText(searchText)] --> H[highlightPattern]
    G --> I[CustomTextRenderer callback]

    J[useGetDocumentUrl] --> K[useGetKnowledgeSearchParams]
    J --> L[api_host]
    J --> M[documentId]
    J --> N[document URL string]

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style E fill:#ccf,stroke:#333,stroke-width:1px
    style G fill:#cfc,stroke:#333,stroke-width:1px
    style J fill:#fcf,stroke:#333,stroke-width:1px

Summary

The hooks.ts file is a utility-focused module providing essential hooks and helper functions for document-related UI features in a React application. It enables responsive container sizing, text highlighting in PDFs, and API URL generation based on routing context, integrating cleanly with other system parts like routing hooks, API configuration, and PDF rendering components. The code is well-structured, making good use of React hooks and TypeScript for maintainability and performance.