hooks.ts

Overview

The hooks.ts file contains several custom React hooks designed to facilitate document-related UI behaviors and data fetching within a React application. The main functionalities provided by this file include:

These hooks abstract common logic related to document rendering, resizing, and fetching, improving reusability and separation of concerns in the application.


Detailed Explanations

1. useDocumentResizeObserver

A React hook that tracks the width of a DOM element and updates state accordingly. It uses the useSize hook from the ahooks library to listen to size changes of a referenced container element.

Purpose

To provide responsive UI behavior by monitoring the width of a container element dynamically and exposing this width to consuming components.

Implementation Details

Return Value

An object with:

Usage Example

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

  return (
    <div ref={setContainerRef}>
      <p>The container width is: {containerWidth}px</p>
    </div>
  );
};

2. highlightPattern

A utility function that wraps matched text patterns with HTML <mark> tags to highlight them.

Parameters

Return Value

Implementation Details


3. useHighlightText

A React hook that returns a text renderer function compatible with react-pdf's custom text rendering API, applying text highlighting based on a search string.

Parameters

Return Value

Implementation Details

Usage Example

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

  return (
    <Document file="sample.pdf">
      <Page pageNumber={1} customTextRenderer={textRenderer} />
    </Document>
  );
};

4. useGetDocumentUrl

A hook that computes the URL from which a document can be fetched, based on the document ID extracted from route parameters.

Implementation Details

Return Value

Usage Example

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

  useEffect(() => {
    fetch(url)
      .then(response => response.blob())
      .then(blob => {
        // handle the document blob
      });
  }, [url]);

  return <div>Loading document...</div>;
};

Important Implementation Details and Algorithms


Interaction with Other System Parts


Mermaid Diagram

flowchart TD
    A[useDocumentResizeObserver] -->|uses| B[useSize (ahooks)]
    A --> C[State: containerWidth, containerRef]
    A --> D[Callback: onResize]
    A --> E[Effect: update containerWidth on size change]

    F[highlightPattern] --> G[Input: text, pattern, pageNumber]
    F --> H[Output: highlighted string]

    I[useHighlightText] --> J[useCallback: textRenderer]
    J -->|calls| F[highlightPattern]

    K[useGetDocumentUrl] --> L[useGetKnowledgeSearchParams]
    K --> M[useMemo: url based on documentId]
    K --> N[Returns: document fetch URL]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style F fill:#bbf,stroke:#333,stroke-width:2px
    style I fill:#f9f,stroke:#333,stroke-width:2px
    style K fill:#f9f,stroke:#333,stroke-width:2px

Summary

The hooks.ts file encapsulates reusable logic related to document UI and data fetching:

This modular design enables clean, maintainable, and efficient integration of document-related features in the broader React application.