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

Returns

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


Interaction with Other Parts of the System


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

This modular and efficient approach helps maintain a clean separation of concerns between data handling, position calculation, and React Flow integration in the application.