hooks.ts

Overview

The hooks.ts file provides custom React hooks designed to interact with a graph-based state management store (useGraphStore). It primarily facilitates building dropdown options dynamically based on nodes in a graph, and synchronizing form fields with changes in the graph's connections. These hooks are meant to be used within operator form components that represent nodes in a graph workflow, helping maintain consistency between the graph's data and the UI form state.


Exports

useBuildRelevantOptions

A hook that returns a memoized function to generate a list of dropdown options filtered by excluding a given list of node IDs. This is useful to avoid selecting the same node multiple times across different dropdowns within the UI.

Signature

const buildRelevantOptions: (toList: string[]) => { label: string; value: string }[]

Parameters

Returns

Usage Example

const buildOptions = useBuildRelevantOptions();

const excludedNodeIds = ['node1', 'node2'];

const options = buildOptions(excludedNodeIds);
// options will include all nodes except 'node1' and 'node2', formatted as dropdown options

Implementation Details


useWatchConnectionChanges

A hook that monitors changes in the graph node's connections and synchronizes the "yes" and "no" fields of a form accordingly. This ensures that the form reflects the current state of the node's connections.

Signature

function useWatchConnectionChanges(params: IOperatorForm): void

Parameters

Returns

Usage Example

useWatchConnectionChanges({ nodeId: 'node123', form: operatorFormInstance });

This hook should be called inside a React component that manages a form representing a graph node. It will update the form fields "yes" and "no" whenever the underlying node data changes.

Implementation Details


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram

The following flowchart illustrates the relationship and workflow between the main functions and hooks within hooks.ts.

flowchart TD
    A[useBuildRelevantOptions Hook]
    B[buildRelevantOptions Function]
    C[Filter nodes excluding toList IDs]
    D[Map filtered nodes to {label, value}]
    E[Return options array]

    F[useWatchConnectionChanges Hook]
    G[getNode from useGraphStore]
    H[watchFormChanges Callback]
    I[Set form fields: yes, no]
    J[useEffect to trigger watchFormChanges]

    A --> B
    B --> C
    C --> D
    D --> E

    F --> G
    G --> H
    H --> I
    J --> H

Diagram Explanation:


Summary

The hooks.ts file provides two key hooks for managing graph-based operator forms:

These hooks rely on a shared graph store and are intended for use within React components that represent nodes in a graph workflow editor UI. The file emphasizes performance optimization and clean UI-state synchronization with underlying graph data.