use-change-node-name.ts

Overview

This file provides React hooks for managing the renaming of nodes within a graph-based user interface, specifically focusing on two node types: generic nodes and "tool" nodes. It handles user input for node names, validates uniqueness and non-emptiness, updates application state accordingly, and provides user feedback on invalid inputs.

The main functionality includes:

Detailed Explanation

Imports


useHandleTooNodeNameChange

Purpose

A custom hook to handle name changes specifically for "tool" nodes—subcomponents attached to a parent node. It performs validation, updates tool names in the node data, and manages error reporting.

Parameters

Returns

Implementation Details

Usage Example

const { handleToolNameBlur, previousToolName } = useHandleTooNodeNameChange({
  id: "parentNodeId",
  name: currentToolName,
  setName: setCurrentToolName,
});

// In input element:
// <input value={currentToolName} onBlur={handleToolNameBlur} />

useHandleNodeNameChange

Purpose

A custom hook to handle renaming of general nodes, including tool nodes by internally delegating to useHandleTooNodeNameChange. It manages local state for the node name, validates uniqueness, updates store state, and handles input changes.

Parameters

Returns

Implementation Details

Usage Example

const { name, handleNameBlur, handleNameChange } = useHandleNodeNameChange({
  id: "nodeId",
  data: nodeData,
});

// In input element:
// <input value={name} onBlur={handleNameBlur} onChange={handleNameChange} />

Important Implementation Details and Algorithms


Interaction with Other System Parts

These hooks act as controllers between the UI and the graph state, ensuring consistent and validated updates.


Mermaid Diagram

flowchart TD
    A[useHandleNodeNameChange] --> B{Is Tool Node?}
    B -- Yes --> C[useHandleTooNodeNameChange]
    B -- No --> D[handleNameBlur]
    C --> E[handleToolNameBlur]
    A --> F[handleNameChange]
    D --> G[Validate Name Uniqueness & Non-empty]
    E --> H[Validate Tool Name Uniqueness & Non-empty]
    G --> I[Call updateNodeName in store]
    H --> J[Call updateNodeForm in store]
    I & J --> K[Update Graph Store]
    K --> L[Reflect changes in UI]

Summary

This file encapsulates the logic needed for renaming nodes and their sub-tools within a graph UI, ensuring data integrity and a smooth user experience. It cleanly separates tool node name handling, validates input, and updates centralized state while providing helpful error messages. The hooks here are essential for maintaining consistent and user-friendly node naming throughout the application.