switch-node.tsx


Overview

The switch-node.tsx file defines a React functional component SwitchNode that represents a "Switch" node in a graphical flow or workflow editor, likely part of a larger low-code/no-code or workflow automation system. This node allows branching logic based on multiple conditions, visually displaying them in a structured format with handles for connecting to other nodes. It renders a toolbar, a header, multiple conditional blocks with their logical operators, and connection handles to facilitate linking in the flow graph.

The component is built on top of the @xyflow/react node framework and integrates UI components such as cards, handles, and toolbars. It relies on custom hooks and constants for positioning handles and retrieving variable labels.


Detailed Explanation

Types and Interfaces


Utility Function

getConditionKey(idx: number, length: number): string


React Components

ConditionBlock

interface ConditionBlockProps {
  condition: ISwitchCondition;
  nodeId: string;
}

InnerSwitchNode

function InnerSwitchNode(props: NodeProps<ISwitchNode>): JSX.Element

Exported Component

export const SwitchNode = memo(InnerSwitchNode);

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

componentDiagram
    component SwitchNode {
        +id: string
        +data: ISwitchNode
        +selected: boolean
    }
    component InnerSwitchNode {
        +renders: ToolBar
        +renders: NodeWrapper
        +renders: NodeHeader
        +renders: ConditionBlock[] (multiple)
        +renders: CommonHandle (target, left)
        +renders: CommonHandle[] (source, right)
    }
    component ConditionBlock {
        +props: condition: ISwitchCondition, nodeId: string
        +renders: Card containing condition items
        +uses: LogicalOperatorIcon
        +uses: useGetVariableLabelByValue
    }
    component ToolBar
    component NodeWrapper
    component NodeHeader
    component CommonHandle
    component LogicalOperatorIcon
    component useBuildSwitchHandlePositions
    component useGetVariableLabelByValue

    SwitchNode <|-- InnerSwitchNode
    InnerSwitchNode --> ToolBar
    InnerSwitchNode --> NodeWrapper
    InnerSwitchNode --> NodeHeader
    InnerSwitchNode --> ConditionBlock
    InnerSwitchNode --> CommonHandle
    ConditionBlock --> LogicalOperatorIcon
    ConditionBlock --> useGetVariableLabelByValue
    InnerSwitchNode --> useBuildSwitchHandlePositions

Summary

The switch-node.tsx file implements a highly interactive, visually rich "Switch" node component for a flow editor, enabling users to define branching logic with multiple conditional paths. It modularizes rendering into reusable subcomponents, leverages hooks for dynamic positioning and label resolution, and integrates tightly with the node framework and UI libraries of the application. The component ensures clear visualization of conditions and logical operators, while providing connection points for building complex workflows.


Example Usage in JSX

import { SwitchNode } from './switch-node';

const switchNodeData: ISwitchNode = {
  name: 'MySwitch',
  label: 'Switch Node',
  conditions: [
    {
      items: [
        { cpn_id: 'var1', operator: 'eq', value: '10' },
        { cpn_id: 'var2', operator: 'gt', value: '5' },
      ],
      logical_operator: 'and',
    },
    {
      items: [
        { cpn_id: 'var3', operator: 'lt', value: '20' },
      ],
      logical_operator: 'or',
    },
  ],
};

<SwitchNode id="node-123" data={switchNodeData} selected={true} />

This will render a switch node with two conditional branches, labeled "If" and "Else", each showing their respective condition items with operator icons and connection handles.


End of Documentation for switch-node.tsx