switch-node.tsx


Overview

switch-node.tsx defines a React functional component named SwitchNode that visually represents a "switch" or conditional branching node within a flow or workflow editor. This component is part of a larger visual flow builder application (likely built on top of the @xyflow/react library), allowing users to define conditional logic flows with multiple branches based on specified conditions.

The file contains supporting components and utilities to:

This component is likely used within a node-based editor canvas where users visually construct logic flows.


Detailed Breakdown

Interfaces and Dependencies


Functions and Components

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

Example:

getConditionKey(0, 3) // returns "If"
getConditionKey(1, 3) // returns "ElseIf"
getConditionKey(2, 3) // returns "Else"
getConditionKey(0, 1) // returns "ElseIf" (special case when only one condition exists)

ConditionBlock

A React functional component that renders the detailed conditions for a single switch branch.

const ConditionBlock = ({
  condition,
  nodeId,
}: {
  condition: ISwitchCondition;
  nodeId: string;
}) => { ... }

Usage example:

<ConditionBlock condition={someCondition} nodeId="node-123" />

SwitchNode

The main exported component representing the switch node in the flow editor.

export function SwitchNode({ id, data, selected }: NodeProps<ISwitchNode>) { ... }

Example usage in a node editor:

<SwitchNode
  id="switch-node-1"
  data={{
    name: "Switch A",
    label: "Switch Condition",
    conditions: [...],
  }}
  selected={true}
/>

Important Implementation Details


Interaction with Other System Parts


Visual Diagram

classDiagram
    class SwitchNode {
        +id: string
        +data: ISwitchNode
        +selected: boolean
        +render()
    }
    class ConditionBlock {
        +condition: ISwitchCondition
        +nodeId: string
        +render()
    }
    SwitchNode --> ConditionBlock : renders multiple
    SwitchNode ..> useBuildSwitchHandlePositions : uses
    SwitchNode ..> useTheme : uses
    ConditionBlock ..> useGetComponentLabelByValue : uses
    SwitchNode ..> NodeHeader : renders
    SwitchNode ..> Handle : renders multiple input/output handles

Summary

The switch-node.tsx file defines a React component for a switch/conditional node in a flow editor. It presents multiple conditional branches with dynamic labels and connection handles, styled with theming and modular CSS. The component is designed to integrate tightly with a node-based flow system, leveraging custom hooks for layout and label resolution. This component enhances the visual clarity and interactivity of complex conditional flows in the editor UI.