use-change-node-name.ts


Overview

The use-change-node-name.ts file provides React hooks focused on managing and validating node names within a graph-based UI application. It primarily addresses two scenarios:

These hooks ensure that node names are unique within their respective contexts and sync updates properly with the global graph state managed through a store (useGraphStore). They also provide user feedback when invalid or duplicate names are entered.


Detailed Explanation

Imports and Dependencies


Exported Hooks

1. useHandleTooNodeNameChange

Handles name changes specifically for a "tool" node's individual tool.

Signature

useHandleTooNodeNameChange(params: {
  id?: string;
  name?: string;
  setName: Dispatch<SetStateAction<string>>;
}): {
  handleToolNameBlur: () => void;
  previousToolName: string | undefined;
}

Parameters

Returns

Functionality

Usage Example

const { handleToolNameBlur, previousToolName } = useHandleTooNodeNameChange({
  id: 'node123',
  name: currentName,
  setName: setCurrentName,
});

// Usage in input:
// <input value={currentName} onBlur={handleToolNameBlur} onChange={...} />

2. useHandleNodeNameChange

Handles name changes for general nodes or delegates to the tool-specific handler if the node is a tool node.

Signature

useHandleNodeNameChange(params: {
  id?: string;
  data: any;
}): {
  name: string;
  handleNameBlur: () => void;
  handleNameChange: (e: ChangeEvent<any>) => void;
}

Parameters

Returns

Functionality

Usage Example

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

// Usage in input:
// <input value={name} onBlur={handleNameBlur} onChange={handleNameChange} />

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Flowchart of Hook Functions and Their Relationships

flowchart TD
    A[useHandleNodeNameChange] -->|Calls internally| B[useHandleTooNodeNameChange]
    A -->|Uses| C[useGraphStore]
    B -->|Uses| C
    B -->|Uses| D[getAgentNodeTools]
    A -->|Handles| E[General Node Name Change]
    B -->|Handles| F[Tool Node Name Change]
    A -->|Exposes| G[name, handleNameBlur, handleNameChange]
    B -->|Exposes| H[handleToolNameBlur, previousToolName]

Summary

The use-change-node-name.ts file provides robust, reusable React hooks for managing node and tool name changes within a graph application. It enforces validation rules, integrates with a global graph store, and supports clear user feedback for invalid inputs. These hooks modularize the complex logic involved in node renaming, making them easy to integrate into UI components and maintain over time.