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
toList: string[]
An array of node IDs that should be excluded from the generated list of options.
Returns
An array of objects with the following shape:
{ label: string; value: string }
Wherelabelis the node's display name andvalueis the node's ID.
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
Uses
useGraphStoreto access the current list of nodes.Filters nodes to exclude those whose IDs appear in
toList.Maps filtered nodes to an object containing
labelandvalueproperties for UI dropdown rendering.Memoized with
useCallbackto avoid unnecessary recalculations unless thenodesarray changes.
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
params: IOperatorForm
An object containing:nodeId: string— The ID of the node to watch.form— An Ant Design form instance or similar, which supports thesetFieldsValuemethod.
Returns
void
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
Retrieves the node from the graph store using
getNode(nodeId).Defines
watchFormChangescallback to set the form fields "yes" and "no" to the current values from the node.Uses
useEffectto callwatchFormChangeswhenever the node or form changes.The commented-out code hints at a more complex monitoring of edges to dynamically update targets based on graph connections, but it is not currently active.
Important Implementation Details and Algorithms
Graph Store Integration: Both hooks interact with a centralized graph state managed by
useGraphStore. This store presumably exposes nodes and edges representing a graph or workflow.Memoization: The
buildRelevantOptionsfunction is memoized withuseCallbackto optimize performance by preventing unnecessary recalculations.Form Synchronization:
useWatchConnectionChangeskeeps UI form state synchronized with graph node data, ensuring reactive updates when the underlying graph changes.Filtering Logic: Filtering in
useBuildRelevantOptionsexcludes nodes already selected in other dropdowns, avoiding inconsistent or duplicate selections.Extensibility: The commented-out code suggests planned enhancements for dynamically tracking edges to update form fields based on graph connections, indicating a design prepared for more reactive graph workflows.
Interaction with Other Parts of the System
useGraphStore(Graph State Store):
This file relies heavily onuseGraphStoreto fetch graph nodes and node-specific data. The store is the single source of truth for the graph structure.IOperatorFormInterface:
The hooks accept or use types defined elsewhere (IOperatorForm), suggesting a typed form structure consistent across the application.UI Components:
These hooks are intended to be used in React components representing operator forms in a graph editor or workflow builder UI. They provide the logic to build dropdown options and keep form fields synchronized with graph changes.Lodash
pick:
Utilized to select only the relevant fields (yes,no) from the node object when updating the form, ensuring minimal and precise form state changes.
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:
The
useBuildRelevantOptionshook returnsbuildRelevantOptions, which filters and maps nodes to dropdown options.The
useWatchConnectionChangeshook gets the node, defines a callback to update form fields, and triggers this callback viauseEffect.
Summary
The hooks.ts file provides two key hooks for managing graph-based operator forms:
useBuildRelevantOptionsgenerates filtered dropdown options based on graph nodes, preventing duplicate selections.useWatchConnectionChangessynchronizes form fields with graph node connection data to maintain UI consistency.
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.