Skip to content

Image Pipeline Playground

feat(frontend/canvas): set up React Flow canvas wrapper component

Mohammad Rajaeiecb778ba74e6
diff --git a/frontend/src/features/canvas/PipelineCanvas.tsx b/frontend/src/features/canvas/PipelineCanvas.tsx
new file mode 100644
index 0000000..dc23285
--- /dev/null
+++ b/frontend/src/features/canvas/PipelineCanvas.tsx
@@ -0,0 +1,61 @@
+import { useCallback, useMemo } from 'react'
+import {
+ Background,
+ Controls,
+ MiniMap,
+ ReactFlow,
+ type NodeTypes,
+} from '@xyflow/react'
+import '@xyflow/react/dist/style.css'
+import { Box } from '@mui/material'
+import { useGraphStore } from '../../store/graphStore'
+import { PipelineNodeView } from './PipelineNode'
+import type { NodeMetadata } from '../../types'
+
+export function PipelineCanvas() {
+ const nodes = useGraphStore((s) => s.nodes)
+ const edges = useGraphStore((s) => s.edges)
+ const onNodesChange = useGraphStore((s) => s.onNodesChange)
+ const onEdgesChange = useGraphStore((s) => s.onEdgesChange)
+ const onConnect = useGraphStore((s) => s.onConnect)
+ const selectNode = useGraphStore((s) => s.selectNode)
+ const addNodeFromType = useGraphStore((s) => s.addNodeFromType)
+
+ const nodeTypes: NodeTypes = useMemo(() => ({ pipeline: PipelineNodeView }), [])
+
+ const onDrop = useCallback(
+ (event: React.DragEvent) => {
+ event.preventDefault()
+ const raw = event.dataTransfer.getData('application/image-pipes-node')
+ if (!raw) return
+ const meta = JSON.parse(raw) as NodeMetadata
+ const bounds = (event.target as HTMLElement).closest('.react-flow')?.getBoundingClientRect()
+ const position = {
+ x: event.clientX - (bounds?.left ?? 0) - 80,
+ y: event.clientY - (bounds?.top ?? 0) - 20,
+ }
+ addNodeFromType(meta, position)
+ },
+ [addNodeFromType],
+ )
+
+ return (
+ <Box sx={{ width: '100%', height: '100%' }} onDragOver={(e) => e.preventDefault()} onDrop={onDrop}>
+ <ReactFlow
+ nodes={nodes}
+ edges={edges}
+ onNodesChange={onNodesChange}
+ onEdgesChange={onEdgesChange}
+ onConnect={onConnect}
+ nodeTypes={nodeTypes}
+ onNodeClick={(_, node) => selectNode(node.id)}
+ onPaneClick={() => selectNode(null)}
+ fitView
+ >
+ <MiniMap />
+ <Controls />
+ <Background gap={16} />
+ </ReactFlow>
+ </Box>
+ )
+}
diff --git a/frontend/src/features/canvas/PipelineNode.tsx b/frontend/src/features/canvas/PipelineNode.tsx
new file mode 100644
index 0000000..962ea17
--- /dev/null
+++ b/frontend/src/features/canvas/PipelineNode.tsx
@@ -0,0 +1,30 @@
+import { Handle, Position, type NodeProps } from '@xyflow/react'
+import { Box, Typography } from '@mui/material'
+import type { GraphNodeData } from '../../types'
+
+export function PipelineNodeView({ data, selected }: NodeProps) {
+ const nodeData = data as GraphNodeData
+ return (
+ <Box
+ sx={{
+ minWidth: 160,
+ borderRadius: 1.5,
+ border: '2px solid',
+ borderColor: nodeData.active ? 'secondary.main' : selected ? 'primary.main' : 'divider',
+ bgcolor: 'background.paper',
+ boxShadow: nodeData.active ? 4 : 1,
+ px: 1.5,
+ py: 1,
+ }}
+ >
+ <Handle type="target" position={Position.Left} id="image" style={{ background: '#1b4d3e' }} />
+ <Typography variant="caption" color="text.secondary" sx={{ textTransform: 'uppercase' }}>
+ {nodeData.category}
+ </Typography>
+ <Typography variant="subtitle2" sx={{ fontWeight: 600 }}>
+ {nodeData.label}
+ </Typography>
+ <Handle type="source" position={Position.Right} id="image" style={{ background: '#c45c26' }} />
+ </Box>
+ )
+}