mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-09 15:07:55 -05:00
Better file structure for workflow file
This commit is contained in:
@@ -23,15 +23,21 @@ import ReactFlow, {
|
|||||||
import 'reactflow/dist/style.css'
|
import 'reactflow/dist/style.css'
|
||||||
import { getBlock } from '../components/blocks/configs'
|
import { getBlock } from '../components/blocks/configs'
|
||||||
import { WorkflowBlock } from '../components/blocks/components/workflow-block/workflow-block'
|
import { WorkflowBlock } from '../components/blocks/components/workflow-block/workflow-block'
|
||||||
|
import { BlockConfig } from '../components/blocks/types/block'
|
||||||
|
import { BlockType } from '../components/blocks/types/block'
|
||||||
|
|
||||||
// Types
|
/**
|
||||||
|
* Represents the data structure for a workflow node
|
||||||
|
*/
|
||||||
interface WorkflowNodeData {
|
interface WorkflowNodeData {
|
||||||
type: string
|
type: BlockType // Updated to use the proper type from block.ts
|
||||||
config: any // Consider adding a proper type for config
|
config: BlockConfig // Updated to use the proper type
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Node Components
|
/**
|
||||||
|
* Custom node component for rendering workflow blocks
|
||||||
|
*/
|
||||||
const WorkflowNode = ({
|
const WorkflowNode = ({
|
||||||
data,
|
data,
|
||||||
id,
|
id,
|
||||||
@@ -47,11 +53,9 @@ const WorkflowNode = ({
|
|||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
||||||
const nodeTypes: NodeTypes = {
|
/**
|
||||||
workflowBlock: WorkflowNode,
|
* Custom edge component with animated dashed line styling
|
||||||
}
|
*/
|
||||||
|
|
||||||
// Add this custom edge component
|
|
||||||
const CustomEdge = (props: EdgeProps) => {
|
const CustomEdge = (props: EdgeProps) => {
|
||||||
const [edgePath] = getSmoothStepPath({
|
const [edgePath] = getSmoothStepPath({
|
||||||
sourceX: props.sourceX,
|
sourceX: props.sourceX,
|
||||||
@@ -75,34 +79,43 @@ const CustomEdge = (props: EdgeProps) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const edgeTypes: EdgeTypes = {
|
// Component type definitions
|
||||||
custom: CustomEdge,
|
const nodeTypes: NodeTypes = { workflowBlock: WorkflowNode }
|
||||||
}
|
const edgeTypes: EdgeTypes = { custom: CustomEdge }
|
||||||
|
|
||||||
// Main Canvas Component
|
/**
|
||||||
|
* Main canvas component for the workflow editor
|
||||||
|
*/
|
||||||
function WorkflowCanvas() {
|
function WorkflowCanvas() {
|
||||||
// State
|
// Flow state management
|
||||||
const [nodes, setNodes, onNodesChange] = useNodesState([])
|
const [nodes, setNodes, onNodesChange] = useNodesState([])
|
||||||
const [edges, setEdges, onEdgesChange] = useEdgesState([])
|
const [edges, setEdges, onEdgesChange] = useEdgesState([])
|
||||||
const { project } = useReactFlow()
|
const { project } = useReactFlow()
|
||||||
|
|
||||||
// Handlers
|
/**
|
||||||
|
* Handles new edge connections between nodes
|
||||||
|
*/
|
||||||
const onConnect = useCallback(
|
const onConnect = useCallback(
|
||||||
(connection: Connection) => setEdges((eds) => addEdge(connection, eds)),
|
(connection: Connection) => setEdges((eds) => addEdge(connection, eds)),
|
||||||
[setEdges]
|
[setEdges]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles dropping new blocks onto the canvas
|
||||||
|
*/
|
||||||
const onDrop = useCallback(
|
const onDrop = useCallback(
|
||||||
(event: React.DragEvent) => {
|
(event: React.DragEvent) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Calculate drop position
|
||||||
const reactFlowBounds = event.currentTarget.getBoundingClientRect()
|
const reactFlowBounds = event.currentTarget.getBoundingClientRect()
|
||||||
const position = project({
|
const position = project({
|
||||||
x: event.clientX - reactFlowBounds.left,
|
x: event.clientX - reactFlowBounds.left,
|
||||||
y: event.clientY - reactFlowBounds.top,
|
y: event.clientY - reactFlowBounds.top,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Get block configuration
|
||||||
const { type } = JSON.parse(
|
const { type } = JSON.parse(
|
||||||
event.dataTransfer.getData('application/json')
|
event.dataTransfer.getData('application/json')
|
||||||
)
|
)
|
||||||
@@ -113,6 +126,7 @@ function WorkflowCanvas() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create new node
|
||||||
const newNode = {
|
const newNode = {
|
||||||
id: crypto.randomUUID(),
|
id: crypto.randomUUID(),
|
||||||
type: 'workflowBlock',
|
type: 'workflowBlock',
|
||||||
@@ -134,20 +148,17 @@ function WorkflowCanvas() {
|
|||||||
[project, nodes, setNodes]
|
[project, nodes, setNodes]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Keyframe animation styles
|
||||||
|
const keyframeStyles = `
|
||||||
|
@keyframes dashdraw {
|
||||||
|
from { stroke-dashoffset: 10; }
|
||||||
|
to { stroke-dashoffset: -10; }
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full h-[calc(100vh-56px)]">
|
<div className="w-full h-[calc(100vh-56px)]">
|
||||||
<style>
|
<style>{keyframeStyles}</style>
|
||||||
{`
|
|
||||||
@keyframes dashdraw {
|
|
||||||
from {
|
|
||||||
stroke-dashoffset: 10;
|
|
||||||
}
|
|
||||||
to {
|
|
||||||
stroke-dashoffset: -10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`}
|
|
||||||
</style>
|
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
nodes={nodes}
|
nodes={nodes}
|
||||||
edges={edges}
|
edges={edges}
|
||||||
@@ -158,11 +169,9 @@ function WorkflowCanvas() {
|
|||||||
onDrop={onDrop}
|
onDrop={onDrop}
|
||||||
onDragOver={(e) => e.preventDefault()}
|
onDragOver={(e) => e.preventDefault()}
|
||||||
fitView
|
fitView
|
||||||
maxZoom={1.1}
|
maxZoom={1}
|
||||||
panOnScroll
|
panOnScroll
|
||||||
defaultEdgeOptions={{
|
defaultEdgeOptions={{ type: 'custom' }}
|
||||||
type: 'custom',
|
|
||||||
}}
|
|
||||||
edgeTypes={edgeTypes}
|
edgeTypes={edgeTypes}
|
||||||
connectionLineStyle={{
|
connectionLineStyle={{
|
||||||
stroke: '#94a3b8',
|
stroke: '#94a3b8',
|
||||||
@@ -179,6 +188,9 @@ function WorkflowCanvas() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Root workflow component wrapped with ReactFlow provider
|
||||||
|
*/
|
||||||
export default function Workflow() {
|
export default function Workflow() {
|
||||||
return (
|
return (
|
||||||
<ReactFlowProvider>
|
<ReactFlowProvider>
|
||||||
|
|||||||
Reference in New Issue
Block a user