use-upload-document.ts


Overview

The use-upload-document.ts file defines a custom React hook named useHandleUploadDocument that manages the state and workflow of uploading documents in a React application. This hook encapsulates modal visibility control, uploading documents to the backend, handling server responses including unsupported file types, and optionally triggering document parsing after upload.

This hook integrates user interface state management with asynchronous document upload logic and post-upload processing, providing a clean API for components that need to handle document uploads.


Detailed Explanation

useHandleUploadDocument Hook

Purpose

useHandleUploadDocument is a reusable hook that simplifies the process of uploading documents, handling modal visibility for the upload dialog, and optionally triggering document parsing after successful upload.

Returned Object

The hook returns an object with the following properties and functions:

Property / Method

Type

Description

documentUploadLoading

boolean

Indicates whether the document upload process is currently in progress.

onDocumentUploadOk

[(UploadFormSchemaType) => Promise<number

undefined>](/projects/311/73292)

documentUploadVisible

boolean

Controls the visibility state of the document upload modal.

hideDocumentUploadModal

() => void

Function to hide the document upload modal.

showDocumentUploadModal

() => void

Function to show the document upload modal.


Function: onDocumentUploadOk

async ({ fileList, parseOnCreation }: UploadFormSchemaType) => Promise<number | undefined>

Parameters

Description

This asynchronous callback handles the actual upload process when the user confirms document selection.

  1. Upload Documents: Calls uploadDocument(fileList) to upload all files.

  2. Check Upload Response:

    • If the response message is not a string, the function returns early.

    • If upload succeeded (code === 0) and parseOnCreation is true, it triggers runDocumentByIds to parse the newly uploaded documents.

  3. Handle Unsupported Files:

    • Uses getUnSupportedFilesCount on the response message to detect how many files failed due to unsupported types.

    • If some files failed (error code 500) but not all, it treats the upload as partially successful.

  4. Modal Visibility:

    • Hides the upload modal if the upload was successful or partially successful.

  5. Returns:

    • Returns the status code of the upload operation or undefined if early exit.

Return Value

Usage Example

const {
  onDocumentUploadOk,
  documentUploadLoading,
  documentUploadVisible,
  showDocumentUploadModal,
  hideDocumentUploadModal,
} = useHandleUploadDocument();

const handleUpload = async () => {
  const statusCode = await onDocumentUploadOk({
    fileList: selectedFiles,
    parseOnCreation: true,
  });

  if (statusCode === 0) {
    console.log('Upload and parsing successful');
  } else {
    console.error('Upload failed or partially failed');
  }
};

Implementation Details and Algorithms


Interaction with Other Parts of the System

This hook is designed to be integrated into components responsible for uploading documents, providing a clear and concise interface for managing the entire upload lifecycle including UI state and backend interaction.


Mermaid Diagram

flowchart TD
    A[useHandleUploadDocument Hook] --> B[useSetModalState]
    A --> C[useUploadNextDocument]
    A --> D[useRunDocument]
    A --> E[getUnSupportedFilesCount]

    B --> |provides| F(documentUploadVisible)
    B --> |provides| G(showDocumentUploadModal)
    B --> |provides| H(hideDocumentUploadModal)

    C --> |provides| I(uploadDocument)
    C --> |provides| J(loading)

    D --> |provides| K(runDocumentByIds)

    A --> |exposes| L(documentUploadLoading)
    A --> |exposes| M(onDocumentUploadOk)
    A --> |exposes| F
    A --> |exposes| H
    A --> |exposes| G

Summary

The use-upload-document.ts file provides a powerful hook, useHandleUploadDocument, for handling document upload workflows in a React application. It integrates modal state management, file uploading, error handling, and optional post-upload parsing into a single interface. This modular design allows for easy integration with UI components requiring document upload capabilities while abstracting away the complexity of backend interactions and UI state synchronization.