hooks.ts

Overview

The hooks.ts file provides custom React hooks designed to work within a graph-based state management context. Specifically, it interacts with a centralized graph store to:

These hooks are meant to be used in UI components dealing with graph nodes and their relationships, enabling reactive and consistent user interfaces for operations such as selecting target nodes and updating connection-related form fields.


Detailed Documentation

Imports


Hook: useBuildRelevantOptions

export const useBuildRelevantOptions = (): ((toList: string[]) => { label: string; value: string }[])

Purpose

Generates a memoized function that, when given a list of node IDs (toList), returns an array of dropdown options excluding those nodes. This is useful to prevent selecting the same node multiple times in related UI dropdowns.

Parameters

Returns

Usage Example

const buildOptions = useBuildRelevantOptions();

// Assuming selectedNodeIds = ['node1', 'node2']
const options = buildOptions(selectedNodeIds);
// options will contain nodes excluding 'node1' and 'node2', formatted as {label, value}

Implementation Details


Hook: useWatchConnectionChanges

export const useWatchConnectionChanges = ({ nodeId, form }: IOperatorForm): void

Purpose

Monitors changes in the connections of a specific graph node and updates the corresponding "yes" and "no" fields in the provided form accordingly. This keeps the form state synchronized with the graph store's node state.

Parameters

Returns

Usage Example

useWatchConnectionChanges({ nodeId: 'node3', form: operatorFormInstance });

This will keep the form fields yes and no in sync with the yes and no properties of the node with ID 'node3'.

Implementation Details


Important Implementation Details


Interaction With Other Parts of the System


Mermaid Diagram

flowchart TD
    A[useBuildRelevantOptions] -->|uses| B[useGraphStore.nodes]
    A --> C[buildRelevantOptions(toList)]
    C -->|filters out| D[toList node IDs]
    C -->|maps to| E[{ label, value } options]

    F[useWatchConnectionChanges({ nodeId, form })] -->|uses| G[useGraphStore.getNode(nodeId)]
    F --> H[watchFormChanges()]
    H -->|updates| I[form.setFieldsValue({ yes, no })]
    F -->|runs| J[useEffect -> watchFormChanges]

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style F fill:#bbf,stroke:#333,stroke-width:1px

Summary

The hooks.ts file provides essential hooks for managing graph node selections and connection synchronization within forms. It bridges the graph store's state with UI elements, enabling consistent, reactive user experiences in graph-related forms and dropdowns. The file's hooks encapsulate common logic for filtering options and syncing form values, promoting code reuse and maintainability.