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:
Render condition blocks with logical expressions,
Manage multiple conditional branches ("If", "ElseIf", "Else"),
Display connection handles on the node for linking flows,
Apply themes and styles dynamically,
Use custom hooks for positioning and label fetching.
This component is likely used within a node-based editor canvas where users visually construct logic flows.
Detailed Breakdown
Interfaces and Dependencies
ISwitchNodeandISwitchCondition: Interfaces imported from the database flow definition, representing the structure of the switch node and its conditions.NodeProps<ISwitchNode>: Generic prop type from@xyflow/reactrepresenting node data and metadata.Other utilities: The component uses
antdUI components, CSS modules for styling, and custom hooks for theme and label management.
Functions and Components
getConditionKey(idx: number, length: number): string
Purpose: Derives a string label for a condition branch based on its index within all branches.
Parameters:
idx: Index of the current condition branch.length: Total number of condition branches.
Returns:
"If"for the first condition if there is more than one."ElseIf"for intermediate conditions."Else"for the last condition branch.
Usage: Used inside the
SwitchNodecomponent to label branches visually.
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;
}) => { ... }
Props:
condition: ISwitchCondition— The condition object containing one or more logical items.nodeId: string— The id of the parent switch node, used to fetch component labels.
Functionality:
Iterates over
condition.items(logical expressions).For each item, renders the component label, operator, and value.
Between multiple items, inserts a divider showing the logical operator (e.g., AND, OR).
Uses:
useGetComponentLabelByValuehook to resolve component labels by their IDs.antd'sFlexandDividerfor layout and separation.CSS modules for styling.
Returns: A vertically stacked JSX block visually representing the condition logic.
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>) { ... }
Props (from
NodeProps<ISwitchNode>):id: string— Unique node identifier.data: ISwitchNode— Data describing the switch node, including name, label, and conditions.selected: boolean— Whether the node is currently selected in the UI.
Internal Logic:
Uses
useBuildSwitchHandlePositionshook to calculate the positions of each conditional branch's output handle.Uses
useThemeto apply dark or light mode styles.Renders:
A single input Handle on the left side for incoming connections.
A
NodeHeadercomponent showing the node's name and label.A vertical list of conditional branches, each with:
A label (e.g., "If", "ElseIf", or "Else").
The condition block rendered by
ConditionBlock.An output Handle on the right side for outgoing connections, positioned dynamically.
Styling:
Applies conditional CSS classes for theme and selection state.
Returns: A JSX section representing the entire switch node UI.
Example usage in a node editor:
<SwitchNode
id="switch-node-1"
data={{
name: "Switch A",
label: "Switch Condition",
conditions: [...],
}}
selected={true}
/>
Important Implementation Details
Dynamic Handle Positions: The output handles are positioned dynamically according to the number and arrangement of conditions. This is handled by the custom hook
useBuildSwitchHandlePositions, which is not detailed here but is critical to layout.Label Resolution: The component labels used in condition items are dynamically fetched via the
useGetComponentLabelByValuehook based on component IDs, allowing for flexible and user-friendly display names.Condition Logic Display: The
ConditionBlockcomponent visually arranges multiple condition items with their operators and logical connectors, making complex logical expressions readable.Theming: The component adapts to themes (light/dark) via
useTheme, enabling consistent UI across the application.Handles for Connections: The use of
Handlecomponents from@xyflow/reactindicates this node is part of a flowchart or graph where nodes connect via these handles.
Interaction with Other System Parts
Node Editor Canvas: This component is intended to be rendered inside a flow or graph editor that supports node-based editing (
@xyflow/react).Hooks:
useBuildSwitchHandlePositionscalculates handle layout, indicating integration with layout logic.useGetComponentLabelByValueprovides dynamic labeling from application state or database.useThemeintegrates with global theming provider.
Styling and Icons:
Uses CSS modules (
index.less) for scoped styling.Imports icon styles from
handle-icon.ts.
Child Components:
NodeHeaderrenders the node's header, likely reused across node types.
Data Interfaces:
ISwitchNodeandISwitchConditionshape the node data stored and manipulated elsewhere in the flow system.
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.