hooks.ts


Overview

The hooks.ts file provides a set of custom React hooks designed to facilitate asynchronous fetching and rendering of document files—particularly Excel and DOCX formats—within a React application. These hooks abstract the complexities of API calls, error handling, and content previewing by leveraging popular libraries such as axios for HTTP requests, js-preview/excel for Excel previews, and mammoth for converting DOCX files to HTML.

These hooks are intended to be used in React functional components to simplify document retrieval and rendering workflows, providing convenient state management (loading status, errors, references to container elements) and side-effect handling.


Exports and Their Functionalities

1. useCatchError(api: string)

Purpose

Fetches a document from the provided API endpoint and tracks any error messages returned by the API response.

Parameters

Returns

Description

Usage Example

const { fetchDocument, error } = useCatchError('/api/document');
if (error) {
  return <div>Error: {error}</div>;
}
// Use fetchDocument() manually if needed

2. useFetchDocument()

Purpose

Provides a reusable function to fetch documents with authorization headers and expects the response as an ArrayBuffer.

Parameters

Returns

Description

Usage Example

const { fetchDocument } = useFetchDocument();
const response = await fetchDocument('/api/file.docx');
// response.data contains ArrayBuffer of the file

3. useFetchExcel(filePath: string)

Purpose

Fetches an Excel file from a given path, renders a preview inside a container, and tracks the loading status and errors.

Parameters

Returns

Description

Usage Example

const { status, containerRef, error } = useFetchExcel('/files/report.xlsx');

return (
  <>
    {error && <div>Error: {error}</div>}
    <div ref={containerRef} />
    {!status && <div>Failed to load Excel preview.</div>}
  </>
);

4. useFetchDocx(filePath: string)

Purpose

Fetches a DOCX file from a given path, converts it to HTML, and renders the content inside a container. Tracks success state and errors.

Parameters

Returns

Description

Usage Example

const { succeed, containerRef, error } = useFetchDocx('/files/document.docx');

if (error) return <div>Error: {error}</div>;
if (!succeed) return <div>Failed to load DOCX preview.</div>;

return <div ref={containerRef} />;

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Flowchart of Hook Functions and Their Relationships

flowchart TD
    A[useCatchError(api)] -->|calls| B[axios.get(api)]
    C[useFetchDocument()] -->|provides| D[fetchDocument(api)]
    E[useFetchExcel(filePath)] -->|uses| C
    E -->|uses| A
    E -->|renders| F[jsPreviewExcel]
    G[useFetchDocx(filePath)] -->|uses| C
    G -->|renders| H[mammoth.convertToHtml]

    subgraph Fetching
        B
        D
    end

    subgraph ExcelPreview
        F
    end

    subgraph DocxPreview
        H
    end

Summary

The hooks.ts file encapsulates complex document fetching and rendering logic into reusable hooks that manage asynchronous operations, error handling, and DOM manipulation. It supports seamless integration with authorization mechanisms and third-party preview libraries, enabling React components to easily display Excel and DOCX documents fetched from remote APIs.

This modular approach improves maintainability and code clarity by separating concerns and promoting hook-based stateful logic in functional components.