use-file-request.ts

Overview

The use-file-request.ts file provides a collection of React hooks designed to manage file system operations within a web application. Utilizing modern React Query techniques, these hooks enable asynchronous interactions with a back-end file management service to perform actions such as uploading files, moving files, creating folders, fetching file lists, deleting files, and retrieving parent folder information.

This file abstracts the complexity of API calls and state management, providing a clean and reactive interface for components to interact with the file system. It integrates with UI feedback mechanisms (e.g., success messages), routing, and pagination to create a seamless user experience when managing files and folders.

Detailed Explanation of Exports


enum FileApiAction

A TypeScript enumeration representing string constants for various API actions related to files. It is used as keys for React Query mutations and queries to uniquely identify each operation.

Member

Description

UploadFile

Action key for file upload

FetchFileList

Action key for fetching files

MoveFile

Action key for moving files

CreateFolder

Action key for creating folder

FetchParentFolderList

Action key for fetching parent folders

DeleteFile

Action key for deleting files


useGetFolderId(): string

A hook that reads the current folder ID from the URL search parameters.

Usage example:

const folderId = useGetFolderId();
console.log('Current folder ID:', folderId);

useUploadFile()

A hook providing functionality to upload files to a specified parent folder.

Usage example:

const { uploadFile, loading } = useUploadFile();

const handleUpload = async (files: File[], parentId: string) => {
  await uploadFile({ fileList: files, parentId });
};

interface IMoveFileBody

Defines the parameter structure for moving files.

interface IMoveFileBody {
  src_file_ids: string[];   // IDs of files to move
  dest_file_id: string;     // Target folder ID
}

useMoveFile()

A hook to move files from their current location to another folder.

Usage example:

const { moveFile } = useMoveFile();

moveFile({ src_file_ids: ['file1', 'file2'], dest_file_id: 'folder123' });

useCreateFolder()

A hook for creating a new folder within a specified parent folder.

Usage example:

const { createFolder } = useCreateFolder();

createFolder({ parentId: 'folder123', name: 'New Folder' });

useFetchParentFolderList()

Hook to fetch the list of all parent folders of the current folder, useful for breadcrumb navigation.

Usage example:

const parentFolders = useFetchParentFolderList();

interface IListResult

Defines the return type for useFetchFileList.

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

useFetchFileList(): IListResult & IFetchFileListResult

A hook to fetch the list of files in the current folder, with search and pagination support.

Usage example:

const {
  files,
  parent_folder,
  total,
  searchString,
  handleInputChange,
  pagination,
  setPagination,
  loading,
} = useFetchFileList();

useDeleteFile()

A hook to delete one or more files from a folder.

Usage example:

const { deleteFile } = useDeleteFile();

deleteFile({ fileIds: ['file1'], parentId: 'folder123' });

Important Implementation Details


Interaction with Other Parts of the Application


Visual Diagram: Hook Structure and Relationships

flowchart TD
    A[use-file-request.ts] --> B[useGetFolderId]
    A --> C[useUploadFile]
    A --> D[useMoveFile]
    A --> E[useCreateFolder]
    A --> F[useFetchParentFolderList]
    A --> G[useFetchFileList]
    A --> H[useDeleteFile]

    C -->|calls| I[fileManagerService.uploadFile]
    D -->|calls| J[fileManagerService.moveFile]
    E -->|calls| K[fileManagerService.createFolder]
    F -->|calls| L[fileManagerService.getAllParentFolder]
    G -->|calls| M[fileManagerService.listFile]
    H -->|calls| N[fileManagerService.removeFile]

    C -->|uses| O[React Query useMutation]
    D -->|uses| O
    E -->|uses| O
    F -->|uses| P[React Query useQuery]
    G -->|uses| P
    H -->|uses| O

    G -->|uses| Q[useDebounce] --> R[searchString]
    G -->|uses| S[useHandleSearchChange]
    G -->|uses| T[useGetPaginationWithRouter]

    C & E & H -->|uses| U[useSetPaginationParams]
    C & D & E & H -->|uses| V[useTranslation]
    C & D & E & H -->|uses| W[message]

    style A fill:#f9f,stroke:#333,stroke-width:2px

Summary

The use-file-request.ts file encapsulates the core file management API interactions through reusable React hooks, integrating React Query's powerful data-fetching capabilities with user interface feedback, localization, and routing synchronization. It acts as a bridge between the UI components and the back-end file management service, enabling efficient, reactive, and user-friendly file system operations within the application.