hooks.ts


Overview

The hooks.ts file provides two custom React hooks—useBuildCategorizeHandlePositions and useBuildSwitchHandlePositions—that are designed to calculate and manage the positional data of handles (connection points) for nodes in a flow diagram system. These hooks are part of a React-based UI framework, likely used within a node-based editor or flowchart tool, where nodes have dynamic handles positioned vertically based on their data.

Each hook:

The hooks use memoization (useMemo) to optimize performance, recalculating positions only when relevant data changes, and use effects (useEffect) to trigger node updates.


Detailed Explanation

Imports and Dependencies


Hook: useBuildCategorizeHandlePositions

useBuildCategorizeHandlePositions({
  data,
  id,
}: {
  id: string;
  data: RAGFlowNodeType['data'];
}) => { ... }

Purpose

Computes the vertical positions of handles for categorization-type nodes, where each handle corresponds to a category in the node's data.

Parameters

Returns

Behavior

Usage Example

const { positions } = useBuildCategorizeHandlePositions({ id: 'node-1', data: nodeData });
positions.forEach(pos => {
  console.log(`Handle: ${pos.text} at top: ${pos.top}px`);
});

Hook: useBuildSwitchHandlePositions

useBuildSwitchHandlePositions({
  data,
  id,
}: {
  id: string;
  data: RAGFlowNodeType['data'];
}) => { ... }

Purpose

Calculates vertical positions of handles for switch-type nodes, where each handle corresponds to a switch condition or the "Else" case.

Parameters

Returns

Behavior

Usage Example

const { positions } = useBuildSwitchHandlePositions({ id: 'node-2', data: nodeData });
positions.forEach(pos => {
  console.log(`Handle: ${pos.text} at top: ${pos.top}px`);
});

Important Implementation Details


Interaction with Other System Components


File Structure Diagram

flowchart TD
    A[useBuildCategorizeHandlePositions Hook]
    B[useBuildSwitchHandlePositions Hook]
    C[useUpdateNodeInternals]
    D[get (lodash)]
    E[generateSwitchHandleText]
    F[SwitchElseTo constant]

    A --> C
    B --> C
    A --> D
    B --> D
    B --> E
    B --> F

Diagram Explanation:


Summary

The hooks.ts file is a utility module containing two key React hooks for calculating and managing the vertical positions of connection handles on flowchart nodes representing categorized and switch-type data. This enables dynamic, data-driven rendering of node connection points with proper spacing and labeling, and integrates seamlessly with the flow rendering system via useUpdateNodeInternals. The code uses efficient memoization and effects to optimize rendering updates.