template-node.tsx
Overview
template-node.tsx defines a React functional component named TemplateNode which represents a custom node within a flow or diagram editor interface. This component is designed for use with the @xyflow/react library, a React-based flowchart or node editor framework.
The component visually renders a node box with two connection handles (left and right) to enable linking with other nodes, a header displaying node information, and a list of parameters associated with the node. It supports theming via a custom theme provider and highlights the node when selected.
This file primarily facilitates the visualization and interaction of a node of type ITemplateNode within a larger flow or graph-based UI.
Detailed Explanation
Imports
React & UI libraries:
useThemefrom a custom theme provider for dark/light mode.Handle,NodeProps, Position from@xyflow/reactfor node connection points.Flexfromantdfor flexible layout.
Utilities:
classNames to conditionally apply CSS classes.
getfromlodashfor safe nested property access.
Hooks & interfaces:
useGetComponentLabelByValuecustom hook to get component labels by ID.IGenerateParameter interface describing node parameters.
ITemplateNodeinterface describing the node data structure.
Styles and components:
Styles imported from a local LESS stylesheet.
NodeHeadercomponent for rendering the node's header.Custom handle styles for left and right connection handles.
TemplateNode Component
export function TemplateNode({
id,
data,
isConnectable = true,
selected,
}: NodeProps<ITemplateNode>) { ... }
Purpose:
Renders a single node in the flow editor with connection handles, header, and parameter list.
Parameters:
id: string
Unique identifier for the node instance in the flow.data: ITemplateNode
The node's data payload, including properties likename,label, andform.parameters.isConnectable?: boolean(default:true)
Flag to enable or disable the node's handles for connecting edges.selected?: boolean
Indicates if the node is currently selected (for styling).
Internal Logic:
Retrieves the
parametersarray fromdata.form.parameterssafely usinglodash.get.Uses
useGetComponentLabelByValuehook with the nodeidto obtain a function that maps component IDs to display labels.Gets current UI theme (
darkor other) fromuseTheme.
JSX Structure:
<section>
Container element with dynamic classes:Base style
logicNode.darktheme modifier.selectedNodeif selected.
Two
<Handle>components (from@xyflow/react):Left handle with id
"c", positioned on the left, styled withLeftHandleStyle.Right handle with id
"b", positioned on the right, styled withRightHandleStyle.
Both handles haveisConnectableprop to enable/disable connections.
<NodeHeader>component:
Displays the node'snameandlabel.<Flex>container (vertical layout) for rendering each parameter:
Iterates overparametersand renders a label and the parameter's corresponding component label fetched viagetLabel(x.component_id).
Return Value:
Returns a JSX element representing the fully rendered node UI.
Usage Example:
<TemplateNode
id="node-123"
data={{
name: "Template 1",
label: "Template Node",
form: {
parameters: [
{ id: "p1", key: "Param A", component_id: "comp-1" },
{ id: "p2", key: "Param B", component_id: "comp-2" }
]
}
}}
isConnectable={true}
selected={false}
/>
Important Implementation Details
Connection Handles:
The node exposes two connection points (Handlecomponents) on the left and right sides. These allow edges (connections) to originate or terminate at this node. The IDs"c"and"b"are used to uniquely identify these handles.Theming:
The component adapts its styles based on the current UI theme (darkor default) retrieved from the theme context.Parameter Rendering:
The parameters displayed are dynamically sourced from the node'sform.parametersarray. Each parameter's display label is resolved through a custom hookuseGetComponentLabelByValue, which likely maps component IDs to human-readable names.Safe Data Access:
The use oflodash.getallows safe access to nested properties (data.form.parameters), avoiding runtime errors if intermediate properties are undefined.Styling:
CSS Modules (index.less) are used for scoped styling, ensuring styles do not leak globally.
Interaction with Other Parts of the System
Flow System (
@xyflow/react):
Integrates with the flow editor system viaHandlecomponents andNodePropstyping, enabling this node to be part of an interactive directed graph.Theme Provider:
Uses a customuseThemehook from the theme provider component in the system to apply consistent theming.Hooks & Interfaces:
Depends onuseGetComponentLabelByValuehook to resolve parameter labels, which suggests the presence of a centralized mapping or store of component metadata.NodeHeader Component:
Composes the UI of this node by embedding a reusableNodeHeadercomponent, promoting modularity.Styles:
Relies on local styles (index.less) and shared handle styles (handle-icon.ts) for consistent visuals.
Visual Diagram
componentDiagram
component TemplateNode {
+id: string
+data: ITemplateNode
+isConnectable?: boolean
+selected?: boolean
+Render()
}
component Handle_Left {
<<Handle>>
+id: "c"
+position: Left
+isConnectable: boolean
}
component Handle_Right {
<<Handle>>
+id: "b"
+position: Right
+isConnectable: boolean
}
component NodeHeader {
+id: string
+name: string
+label: string
}
component useTheme {
+theme: string
}
component useGetComponentLabelByValue {
+getLabel(component_id: string): string
}
TemplateNode --> Handle_Left : renders
TemplateNode --> Handle_Right : renders
TemplateNode --> NodeHeader : renders
TemplateNode ..> useTheme : uses
TemplateNode ..> useGetComponentLabelByValue : uses
Summary
This file defines a React node component for a flow editor, responsible for rendering node UI with connection points, headers, and parameters. It leverages theming, hooks for label resolution, and integrates tightly with the flow framework's node system. The component is modular and styled with CSS Modules, making it reusable and adaptable within the overall application flow editing interface.