form-hooks.ts


Overview

The form-hooks.ts file provides custom React hooks designed to build option lists for form elements in a flow or graph-based UI. These hooks assist in constructing dropdown options for selecting nodes and sorting criteria, ensuring that invalid or restricted choices are automatically excluded based on business logic and graph state.

Primarily, the file exports two hooks:

These hooks rely on internal graph state management and localization utilities, making them integral to dynamic form controls within a graph-driven application.


Detailed Documentation

Imports


Exported Hooks

useBuildFormSelectOptions

useBuildFormSelectOptions(operatorName: Operator, selfId?: string) => (toList: string[]) => { label: string; value: string }[]

Purpose

Generates a memoized function that builds an array of selectable options representing graph nodes, excluding those that are restricted for the given operatorName, the current node itself (selfId), and any nodes already selected in other "to" fields (toList).

Parameters

Returns

Behavior and Implementation Details

Usage Example

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

const selectedIds = ['node1', 'node3'];

const options = buildOptions(selectedIds);

// options can be passed to a <Select> component
<Select options={options} />

useBuildSortOptions

useBuildSortOptions() => Array<{ value: string; label: string }>

Purpose

Creates a memoized array of sorting options with internationalized labels, typically used for sorting data in UI dropdowns.

Returns

Behavior and Implementation Details

Usage Example

const sortOptions = useBuildSortOptions();

<Select options={sortOptions} />

Important Implementation Details


Interaction with Other System Components


Visual Diagram

flowchart TD
    A[useBuildFormSelectOptions] --> B[useGraphStore.nodes]
    A --> C[Operator & RestrictedUpstreamMap]
    A --> D[Callback function: buildCategorizeToOptions(toList)]
    D --> E[Filters nodes excluding restricted, self, and selected in toList]
    D --> F[Returns list of { label, value } options]

    G[useBuildSortOptions] --> H[useTranslate('flow')]
    G --> I[useMemo generates options for 'data' and 'relevance']
    I --> J[Returns list of { value, label }]

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style G fill:#ccf,stroke:#333,stroke-width:1px

Summary

The form-hooks.ts file is a utility module that provides reusable hooks to build and filter options for forms within a graph-based application. It encapsulates complex filtering logic and localization concerns, enabling consistent and efficient creation of dropdown options that respect graph constraints and UI localization requirements. This file plays a key role in ensuring forms reflect the current graph state and adhere to operator restrictions, enhancing user experience and data integrity.