use-move-file.ts


Overview

The use-move-file.ts file defines a custom React hook named useHandleMoveFile that encapsulates the logic required to move one or multiple files within a file management UI context. This hook manages modal visibility for the move operation, handles the move request via an API hook, and maintains state related to the files being moved and whether the move is a bulk operation.

This hook is designed to be integrated into React components that require file moving functionality, providing a clean interface to open/close the move modal, execute the move operation, and manage related UI states such as loading indicators.


Detailed Explanation

useHandleMoveFile Hook

useHandleMoveFile({
  clearRowSelection,
}: Pick<UseRowSelectionType, 'clearRowSelection'>)

Purpose

Parameters

Returned Object

The hook returns the following properties and functions:

Name

Type

Description

initialValue

string

Initial value for the move modal input (empty string).

moveFileLoading

boolean

Boolean flag indicating whether the move file operation is in progress (loading state).

onMoveFileOk

function

Async function to execute the move operation. Takes a target folder ID and returns a status code.

moveFileVisible

boolean

Boolean flag indicating whether the move file modal is currently visible.

hideMoveFileModal

function

Function to hide the move file modal.

showMoveFileModal

function

Function to show the move file modal and set the files to be moved.

Internal State & Variables

Methods

onMoveFileOk
async function onMoveFileOk(targetFolderId: string): Promise<number>
handleShowMoveFileModal
function handleShowMoveFileModal(ids: string[], isBulk = false): void

Usage Example

import React from 'react';
import { useHandleMoveFile } from './use-move-file';
import { useRowSelection } from '@/hooks/logic-hooks/use-row-selection';

const FileMoveComponent = () => {
  const { clearRowSelection } = useRowSelection();
  const {
    moveFileVisible,
    showMoveFileModal,
    hideMoveFileModal,
    onMoveFileOk,
    moveFileLoading,
  } = useHandleMoveFile({ clearRowSelection });

  const onUserInitiatedMove = () => {
    // For example, move selected files
    showMoveFileModal(['fileId1', 'fileId2'], true);
  };

  const onConfirmMove = async (targetFolderId: string) => {
    const result = await onMoveFileOk(targetFolderId);
    if (result === 0) {
      // Move successful
    }
  };

  return (
    <>
      {/* UI components that trigger move modal */}
      {moveFileVisible && (
        <MoveFileModal
          loading={moveFileLoading}
          onOk={onConfirmMove}
          onCancel={hideMoveFileModal}
        />
      )}
      <button onClick={onUserInitiatedMove}>Move Files</button>
    </>
  );
};

Important Implementation Details


Interaction with Other Parts of the System


Type Exports


Mermaid Diagram

This flowchart illustrates the main functions in the useHandleMoveFile hook and their relationships:

flowchart TD
    A[useHandleMoveFile Hook] --> B[useSetModalState]
    A --> C[useMoveFile]
    A --> D[sourceFileIds State]
    A --> E[isBulkRef Ref]

    subgraph ModalControl
        B --> moveFileVisible["moveFileVisible (boolean)"]
        B --> showMoveFileModal["showMoveFileModal()"]
        B --> hideMoveFileModal["hideMoveFileModal()"]
    end

    subgraph MoveOperation
        C --> moveFile["moveFile()"]
        C --> loading["loading (boolean)"]
    end

    A --> F[handleShowMoveFileModal(ids[], isBulk)]
    A --> G[onMoveFileOk(targetFolderId)]

    F -->|sets| E
    F -->|sets| D
    F -->|calls| B.showMoveFileModal

    G -->|calls| C.moveFile
    G -->|on success| B.hideMoveFileModal
    G -->|if bulk| clearRowSelection

    classDef hook fill:#f9f,stroke:#333,stroke-width:1px;
    class A hook;

Summary

The use-move-file.ts file provides a focused and reusable React hook for handling the "move file" functionality in a file management system. It cleanly abstracts modal management, API interaction, and state handling related to moving files, supporting both single and bulk operations. The hook integrates with other system hooks such as modal state management and file selection state, allowing for composability and maintainability in complex UI workflows.