use-create-folder.ts
Overview
The use-create-folder.ts file defines a custom React hook useHandleCreateFolder that manages the creation of folders within a file system or document management context. It encapsulates the logic for displaying a modal dialog to create a folder, handling the asynchronous folder creation request, and managing the modal's visibility state.
This hook abstracts the UI state management and backend interaction required to create a folder, making it reusable and easy to integrate into components that require folder creation functionality.
Detailed Explanation
useHandleCreateFolder()
Description
useHandleCreateFolder is a custom React hook that provides state and handlers for creating a new folder. It controls the visibility of the folder creation modal, handles the creation request via an API hook, and provides feedback on the loading state.
Returns
An object containing:
Property Name | Type | Description |
|---|---|---|
|
| Indicates if the folder creation request is in progress. |
| Callback function invoked when the user confirms folder creation with the folder name. | |
|
| Controls the visibility of the folder creation modal. |
| Function to hide the folder creation modal. | |
| Function to show the folder creation modal. |
Parameters
This hook does not take any parameters.
Usage Example
import React from 'react';
import { useHandleCreateFolder } from './use-create-folder';
const FolderCreationComponent = () => {
const {
folderCreateLoading,
onFolderCreateOk,
folderCreateModalVisible,
hideFolderCreateModal,
showFolderCreateModal,
} = useHandleCreateFolder();
const handleCreateClick = () => {
showFolderCreateModal();
};
const handleConfirm = (folderName: string) => {
onFolderCreateOk(folderName);
};
return (
<>
<button onClick={handleCreateClick}>Create Folder</button>
{folderCreateModalVisible && (
<FolderCreateModal
loading={folderCreateLoading}
onConfirm={handleConfirm}
onCancel={hideFolderCreateModal}
/>
)}
</>
);
};
Implementation Details
Modal State Management: The hook uses
useSetModalState()to manage the modal visibility (visible), and provides functionsshowModalandhideModalto toggle visibility. This is a reusable hook designed for generic modal state management.Folder Creation API Call: The hook relies on
useCreateFolder()which exposes acreateFolderfunction and aloadingstate. ThecreateFolderfunction sends a request to create a folder with provided parameters (parentIdandname).Current Folder Context: The hook uses
useGetFolderId()to retrieve the current folder ID, which is used as theparentIdfor the new folder creation. This ensures the new folder is created under the correct parent directory.Asynchronous Handling: The
onFolderCreateOkfunction is wrapped withuseCallbackto memoize it and prevent unnecessary re-renders. It asynchronously callscreateFolderand, upon successful creation (indicated byret === 0), hides the modal.Return Object: The returned object provides all necessary variables and functions to control the folder creation UI and logic from a consuming component.
Interaction with Other System Parts
Hooks Imported:
useSetModalState(from@/hooks/common-hooks): Provides modal visibility state and toggle functions.useCreateFolder(from@/hooks/use-file-request): Handles the API request for creating folders.useGetFolderId(local import from./hooks): Retrieves the current folder context (ID) for creation.
UI Components (Expected):
The hook is designed to be used in UI components that render a modal dialog for folder creation, such as a
FolderCreateModalcomponent, which will use the hook's return values to control display and trigger folder creation.Folder Structure Management:
This hook is part of a file/folder management system, facilitating folder creation within a nested structure by associating new folders with their parent folders.
Visual Diagram
flowchart TD
A[useHandleCreateFolder Hook] --> B[useSetModalState Hook]
A --> C[useCreateFolder Hook]
A --> D[useGetFolderId Hook]
A --> E{onFolderCreateOk(name: string)}
E -->|Calls| C.createFolder({parentId: id, name})
E -->|On success (ret === 0)| B.hideModal()
B -->|Provides| folderCreateModalVisible
B -->|Provides| showFolderCreateModal()
B -->|Provides| hideFolderCreateModal()
C -->|Provides| loading
D -->|Provides| id (current folder id)
A -->|Returns| folderCreateLoading
A -->|Returns| onFolderCreateOk
A -->|Returns| folderCreateModalVisible
A -->|Returns| hideFolderCreateModal
A -->|Returns| showFolderCreateModal
Summary
use-create-folder.ts defines a single custom hook
useHandleCreateFolderthat orchestrates modal state management, folder creation API calls, and contextual parent folder identification.It simplifies folder creation workflows for UI components by providing loading states, modal visibility controls, and an asynchronous callback to create folders.
The file depends on several reusable hooks, promoting separation of concerns and code reuse.
This hook is integral to the folder management UI workflow, enabling users to create new folders within a hierarchical folder structure efficiently and with feedback.