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

folderCreateLoading

boolean

Indicates if the folder creation request is in progress.

onFolderCreateOk

(name: string) => Promise

Callback function invoked when the user confirms folder creation with the folder name.

folderCreateModalVisible

boolean

Controls the visibility of the folder creation modal.

hideFolderCreateModal

() => void

Function to hide the folder creation modal.

showFolderCreateModal

() => void

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


Interaction with Other System Parts


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