hooks.ts

Overview

The hooks.ts file exports a collection of custom React hooks primarily designed to facilitate file and folder management functionalities within a frontend application. These hooks abstract common UI and data management patterns such as modal state handling, file operations (create, rename, delete, move, upload), navigation between folders, and integration with external knowledge bases.

The hooks leverage React's useState, useCallback, and useMemo for state and performance optimizations, alongside utility hooks from libraries such as umi for routing and Ant Design for UI components. The hooks also interact with backend operations through imported API hooks (e.g., useRenameFile, useDeleteFile) encapsulating the actual data fetching and mutations.

This file acts as a middle layer connecting UI components with business logic and backend services, simplifying component code by encapsulating complex behaviors and state management related to file/folder operations.


Detailed Hook Descriptions

useGetFolderId

Purpose:
Extracts the current folder ID from the URL search parameters.

Parameters:
None.

Returns:

Usage Example:

const folderId = useGetFolderId();
console.log(folderId); // e.g., "12345"

useGetRowSelection

Purpose:
Manages the selection state of file rows in a table, providing selection control and disabling selection of files originating from the 'knowledgebase'.

Parameters:
None.

Returns:

Implementation Details:
Uses React's useState to track selected rows. Disables selection for rows where source_type === 'knowledgebase'.

Usage Example:

const { rowSelection, setSelectedRowKeys } = useGetRowSelection();

// Pass rowSelection to Ant Design Table component
<Table rowSelection={rowSelection} />

useNavigateToOtherFolder

Purpose:
Provides a function to navigate programmatically to a different folder by updating the URL.

Parameters:
None.

Returns:

Implementation Details:
Uses useNavigate from umi to navigate.

Usage Example:

const navigateToOtherFolder = useNavigateToOtherFolder();
navigateToOtherFolder('folder123'); // Navigates to /file?folderId=folder123

useRenameCurrentFile

Purpose:
Manages the state and modal logic for renaming a file.

Parameters:
None.

Returns:
An object containing:

Implementation Details:

Usage Example:

const {
  fileRenameLoading,
  initialFileName,
  onFileRenameOk,
  fileRenameVisible,
  hideFileRenameModal,
  showFileRenameModal,
} = useRenameCurrentFile();

showFileRenameModal(selectedFile);

useSelectBreadcrumbItems

Purpose:
Generates breadcrumb navigation items based on the current folder's parent hierarchy.

Parameters:
None.

Returns:

Implementation Details:
Fetches parent folder list via useFetchParentFolderList hook and transforms it into breadcrumb objects.

Usage Example:

const breadcrumbItems = useSelectBreadcrumbItems();
// [{ title: 'root', path: '/file?folderId=rootId' }, {...}]

useHandleCreateFolder

Purpose:
Handles the creation of a new folder within the current folder context, including modal state and operation status.

Parameters:
None.

Returns:
An object containing:

Implementation Details:
Uses current folder ID (useGetFolderId) as the parent for new folders.

Usage Example:

const {
  folderCreateLoading,
  onFolderCreateOk,
  folderCreateModalVisible,
  hideFolderCreateModal,
  showFolderCreateModal,
} = useHandleCreateFolder();

showFolderCreateModal();

useHandleDeleteFile

Purpose:
Provides functionality to delete selected files with a confirmation dialog.

Parameters:

Returns:

Implementation Details:
Shows a confirmation modal via useShowDeleteConfirm. Calls removeDocument mutation hook to delete files and clears selection on success.

Usage Example:

const { handleRemoveFile } = useHandleDeleteFile(selectedFileIds, setSelectedRowKeys);
handleRemoveFile();

useHandleUploadFile

Purpose:
Manages file upload modal and uploading process.

Parameters:
None.

Returns:
An object containing:

Implementation Details:
Uses current folder ID for upload destination. Uploads files via uploadFile mutation hook.

Usage Example:

const {
  fileUploadLoading,
  onFileUploadOk,
  fileUploadVisible,
  hideFileUploadModal,
  showFileUploadModal,
} = useHandleUploadFile();

showFileUploadModal();

useHandleConnectToKnowledge

Purpose:
Handles connecting a file to one or more knowledge bases, including modal state and connection logic.

Parameters:
None.

Returns:
An object containing:

Implementation Details:
Stores the selected file in local state. Maps kbs_info to extract knowledge base IDs.

Usage Example:

const {
  initialValue,
  connectToKnowledgeLoading,
  onConnectToKnowledgeOk,
  connectToKnowledgeVisible,
  hideConnectToKnowledgeModal,
  showConnectToKnowledgeModal,
} = useHandleConnectToKnowledge();

showConnectToKnowledgeModal(selectedFile);

useHandleBreadcrumbClick

Purpose:
Provides a handler to navigate when a breadcrumb element is clicked.

Parameters:
None.

Returns:

Implementation Details:
Uses useNavigate from umi.

Usage Example:

const { handleBreadcrumbClick } = useHandleBreadcrumbClick();
handleBreadcrumbClick('/file?folderId=123');

useHandleMoveFile

Purpose:
Manages moving files from one folder to another, including modal display and operation handling.

Parameters:

Returns:
An object containing:

Implementation Details:
Keeps track of source file IDs in state. Calls moveFile mutation hook and clears selection on success.

Usage Example:

const {
  moveFileLoading,
  onMoveFileOk,
  moveFileVisible,
  hideMoveFileModal,
  showMoveFileModal,
} = useHandleMoveFile(setSelectedRowKeys);

showMoveFileModal(['fileId1', 'fileId2']);

Important Implementation Details and Algorithms


Interaction with Other System Components

This file acts as a bridge between UI components (such as tables, modals, and navigation bars) and backend services, enabling a modular and maintainable approach to file management features.


Visual Diagram

flowchart TD
    useGetFolderId["useGetFolderId()"]
    useGetRowSelection["useGetRowSelection()"]
    useNavigateToOtherFolder["useNavigateToOtherFolder()"]
    useRenameCurrentFile["useRenameCurrentFile()"]
    useSelectBreadcrumbItems["useSelectBreadcrumbItems()"]
    useHandleCreateFolder["useHandleCreateFolder()"]
    useHandleDeleteFile["useHandleDeleteFile(fileIds, setSelectedRowKeys)"]
    useHandleUploadFile["useHandleUploadFile()"]
    useHandleConnectToKnowledge["useHandleConnectToKnowledge()"]
    useHandleBreadcrumbClick["useHandleBreadcrumbClick()"]
    useHandleMoveFile["useHandleMoveFile(setSelectedRowKeys)"]

    subgraph ModalStateHooks["Modal State Management"]
        useRenameCurrentFile
        useHandleCreateFolder
        useHandleUploadFile
        useHandleConnectToKnowledge
        useHandleMoveFile
    end

    subgraph FileOperations["File & Folder Operations"]
        useRenameCurrentFile
        useHandleCreateFolder
        useHandleDeleteFile
        useHandleUploadFile
        useHandleConnectToKnowledge
        useHandleMoveFile
    end

    subgraph Navigation["Navigation & Selection"]
        useGetFolderId
        useGetRowSelection
        useNavigateToOtherFolder
        useSelectBreadcrumbItems
        useHandleBreadcrumbClick
    end

    %% Relationships
    useRenameCurrentFile --> FileOperations
    useHandleCreateFolder --> FileOperations
    useHandleDeleteFile --> FileOperations
    useHandleUploadFile --> FileOperations
    useHandleConnectToKnowledge --> FileOperations
    useHandleMoveFile --> FileOperations

    useRenameCurrentFile --> ModalStateHooks
    useHandleCreateFolder --> ModalStateHooks
    useHandleUploadFile --> ModalStateHooks
    useHandleConnectToKnowledge --> ModalStateHooks
    useHandleMoveFile --> ModalStateHooks

    useNavigateToOtherFolder --> Navigation
    useSelectBreadcrumbItems --> Navigation
    useHandleBreadcrumbClick --> Navigation
    useGetRowSelection --> Navigation
    useGetFolderId --> Navigation

Summary

The hooks.ts file provides a comprehensive suite of reusable hooks that manage complex UI state and backend interactions for file and folder management in a React application. By abstracting logic into these hooks, the system achieves modularity, ease of maintenance, and clear separation between UI and data operations. The file integrates tightly with modal management utilities, API hooks, and navigation utilities to provide an efficient developer experience when building file management features.