hooks.ts


Overview

The hooks.ts file contains custom React hooks designed to manage and synchronize state related to graph nodes within a React application. These hooks primarily interact with a global graph state store (useGraphStore) to provide filtered node options and keep form fields in sync with node connections.

Specifically, this file exports two hooks:

These hooks help maintain consistency between the UI form elements and the underlying graph data, facilitating user interactions in workflows involving node connections.


Detailed Documentation

Imports


useBuildRelevantOptions

export const useBuildRelevantOptions = () => { ... }

Purpose

Creates a memoized function that generates a list of node options for dropdown menus, excluding nodes already selected in other related dropdowns.

Functionality

Parameters

Returns

Usage Example

const buildOptions = useBuildRelevantOptions();
const selectedIds = ['node1', 'node5'];

const options = buildOptions(selectedIds);
// options is now a list of nodes excluding 'node1' and 'node5'

Important Details


useWatchConnectionChanges

export const useWatchConnectionChanges = ({ nodeId, form }: IOperatorForm) => { ... }

Purpose

Monitors changes to a graph node’s connection targets and synchronizes the yes and no fields of a form accordingly.

Parameters

Returns

Functionality

Usage Example

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

This will keep the yes and no fields in operatorForm synchronized with the connections of node 'node123'.

Important Details


Implementation Details & Algorithms

No complex algorithms are used; the hooks mainly leverage React’s hooks for memoization and side effects combined with array filtering and object property picking.


Interaction with Other System Parts


Mermaid Diagram

flowchart TD
    A[useBuildRelevantOptions] --> B[get nodes from useGraphStore]
    B --> C[filter nodes excluding toList]
    C --> D[map nodes to {label, value} options]
    A --> E[returns buildRelevantOptions function]

    F[useWatchConnectionChanges] --> G[getNode(nodeId) from useGraphStore]
    G --> H[extract yes, no from node]
    H --> I[setFieldsValue on form with picked values]
    F --> J[useEffect to trigger watchFormChanges on node/form change]

Summary

The hooks.ts file provides two essential hooks for managing graph node selections and form synchronization in a React application. useBuildRelevantOptions helps generate filtered dropdown options excluding already selected nodes, while useWatchConnectionChanges ensures form fields reflect the current state of node connections. Both hooks rely on a global graph store and integrate closely with form and UI components to maintain consistency and improve user experience.