form-hooks.ts


Overview

The form-hooks.ts file provides custom React hooks designed to facilitate building select options for forms within a graph-based UI context. Specifically, it offers hooks to:

These hooks are intended to be used in React components that require dynamically generated select options, particularly when dealing with nodes and operators in a graph data structure, ensuring that invalid or restricted choices are excluded.


Detailed API Documentation

1. useBuildFormSelectOptions

Description

useBuildFormSelectOptions is a React hook that returns a memoized function (buildCategorizeToOptions) to generate a filtered list of select options for a dropdown. These options represent graph nodes that:

This hook helps maintain data integrity and user experience by preventing selection of invalid or conflicting nodes in a form.

Signature

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

Parameters

Returns

Usage Example

const operatorName = Operator.Filter;
const selfId = "node-123";

const buildOptions = useBuildFormSelectOptions(operatorName, selfId);

const selectedToList = ["node-456", "node-789"];
const options = buildOptions(selectedToList);

// options now contains nodes that:
// - Are not 'Note' operator nodes
// - Are not restricted upstream of 'Filter'
// - Are not selfId ("node-123")
// - Are not in selectedToList ("node-456", "node-789")

Implementation Details


2. useBuildSortOptions

Description

useBuildSortOptions is a React hook that provides localized sorting options for use in forms or dropdowns. It returns a memoized array of options with values and translated labels.

Signature

useBuildSortOptions(): { value: string; label: string }[]

Parameters

Returns

Usage Example

const sortOptions = useBuildSortOptions();

// sortOptions might be:
// [
//   { value: 'data', label: 'Data' },        // localized label for 'data'
//   { value: 'relevance', label: 'Relevance' } // localized label for 'relevance'
// ]

Implementation Details


Important Implementation Details and Algorithms


Interactions with Other Parts of the System

Together, these interactions enable the dynamic generation of form options that are context-aware and consistent with the graph's rules and localization.


Mermaid Diagram: Flowchart of Main Functions and Their Relationships

flowchart TD
    A[useBuildFormSelectOptions] --> B[useGraphStore.nodes]
    A --> C[Operator & RestrictedUpstreamMap]
    A --> D[buildCategorizeToOptions(toList)]
    D --> E[Filter nodes: exclude Operator.Note, restricted operators, selfId, toList]
    E --> F[Map to { label, value } options]

    G[useBuildSortOptions] --> H[useTranslate('flow')]
    G --> I[useMemo: ['data', 'relevance']]
    I --> J[Map to { value, label } options]

Summary

The form-hooks.ts file provides two essential hooks for generating form select options in a graph-based UI environment:

These hooks leverage global state, constants defining operator rules, and internationalization to deliver robust, reusable form utilities aligned with the system's data model and UX requirements.