file-manager-hooks.ts


Overview

This file contains a set of React hooks designed to interact with a file management system in a web application. These hooks encapsulate the logic for fetching, manipulating, and managing files and folders via asynchronous API calls, primarily using the React Query library for server state management and caching.

The hooks abstract complex operations such as:

This modular approach enables reusable, declarative interaction with the file management backend services while integrating smoothly with UI components.


Detailed Explanation of Exports

1. useGetFolderId

export const useGetFolderId = () => string;
const folderId = useGetFolderId();
console.log(`Current folder ID is: ${folderId}`);

2. IListResult Interface

export interface IListResult {
  searchString: string;
  handleInputChange: React.ChangeEventHandler<HTMLInputElement>;
  pagination: PaginationProps;
  setPagination: (pagination: { page: number; pageSize: number }) => void;
  loading: boolean;
}

3. useFetchPureFileList

export const useFetchPureFileList: () => {
  loading: boolean;
  fetchList: (parentId: string) => Promise<any>;
};
const { loading, fetchList } = useFetchPureFileList();
const files = await fetchList('folder123');

4. useFetchFileList

export const useFetchFileList: () => ResponseType<any> & IListResult;
const {
  data,
  searchString,
  handleInputChange,
  pagination,
  setPagination,
  loading,
} = useFetchFileList();

// Use in UI to display files and handle search & pagination

5. useDeleteFile

export const useDeleteFile: () => {
  data: any;
  loading: boolean;
  deleteFile: (params: { fileIds: string[]; parentId: string }) => Promise<number>;
};
const { deleteFile, loading } = useDeleteFile();
await deleteFile({ fileIds: ['file1'], parentId: 'folder1' });

6. useDownloadFile

export const useDownloadFile: () => {
  data: any;
  loading: boolean;
  downloadFile: (params: { id: string; filename?: string }) => Promise<void>;
};
const { downloadFile } = useDownloadFile();
downloadFile({ id: 'file123', filename: 'document.pdf' });

7. useRenameFile

export const useRenameFile: () => {
  data: any;
  loading: boolean;
  renameFile: (params: { fileId: string; name: string }) => Promise<number>;
};
const { renameFile } = useRenameFile();
renameFile({ fileId: 'file1', name: 'newName.txt' });

8. useFetchParentFolderList

export const useFetchParentFolderList: () => IFolder[];
const parents = useFetchParentFolderList();
console.log(parents);

9. useCreateFolder

export const useCreateFolder: () => {
  data: any;
  loading: boolean;
  createFolder: (params: { parentId: string; name: string }) => Promise<number>;
};
const { createFolder } = useCreateFolder();
createFolder({ parentId: 'folder1', name: 'New Folder' });

10. useUploadFile

export const useUploadFile: () => {
  data: any;
  loading: boolean;
  uploadFile: (params: { fileList: UploadFile[]; parentId: string }) => Promise<number | void>;
};
const { uploadFile } = useUploadFile();
uploadFile({ fileList: filesArray, parentId: 'folder1' });

11. useConnectToKnowledge

export const useConnectToKnowledge: () => {
  data: any;
  loading: boolean;
  connectFileToKnowledge: (params: IConnectRequestBody) => Promise<number>;
};
const { connectFileToKnowledge } = useConnectToKnowledge();
connectFileToKnowledge({ fileId: 'file1', knowledgeId: 'knowledge123' });

12. useMoveFile

export interface IMoveFileBody {
  src_file_ids: string[];
  dest_file_id: string;
}

export const useMoveFile: () => {
  data: any;
  loading: boolean;
  moveFile: (params: IMoveFileBody) => Promise<number>;
};
const { moveFile } = useMoveFile();
moveFile({ src_file_ids: ['file1'], dest_file_id: 'folder2' });

Important Implementation Details


Interaction with Other System Parts


Mermaid Diagram

flowchart TD
    subgraph Hooks
        A[useGetFolderId]
        B[useFetchPureFileList]
        C[useFetchFileList]
        D[useDeleteFile]
        E[useDownloadFile]
        F[useRenameFile]
        G[useFetchParentFolderList]
        H[useCreateFolder]
        I[useUploadFile]
        J[useConnectToKnowledge]
        K[useMoveFile]
    end

    subgraph Services
        S[fileManagerService]
    end

    subgraph Utils
        U[downloadFileFromBlob]
    end

    A --> C
    B --> S
    C --> S
    D --> S
    E --> S
    E --> U
    F --> S
    G --> S
    H --> S
    I --> S
    J --> S
    K --> S

    style Hooks fill:#f9f,stroke:#333,stroke-width:1px
    style Services fill:#bbf,stroke:#333,stroke-width:1px
    style Utils fill:#bfb,stroke:#333,stroke-width:1px

Summary

This file provides a comprehensive suite of React hooks to manage file operations in a frontend application, integrating tightly with backend services and UI components. It abstracts complexities such as API interaction, pagination, search state management, and user feedback into reusable hooks, promoting clean and maintainable code in the file manager feature.