use-build-options.ts


Overview

The use-build-options.ts file defines a custom React hook useBuildSubNodeOutputOptions that constructs and returns a structured list of output options for sub-nodes of a given node within a graph data structure. This hook is primarily used in the context of a node-based graph system, where each node can have children (sub-nodes) with outputs that need to be dynamically fetched and formatted for UI components such as dropdowns or selectors.

The functionality centers around filtering sub-nodes based on a parent node ID, excluding certain node types (specifically those labeled as IterationStart), and generating output options based on their form outputs. It leverages memoization for performance optimization and integrates with a centralized graph state store.


Detailed Explanation

Imports


Function: useBuildSubNodeOutputOptions

export function useBuildSubNodeOutputOptions(nodeId?: string): Array<{
  label: string;
  value: string;
  title: string;
  options: ReturnType<typeof buildOutputOptions>;
}>

Purpose

This React hook returns a list of output option objects representing the outputs of all valid sub-nodes of the node identified by nodeId.

Parameters

Returns

An array of objects, where each object represents a sub-node's output options structured as:

Behavior & Implementation Details

Usage Example

import { useBuildSubNodeOutputOptions } from './use-build-options';

function SubNodeOutputSelector({ parentNodeId }) {
  const outputOptions = useBuildSubNodeOutputOptions(parentNodeId);

  return (
    <select>
      {outputOptions.map((optionGroup) => (
        <optgroup label={optionGroup.label} key={optionGroup.value}>
          {optionGroup.options.map((opt) => (
            <option value={opt.value} key={opt.value} title={opt.title}>
              {opt.label}
            </option>
          ))}
        </optgroup>
      ))}
    </select>
  );
}

Important Implementation Details


Interaction with Other System Components


Mermaid Diagram

flowchart TD
    A[useBuildSubNodeOutputOptions(nodeId)] --> |calls| B[useGraphStore(state => state.nodes)]
    A --> |filters| C[Filter nodes where parentId = nodeId]
    C --> D[Exclude nodes with label === Operator.IterationStart]
    D --> E[Filter nodes with non-empty outputs]
    E --> F[Map nodes to output option objects]
    F --> G[Call buildOutputOptions on each node's outputs]
    G --> H[Return array of output option groups]

Summary

The use-build-options.ts file provides a specialized React hook that builds a structured set of output options from sub-nodes of a given node in a graph. It efficiently filters and processes nodes, excluding certain node types, and formats their outputs for UI consumption. It is a critical utility for components that allow users to select or interact with outputs of node subtrees in the graph, leveraging a centralized state store and modular helper functions.