hooks.ts


Overview

The hooks.ts file provides a collection of custom React hooks used primarily for managing document-related workflows within a knowledge management or dataset application. These hooks abstract and encapsulate UI state management (e.g., modal visibility), asynchronous operations (e.g., API calls for creating, renaming, uploading documents), and navigation actions related to document handling, uploading, parsing, and crawling web content.

Each hook typically combines state management (using React's useState and useCallback) with API interaction hooks imported from shared modules like document-hooks and modal state hooks from common-hooks. This modular approach improves code reusability and keeps component code clean by separating concerns of document operations and UI state.


Exports: Detailed Explanation of Hooks

1. useNavigateToOtherPage

Purpose:
Provides navigation functions to switch between knowledge dataset upload pages and document chunk pages, incorporating the current knowledge context.

Functions:

Usage Example:

const { linkToUploadPage, toChunk } = useNavigateToOtherPage();
linkToUploadPage(); // navigates to upload page
toChunk('doc123'); // navigates to chunk page for document 'doc123'

2. useRenameDocument

Parameters:

Returns:

Description:
Manages modal visibility and handles renaming a document by calling saveName. If rename succeeds (returns code 0), modal is hidden.

Usage Example:

const { renameLoading, onRenameOk, renameVisible, showRenameModal, hideRenameModal } = useRenameDocument('docId');

showRenameModal();
if (!renameLoading) {
  await onRenameOk('New Document Name');
}

3. useCreateEmptyDocument

Returns:

Description:
Manages modal state and document creation process via createDocument hook. Hides modal on success.


4. useChangeDocumentParser

Parameters:

Returns:

Description:
Handles showing/hiding modal and changing the document parser config via setDocumentParser. Modal closes on success.


5. useGetRowSelection

Returns:

Description:
Manages selected row keys state for table row selection, exposing the selection object compatible with Ant Design tables.


6. useHandleUploadDocument

Returns:

Description:
This is a complex hook managing multi-step file upload with progress status updates and batch processing in chunks of 10 files. It updates UI file upload statuses, handles directory uploads, and optionally triggers parsing after upload. The upload is done using uploadDocument, and documents can be run/executed after upload via runDocumentByIds.

Important Algorithm Details:


7. useHandleWebCrawl

Returns:

Description:
Handles the modal and API interaction for crawling web content by name and URL, using webCrawl hook.


8. useHandleRunDocumentByIds

Parameters:

Returns:

Description:
Manages running/stopping document processing by IDs, with loading state scoped to a single document to avoid concurrent runs.


9. useShowMetaModal

Parameters:

Returns:

Description:
Manages modal visibility and setting document metadata via setDocumentMeta. Modal closes on success.


Implementation Details & Algorithms


Interaction with Other Parts of the System

The file acts as an intermediary layer that bridges UI components and API hooks, managing UI state, side effects, and user interactions related to knowledge documents and datasets.


Mermaid Diagram: Hook Structure and Relationships

flowchart TD
    A[useNavigateToOtherPage]
    B[useRenameDocument]
    C[useCreateEmptyDocument]
    D[useChangeDocumentParser]
    E[useGetRowSelection]
    F[useHandleUploadDocument]
    G[useHandleWebCrawl]
    H[useHandleRunDocumentByIds]
    I[useShowMetaModal]

    subgraph ModalState [Modal State Management]
      MS[useSetModalState]
    end

    subgraph DocumentHooks [Document API Hooks]
      CD[useCreateNextDocument]
      RN[useSaveNextDocumentName]
      UP[useUploadNextDocument]
      RP[useRunNextDocument]
      SP[useSetNextDocumentParser]
      SM[useSetDocumentMeta]
      WC[useNextWebCrawl]
    end

    subgraph Routing [Routing & Params]
      UN[useNavigate]
      GP[useGetKnowledgeSearchParams]
    end

    A --> UN
    A --> GP

    B --> RN
    B --> MS

    C --> CD
    C --> MS

    D --> SP
    D --> MS

    E

    F --> UP
    F --> RP
    F --> MS

    G --> WC
    G --> MS

    H --> RP

    I --> SM
    I --> MS

Summary

hooks.ts is a centralized utility file providing custom React hooks to manage document lifecycle actions such as navigation, creation, renaming, uploading, parsing, running, metadata updates, and web crawling. It cleverly combines modal state management, file upload batching, API interactions, and navigation to streamline UI logic related to knowledge dataset management in a React + Umi + Ant Design environment.

This modular design promotes maintainability and clarity by abstracting common document workflows into reusable hooks, easing integration into React components.