hooks.ts


Overview

The hooks.ts file defines a custom React hook named useHandleOperateParameters designed to manage and manipulate a list of parameters associated with a specific node in a graph-based data structure. This hook provides a set of handlers to add, remove, update, and save parameter variables tied to a node's form data, abstracting the complexity of state management and interaction with a centralized store (useGraphStore).

The primary purpose of this hook is to facilitate controlled updates to node parameters within a React component, ensuring consistent state synchronization and providing convenient event handlers for UI components such as input fields and buttons.


Detailed Explanation

useHandleOperateParameters(nodeId: string)

Description

A React hook that manages the parameters (variables) of a node identified by nodeId. It retrieves the node's current parameters from a global graph store, provides handlers to update these parameters, and exposes the current list of parameters (dataSource) for rendering or further processing.

Parameters

Returns

An object containing:

Usage Example

import React from 'react';
import { useHandleOperateParameters } from './hooks';

const NodeParametersEditor = ({ nodeId }: { nodeId: string }) => {
  const {
    handleAdd,
    handleRemove,
    handleComponentIdChange,
    handleValueChange,
    handleSave,
    dataSource,
  } = useHandleOperateParameters(nodeId);

  return (
    <div>
      {dataSource.map((param) => (
        <div key={param.id}>
          <input
            value={param.key}
            onChange={(e) => handleSave({ ...param, key: e.target.value })}
          />
          <input
            value={param.component_id || ''}
            onChange={(e) => handleComponentIdChange(param)(e.target.value)}
          />
          <input
            value={param.value}
            onChange={handleValueChange(param)}
          />
          <button onClick={handleRemove(param.id)}>Remove</button>
        </div>
      ))}
      <button onClick={handleAdd}>Add Parameter</button>
    </div>
  );
};

Implementation Details


Interaction with Other System Parts


Mermaid Diagram

This flowchart illustrates the main functions returned by useHandleOperateParameters and their relationships to the dataSource state and the global store methods.

flowchart TD
    A[useHandleOperateParameters(nodeId)]
    A --> B[dataSource (parameters list)]
    A --> C[handleAdd]
    A --> D[handleRemove(id)]
    A --> E[handleComponentIdChange(row)]
    A --> F[handleValueChange(row)]
    A --> G[handleSave(row)]

    C -->|Adds new param| H[updateNodeForm(nodeId, new variables)]
    D -->|Removes param by id| H
    E -->|Changes component_id of param| H
    F -->|Changes value of param| H
    G -->|Saves updated param| H

Summary

The hooks.ts file provides a reusable React hook useHandleOperateParameters that abstracts complex state management logic for node parameters in a graph-based UI. It offers a clean API to manipulate parameter variables, ensuring immutability, synchronization with a global store, and convenient event handlers for UI integration. This hook is a critical part of the user interaction layer for nodes within the larger graph or flow editor system.