toolbar.tsx
Overview
The toolbar.tsx file defines a React functional component named ToolBar that provides an interactive toolbar interface for nodes within a graphical flow or editor. It offers common node actions such as running, duplicating, and deleting nodes, presented as icon buttons within a tooltip overlay.
This component is designed to be used in a node-based graph editor UI, integrating closely with the application’s graph state management and custom hooks for duplicating and deleting nodes. The toolbar's UI is rendered as a tooltip that appears when interacting with a node, enabling quick access to node-specific operations.
Exports
ToolBar Component
Description
ToolBar is the main exported React component from this file. It renders a set of icon buttons inside a tooltip associated with a node. It supports:
Running the node (optional, controlled by
showRunprop)Duplicating the node
Deleting the node
It uses internal hooks and global state store functions to perform these actions.
Props
Prop | Type | Description | Default |
|---|---|---|---|
| [boolean \ | undefined](/projects/311/74002) | Indicates if the node is currently selected, affecting tooltip styling |
| The label/type of the node, used to differentiate deletion logic | ||
| Unique identifier of the node to perform actions on | ||
|
| Whether to show the run/play icon button |
|
| The node's visual element triggering the tooltip | required |
Usage Example
<ToolBar selected={true} label="Iteration" id="node-123" showRun={false}>
<NodeVisualElement />
</ToolBar>
Behavior
Delete Node: When the trash icon is clicked, the component checks if the node’s label equals the constant
Operator.Iteration. If so, it callsdeleteIterationNodeById(id); otherwise, it callsdeleteNodeById(id).Duplicate Node: Clicking the copy icon triggers the
duplicateNode(id, label)hook.Run Node: If
showRunis true, a play icon is shown, but no click handler is attached (likely a placeholder or visual indicator).The toolbar is wrapped inside a
TooltipNodecomponent which manages showing the tooltip on hover or focus, with the toolbar buttons rendered inside aTooltipContentpositioned above the trigger element.
Internal Components and Functions
IconWrapper Component
A simple presentational wrapper component that styles the icon buttons consistently.
Props
Inherits all standard
HTMLAttributes<HTMLDivElement>.Accepts
childrenas the icon element to render.
Usage
Wraps icons (Play, Copy, Trash2) to provide padding, background color, rounded corners, and cursor styling.
<IconWrapper onClick={handleDuplicate}>
<Copy className="size-3.5" />
</IconWrapper>
Imports and Dependencies
UI Components:
TooltipNode,TooltipTrigger,TooltipContentfrom@/components/xyflow/tooltip-nodefor tooltip UI.Icon components (
Copy,Play,Trash2) fromlucide-react.
React:
Hooks like
useCallbackfor memoizing event handlers.Type definitions for props and events.
Application-Specific:
Operatorconstant used to differentiate node types (e.g., iteration nodes).useDuplicateNodecustom hook to perform node duplication logic.useGraphStoreglobal store hook for deleting nodes from graph state.
Implementation Details
Event Handling:
Event handlers for duplicate and delete actions useuseCallbackto prevent unnecessary re-renders. They also calle.stopPropagation()to prevent the click events from bubbling up to parent components, which is important in complex nested UI structures like graph editors.Conditional Deletion Logic:
The delete handler distinguishes between regular nodes and iteration nodes based on thelabelprop, calling different store functions accordingly. This reflects different deletion side effects or state updates needed per node type.Tooltip Positioning:
The toolbar buttons appear in a tooltip positioned above the node (Position.Top), improving UI clarity and minimizing space usage.
Interaction with Other Parts of the System
Graph State Management:
Interacts with the global graph state viauseGraphStoreto remove nodes. This store likely manages the entire graph’s data structure and UI state.Node Duplication Logic:
UsesuseDuplicateNodehook, which encapsulates the duplication algorithm and state updates required to clone a node.UI Components:
Relies on theTooltipNodefamily of components to manage tooltip visibility and positioning, which are presumably shared UI utilities across the node editor.Constants and Types:
Uses theOperatorconstant to apply special logic for iteration nodes, indicating integration with a larger domain model defining node types and behaviors.
Visual Diagram: Component Structure and Interaction
componentDiagram
direction LR
ToolBar -- uses --> TooltipNode
TooltipNode -- contains --> TooltipTrigger
TooltipNode -- contains --> TooltipContent
TooltipContent -- contains --> IconWrapper
IconWrapper -- wraps --> Play
IconWrapper -- wraps --> Copy
IconWrapper -- wraps --> Trash2
ToolBar -- uses --> useGraphStore
ToolBar -- uses --> useDuplicateNode
Summary
The toolbar.tsx file provides a focused UI component enabling common node actions in a graph editor context. It combines reusable UI primitives (tooltips and icons), application state management (graph store and duplication hooks), and conditional logic to cater to different node types. This modular design promotes maintainability and clear separation of concerns within the node editor system.
If you need further details on the related hooks or store implementations (useDuplicateNode, useGraphStore), or on the tooltip components, those would be found in other files in the project.