use-build-categorize-handle-positions.ts
Overview
The use-build-categorize-handle-positions.ts file defines a custom React hook named useBuildCategorizeHandlePositions. This hook is designed to calculate and manage the vertical positions of "handles" or interactive UI elements within a categorized flow node in a React flow diagram or editor. It uses form schema validation, memoization, and React Flow's internal update mechanism to ensure the positions are correctly computed and updated whenever the node's data changes.
This hook is particularly useful in UI components that render categorized items inside flow nodes, where each item needs a dynamically computed vertical position for proper layout or interaction handling.
Detailed Explanation
useBuildCategorizeHandlePositions
Description
A React hook that computes the vertical positions of categorized handles based on the form items data contained in a flow node’s data structure. It also triggers an internal update of the node in the React Flow graph to reflect position changes.
Parameters
params: { id: string; data: RAGFlowNodeType['data'] }id— The unique identifier of the flow node for which handle positions are being computed.data— The data object associated with the flow node, containing form information and items to be categorized.
Returns
{ positions: Array<{ top: number; name: string; uuid: string }> }positions— An array of objects, each representing a categorized item with a calculatedtopposition (in pixels), along with itsnameanduuid.
Usage Example
import { useBuildCategorizeHandlePositions } from './use-build-categorize-handle-positions';
const CategorizeNodeComponent = ({ id, data }) => {
const { positions } = useBuildCategorizeHandlePositions({ id, data });
return (
<div>
{positions.map(({ name, top, uuid }) => (
<Handle
key={uuid}
type="source"
position="right"
style={{ top }}
id={uuid}
title={name}
/>
))}
</div>
);
};
Implementation Details
Schema Validation:
The hook uses a Zod schema returned byuseCreateCategorizeFormSchema()to infer the expected shape of the form data (FormSchemaType). This ensures strong typing and validation of the form items.Data Extraction:
The hook useslodash.getto safely extract the array of form items fromdata.form.items. If the path doesn't exist or is empty, it defaults to an empty array.Position Calculation Algorithm:
The vertical positiontopfor each item is computed based on its index in the list:The first item is placed at 86 pixels from the top.
Each subsequent item is positioned 32 pixels (8 + 24) below the previous item.
This algorithm ensures consistent spacing between items and is easily adjustable by changing the constants.
React Memoization:
Both the extracteditemsand the computedpositionsare memoized usinguseMemoto optimize performance and avoid unnecessary recalculations on re-renders.Node Internals Update:
The hook callsupdateNodeInternals(id)inside auseEffectwheneveridoritemschange. This call triggers React Flow to recalculate the node's internal state and layout, ensuring the UI reflects the updated handle positions.
Interaction with Other Parts of the System
React Flow Integration:
This hook depends on theuseUpdateNodeInternalshook from@xyflow/react(a React Flow related library) to inform React Flow about updates to the node’s handles or layout.Form Schema:
It uses the schema fromuseCreateCategorizeFormSchema(from the local form system) to understand the structure of the categorized items. This tightly couples it to the form validation logic and ensures data consistency.Flow Node Data Interface:
It works with nodes typed asRAGFlowNodeType, defined elsewhere in the system, which presumably contains the flow node data structure including the form data.
Mermaid Flowchart Diagram
flowchart TD
A[useBuildCategorizeHandlePositions Hook]
A --> B[Extract form.items from data]
B --> C[Memoize items array]
C --> D[Compute positions array with top, name, uuid]
D --> E[Return positions]
A --> F[Trigger updateNodeInternals(id) on items or id change]
subgraph React Flow
F
end
subgraph Form Schema
B
end
Summary
The hook abstracts the logic for building handle positions for categorized flow nodes.
It leverages form schema validation for robust data typing.
It calculates vertical positions with a simple linear spacing algorithm.
It integrates with React Flow to update the node’s internals after position recalculation.
It is intended to be used in React components rendering categorized flow nodes to position interactive handles correctly.
This modular and efficient approach helps maintain a clean separation of concerns between data handling, position calculation, and React Flow integration in the application.