Added comments to workflow

This commit is contained in:
Emir Karabeg
2025-01-30 11:23:32 -08:00
parent 9defa59c72
commit c8123903d3
2 changed files with 24 additions and 2 deletions
@@ -17,15 +17,18 @@ import { WorkflowNode } from '../workflow-node/workflow-node'
import { CustomEdge } from '../custom-edge/custom-edge'
import { initializeStateLogger } from '@/stores/workflow/state-logger'
// Define custom node and edge types for ReactFlow
const nodeTypes: NodeTypes = { workflowBlock: WorkflowNode }
const edgeTypes: EdgeTypes = { custom: CustomEdge }
export function WorkflowCanvas() {
// Track selected elements in the workflow
const [selectedBlockId, setSelectedBlockId] = useState<string | null>(null)
const [selectedEdgeId, setSelectedEdgeId] = useState<string | null>(null)
// Store references and state management hooks
const { isExecuting, executionResult, handleRunWorkflow } =
useWorkflowExecution()
const {
blocks,
edges,
@@ -41,7 +44,7 @@ export function WorkflowCanvas() {
const { addNotification } = useNotificationStore()
const { project } = useReactFlow()
// Convert blocks to ReactFlow nodes
// Transform blocks into ReactFlow node format
const nodes = Object.values(blocks).map((block) => ({
id: block.id,
type: 'workflowBlock',
@@ -55,6 +58,7 @@ export function WorkflowCanvas() {
},
}))
// Handle node position updates during drag operations
const onNodesChange = useCallback(
(changes: any) => {
changes.forEach((change: any) => {
@@ -66,6 +70,7 @@ export function WorkflowCanvas() {
[updateBlockPosition]
)
// Handle edge removal and updates
const onEdgesChange = useCallback(
(changes: any) => {
changes.forEach((change: any) => {
@@ -77,6 +82,7 @@ export function WorkflowCanvas() {
[removeEdge]
)
// Create new edges when nodes are connected
const onConnect = useCallback(
(connection: any) => {
addEdge({
@@ -88,6 +94,7 @@ export function WorkflowCanvas() {
[addEdge]
)
// Handle new block creation from toolbar drag and drop
const onDrop = useCallback(
(event: React.DragEvent) => {
event.preventDefault()
@@ -121,21 +128,25 @@ export function WorkflowCanvas() {
[project, blocks, addBlock]
)
// Update selection state when clicking nodes
const onNodeClick = useCallback((event: React.MouseEvent, node: any) => {
event.stopPropagation()
setSelectedBlockId(node.id)
setSelectedEdgeId(null)
}, [])
// Clear selection when clicking on the canvas
const onPaneClick = useCallback(() => {
setSelectedBlockId(null)
setSelectedEdgeId(null)
}, [])
// Update selected edge when clicking on connections
const onEdgeClick = useCallback((event: React.MouseEvent, edge: any) => {
setSelectedEdgeId(edge.id)
}, [])
// Handle keyboard shortcuts for edge deletion
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (
@@ -151,10 +162,12 @@ export function WorkflowCanvas() {
return () => window.removeEventListener('keydown', handleKeyDown)
}, [selectedEdgeId, removeEdge])
// Initialize state logging for debugging
useEffect(() => {
initializeStateLogger()
}, [])
// Add selection data to edges for visual feedback
const edgesWithSelection = edges.map((edge) => ({
...edge,
data: {
+9
View File
@@ -8,11 +8,14 @@ import { useParams, useRouter } from 'next/navigation'
import { WorkflowCanvas } from './components/workflow-canvas/workflow-canvas'
export default function Workflow() {
// Get URL parameters and routing functionality
const params = useParams()
const router = useRouter()
const { workflows, setActiveWorkflow, addWorkflow } = useWorkflowRegistry()
// Track if initial data loading is complete
const [isInitialized, setIsInitialized] = useState(false)
// Load saved workflows from localStorage on component mount
useEffect(() => {
if (typeof window !== 'undefined') {
const savedRegistry = localStorage.getItem('workflow-registry')
@@ -23,9 +26,11 @@ export default function Workflow() {
}
}, [])
// Handle workflow initialization and navigation
useEffect(() => {
if (!isInitialized) return
// Create a new workflow with default values
const createInitialWorkflow = () => {
const id = crypto.randomUUID()
const newWorkflow = {
@@ -39,16 +44,19 @@ export default function Workflow() {
return id
}
// Ensure valid workflow ID and redirect if necessary
const validateAndNavigate = () => {
const workflowIds = Object.keys(workflows)
const currentId = params.id as string
// Create first workflow if none exist
if (workflowIds.length === 0) {
const newId = createInitialWorkflow()
router.replace(`/w/${newId}`)
return
}
// Redirect to first workflow if current ID is invalid
if (!workflows[currentId]) {
router.replace(`/w/${workflowIds[0]}`)
return
@@ -67,6 +75,7 @@ export default function Workflow() {
isInitialized,
])
// Don't render until initial data is loaded
if (!isInitialized) {
return null
}