use-rename-document.ts


Overview

The use-rename-document.ts file defines a custom React hook named useRenameDocument designed to manage the renaming process of a document within an application. It encapsulates the state and behaviors needed to display a rename modal dialog, handle user input for renaming, and persist the new document name via an asynchronous API request.

This hook provides a clean and reusable interface to integrate document renaming functionality into React components, abstracting away modal state management and server communication.


Detailed Explanation

useRenameDocument Hook

Purpose

useRenameDocument is a custom React hook that:

Imports Used

Hook Signature

export const useRenameDocument: () => {
  renameLoading: boolean;
  onRenameOk: (name: string) => Promise<void>;
  renameVisible: boolean;
  hideRenameModal: () => void;
  showRenameModal: (row: IDocumentInfo) => void;
  initialName?: string;
};

Internal State and Variables

Methods

Returned Object

The hook returns an object containing:


Type Alias

export type UseRenameDocumentShowType = Pick<
  ReturnType<typeof useRenameDocument>,
  'showRenameModal'
>;

Important Implementation Details


Interaction with Other Parts of the System


Usage Example

import React from 'react';
import { useRenameDocument } from './use-rename-document';

const DocumentList = ({ documents }) => {
  const { renameVisible, renameLoading, onRenameOk, hideRenameModal, showRenameModal, initialName } = useRenameDocument();

  return (
    <>
      {documents.map(doc => (
        <div key={doc.id}>
          <span>{doc.name}</span>
          <button onClick={() => showRenameModal(doc)}>Rename</button>
        </div>
      ))}

      {renameVisible && (
        <RenameModal
          initialName={initialName}
          loading={renameLoading}
          onOk={onRenameOk}
          onCancel={hideRenameModal}
        />
      )}
    </>
  );
};

Mermaid Diagram — Flowchart of Main Functions and Relationships

flowchart TD
    A[useRenameDocument Hook] --> B[useSetModalState Hook]
    A --> C[useSaveDocumentName Hook]
    A --> D[useState: record (IDocumentInfo)]
    A --> E[onRenameOk(name)]
    A --> F[handleShow(row)]
    E -->|calls| C.saveName({documentId, name})
    E -->|on success| B.hideModal()
    F -->|sets| D.record
    F -->|calls| B.showModal()

Summary

use-rename-document.ts is a concise utility hook providing a standardized and reactive way to rename documents in a React application. It cleanly integrates modal visibility management, asynchronous API calls, and per-document state handling, enabling components to easily implement rename functionality with minimal boilerplate.