rewrite-node.tsx
Overview
rewrite-node.tsx defines a React functional component named RewriteNode which represents a specialized node within a visual flow or graph-based editor (likely part of a workflow or automation UI). This node visually encapsulates rewrite logic or configuration data (IRewriteNode) and integrates with the XYFlow React library for node-based workflows. It supports theming, connection handles for graph edges, and displays metadata such as the node's name, label, and a linked LLM (Large Language Model) identifier.
This file's primary purpose is to render the UI and interaction points of a "rewrite" node, making it visually distinct, selectable, and connectable within a larger flow diagram.
Detailed Explanation
Imports
LLMLabel: Component displaying the label for an LLM identifier.
useTheme: Custom hook for retrieving the current UI theme (dark/light).
IRewriteNode: Interface describing the data shape for the rewrite node.
Handle, NodeProps, Position: Components and types from
@xyflow/reactfor managing node connection points.classNames: Utility for conditional CSS class joining.
get: Lodash utility for safe property access.
memo: React optimization HOC to memoize the component.
LeftHandleStyle, RightHandleStyle: Styling objects for the handles.
styles: CSS module for scoped styles.
NodeHeader: Component rendering the node's header information.
Component: InnerRewriteNode
Declaration
function InnerRewriteNode({
id,
data,
isConnectable = true,
selected,
}: NodeProps<IRewriteNode>)
Props
id: string
Unique identifier for the node instance.data: IRewriteNode
The node's data object, containing at minimum:name(string): Node's display name.label(string): Additional descriptive label.form.llm_id(string | undefined): ID referencing a Large Language Model.
isConnectable?: boolean(defaulttrue)
Whether the node handles can be connected to other nodes.selected?: boolean
Indicates if the node is currently selected in the UI.
Returns
A React element representing the node UI.
Description
Uses
useTheme()to get the current UI theme and conditionally applies dark mode styles.Renders two connection handles (
<Handle />) on the left and right, styled via imported constants and CSS modules:Left handle with id
"c", positioned on the left, source type.Right handle with id
"b", positioned on the right, source type.
Displays a
NodeHeadershowing the node'sid,name, andlabel.Shows the LLM label fetched via safe Lodash
getfromdata.form.llm_idinside anLLMLabelcomponent.Applies CSS classes conditionally for theme and selection status.
Usage Example
import { RewriteNode } from './rewrite-node';
const exampleNodeData = {
name: 'Rewrite Step',
label: 'Rewrite Logic',
form: { llm_id: 'llm-1234' },
};
<RewriteNode
id="node-1"
data={exampleNodeData}
isConnectable={true}
selected={false}
/>;
Exported Component: RewriteNode
This is a memoized version of InnerRewriteNode to prevent unnecessary re-renders when props have not changed.
export const RewriteNode = memo(InnerRewriteNode);
Implementation Details
React.memo is used to optimize rendering performance of the node component in complex flow graphs.
@xyflow/react Handle components define connection points for graph edges, critical for node-based editors.
Class name management uses
classnamesto combine CSS module classes with conditional logic for theme and selection.Safe data access via Lodash
getprotects against undefined nested properties (data.form.llm_id).The component is purely presentational, without internal state or side effects.
System Interaction
This node component is designed to be used within a flow editor powered by
@xyflow/react.It interacts with:
The theme provider (
useTheme) to adapt styling.The LLM selection UI (
LLMLabel) to display linked model info.Parent flow management components that handle node connections, selection, and graph layout.
CSS modules isolate styling specific to this node type.
NodeHeaderlikely standardizes the header appearance across various node types.
Visual Diagram
componentDiagram
RewriteNode <|-- InnerRewriteNode
InnerRewriteNode o-- Handle : leftHandle("c")
InnerRewriteNode o-- Handle : rightHandle("b")
InnerRewriteNode o-- NodeHeader
InnerRewriteNode o-- LLMLabel
InnerRewriteNode ..> useTheme
InnerRewriteNode ..> classNames
Handle <|-- @xyflow/react
NodeHeader <|-- ./node-header
LLMLabel <|-- @/components/llm-select/llm-label
Summary
rewrite-node.tsx provides a memoized React component designed for rendering a "rewrite" node within a workflow graph. It supports connection handles on both sides, adapts to dark/light themes, displays key node metadata, and integrates smoothly with the XYFlow node ecosystem. Its main role is UI representation and interaction points for rewrite logic nodes in a larger flow editor application.