hooks.ts

Overview

The hooks.ts file defines a custom React hook named useHandleOperateParameters designed to manage parameter objects associated with a specific node in a graph-based state store. This hook abstracts the logic for retrieving, updating, adding, and removing parameters within a node's form data. It leverages React's memoization hooks (useMemo, useCallback) for performance optimization and integrates tightly with a centralized graph store (useGraphStore) to ensure state consistency across the application.

This hook is particularly useful in scenarios where nodes represent configurable entities or components, and each node holds a dynamic list of parameters that can be edited by users via a form interface.


Detailed Documentation

useHandleOperateParameters

useHandleOperateParameters(nodeId: string): {
  handleAdd: () => void;
  handleRemove: (id?: string) => () => void;
  handleComponentIdChange: (row: IGenerateParameter) => (value: string) => void;
  handleSave: (row: IGenerateParameter) => void;
  dataSource: IGenerateParameter[];
}

Description

A custom React hook that manages the lifecycle and operations on a node's parameters within a graph structure. It provides functions to add, remove, update, and save parameters for the node identified by nodeId. It also returns the current list of parameters (dataSource) for rendering or further operations.

Parameters

Returns

An object containing:

Usage Example

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

interface ParameterEditorProps {
  nodeId: string;
}

const ParameterEditor: React.FC<ParameterEditorProps> = ({ nodeId }) => {
  const {
    dataSource,
    handleAdd,
    handleRemove,
    handleComponentIdChange,
    handleSave,
  } = useHandleOperateParameters(nodeId);

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

Implementation Details


Interaction with Other System Parts


Mermaid Diagram: Flowchart of Functions and Data Flow

flowchart TD
    A[Start: Call useHandleOperateParameters(nodeId)]
    A --> B[getNode(nodeId) from useGraphStore]
    B --> C[Extract dataSource from node.data.form.parameters]
    C --> D{User Interaction}
    
    D -->|Add Parameter| E[handleAdd]
    E --> F[Generate UUID and add new parameter]
    F --> G[updateNodeForm(nodeId, updated parameters)]
    
    D -->|Remove Parameter| H[handleRemove(id)]
    H --> I[Filter out parameter with id]
    I --> G
    
    D -->|Change Component ID| J[handleComponentIdChange(row)]
    J --> K[Update component_id in parameter]
    K --> G
    
    D -->|Save Parameter| L[handleSave(row)]
    L --> M[Update parameter with new row data]
    M --> G

    G --> C

Diagram Explanation:


Summary

The hooks.ts file provides a focused, performant, and reusable React hook to manage a node's parameter list within a graph store. It abstracts common operations needed in UI forms for parameter manipulation while ensuring state consistency and immutability. Its design promotes clean component code by encapsulating logic, and its integration with the centralized store ensures synchronization across the system.