form-hooks.ts


Overview

The form-hooks.ts file provides a set of custom React hooks designed to facilitate form-related interactions within a graph-based UI, particularly for managing node connections and option selections in a flow or graph editor. It leverages state management from a centralized graph store (useGraphStore) and supports localized UI text through translation hooks.

This file's primary responsibilities include:

These hooks are intended for use in React components that manage graph nodes and their interconnections, enabling intuitive user interactions and maintaining consistent graph state.


Detailed Explanations

Imports


Hook: useBuildFormSelectOptions

export const useBuildFormSelectOptions = (
  operatorName: Operator,
  selfId?: string,
) => { ... }

Purpose

Generates a memoized function that builds a list of selectable options for a form dropdown, representing nodes in the graph that can be connected to the current node. It filters out nodes based on operator restrictions and prevents selection of the current node or already selected nodes in other dropdowns.

Parameters

Returns

Usage Example

const buildOptions = useBuildFormSelectOptions(Operator.Filter, currentNodeId);
const options = buildOptions(selectedTargetIds);

Where selectedTargetIds is an array of node IDs already selected in other related form fields to avoid duplicates.


Hook: useHandleFormSelectChange

export const useHandleFormSelectChange = (nodeId?: string) => { ... }

Purpose

Provides a memoized callback function to handle changes in form select inputs by adding or removing edges (connections) between nodes in the graph store.

Parameters

Returns

Behavior

Usage Example

const { handleSelectChange } = useHandleFormSelectChange(currentNodeId);

<select onChange={e => handleSelectChange('output')(e.target.value)}>
  {options.map(opt => (
    <option key={opt.value} value={opt.value}>{opt.label}</option>
  ))}
</select>

Hook: useBuildSortOptions

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

Purpose

Generates a memoized array of sorting options for form select inputs, with localized labels for sorting criteria such as "data" and "relevance".

Returns

Implementation Details

Usage Example

const sortOptions = useBuildSortOptions();

<select>
  {sortOptions.map(opt => (
    <option key={opt.value} value={opt.value}>{opt.label}</option>
  ))}
</select>

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram

flowchart TD
    A[useBuildFormSelectOptions] -->|uses| B[useGraphStore.nodes]
    A -->|uses| C[Operator, RestrictedUpstreamMap]
    D[useHandleFormSelectChange] -->|uses| E[useGraphStore.addEdge]
    D -->|uses| F[useGraphStore.deleteEdgeBySourceAndSourceHandle]
    G[useBuildSortOptions] -->|uses| H[useTranslate('flow')]
    subgraph GraphStore
        B
        E
        F
    end
    subgraph Constants
        C
    end
    subgraph Hooks
        A
        D
        G
    end

Summary

The form-hooks.ts file encapsulates essential logic for managing dynamic form options and selections in a graph editing UI. By abstracting the complexity of filtering nodes, managing graph edges, and providing localized options, it supports clean and maintainable React component implementations that interact with a graph data model. The hooks ensure that user inputs remain consistent with the underlying graph constraints and provide a seamless user experience.