-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): Implement drag and drop logic
- Loading branch information
Showing
4 changed files
with
347 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from "react" | ||
import { Handle, NodeProps, Position } from "reactflow" | ||
|
||
import { | ||
ChevronsDownIcon, | ||
CircleIcon, | ||
GanttChartIcon, | ||
PlayIcon, | ||
TestTubeIcon, | ||
} from "lucide-react" | ||
import { Button } from "@/components/ui/button" | ||
import { | ||
Card, | ||
CardContent, | ||
CardDescription, | ||
CardHeader, | ||
CardTitle, | ||
} from "@/components/ui/card" | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "@/components/ui/dropdown-menu" | ||
|
||
export interface ActionNodeData { | ||
title: string | ||
name: string | ||
status: "online" | "error" | "offline" | ||
numberOfEvents: number | ||
// Generic metadata | ||
} | ||
|
||
const handleStyle = { width: 8, height: 8 } | ||
|
||
export default React.memo(function ActionNode({ | ||
data: { title, name, status, numberOfEvents }, | ||
}: NodeProps<ActionNodeData>) { | ||
return ( | ||
<Card> | ||
<CardHeader className="grid grid-cols-[1fr_110px] items-start gap-4 space-y-0"> | ||
<div className="space-y-1"> | ||
<CardTitle>{title}</CardTitle> | ||
<CardDescription>{name}</CardDescription> | ||
</div> | ||
<div className="flex items-center space-x-1 rounded-md bg-secondary text-secondary-foreground"> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="secondary" className="px-2 shadow-none"> | ||
<ChevronsDownIcon className="h-4 w-4 text-secondary-foreground" /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent | ||
align="end" | ||
alignOffset={-5} | ||
className="w-[200px]" | ||
forceMount | ||
> | ||
<DropdownMenuItem> | ||
<PlayIcon className="mr-2 h-4 w-4" /> Run | ||
</DropdownMenuItem> | ||
<DropdownMenuItem> | ||
<TestTubeIcon className="mr-2 h-4 w-4" /> Test action | ||
</DropdownMenuItem> | ||
<DropdownMenuItem> | ||
<GanttChartIcon className="mr-2 h-4 w-4" /> View events | ||
</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
</CardHeader> | ||
<CardContent> | ||
<div className="flex space-x-4 text-sm text-muted-foreground"> | ||
<div className="flex items-center"> | ||
<CircleIcon className="mr-1 h-3 w-3 fill-sky-400 text-sky-400" /> {status} | ||
</div> | ||
<div className="flex items-center"> | ||
<GanttChartIcon className="mr-1 h-3 w-3" /> {numberOfEvents} events | ||
</div> | ||
</div> | ||
</CardContent> | ||
|
||
<Handle | ||
type="target" | ||
position={Position.Top} | ||
className="w-16 !bg-gray-500" | ||
style={handleStyle} | ||
/> | ||
<Handle | ||
type="source" | ||
position={Position.Bottom} | ||
className="w-16 !bg-gray-500" | ||
style={handleStyle} | ||
/> | ||
</Card> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import React, { useCallback, useRef, useState } from "react" | ||
import ReactFlow, { | ||
Background, | ||
Connection, | ||
Controls, | ||
Edge, | ||
MarkerType, | ||
Node, | ||
OnConnect, | ||
ReactFlowInstance, | ||
ReactFlowProvider, | ||
addEdge, | ||
useEdgesState, | ||
useNodesState, | ||
useReactFlow, | ||
} from "reactflow" | ||
|
||
import "reactflow/dist/style.css" | ||
import { useToast } from "@/components/ui/use-toast" | ||
import ActionNode, { ActionNodeData } from "@/components/action-node" | ||
|
||
let id = 0 | ||
const getActionNodeId = (): string => `node_${id++}` | ||
|
||
const nodeTypes = { | ||
action: ActionNode, | ||
} | ||
|
||
const defaultEdgeOptions = { | ||
markerEnd: { | ||
type: MarkerType.ArrowClosed, | ||
}, | ||
style: { strokeWidth: 3 }, | ||
} | ||
|
||
const Workflow: React.FC = () => { | ||
const reactFlowWrapper = useRef<HTMLDivElement>(null) | ||
const [nodes, setNodes, onNodesChange] = useNodesState([]) | ||
const [edges, setEdges, onEdgesChange] = useEdgesState([]) | ||
const [reactFlowInstance, setReactFlowInstance] = | ||
useState<ReactFlowInstance | null>(null) | ||
const { toast } = useToast() | ||
|
||
const onConnect = useCallback( | ||
(params: Edge | Connection) => { | ||
setEdges((eds) => addEdge(params, eds)) | ||
}, | ||
[toast, edges, setEdges] | ||
) | ||
|
||
const onDragOver = useCallback((event: React.DragEvent) => { | ||
event.preventDefault() | ||
event.dataTransfer.dropEffect = "move" | ||
}, []) | ||
|
||
const onDrop = (event: React.DragEvent) => { | ||
event.preventDefault() | ||
|
||
// Limit total number of nodes | ||
if (nodes.length >= 50) { | ||
toast({ | ||
title: "Invalid action", | ||
description: "Maximum 50 nodes allowed.", | ||
}) | ||
return | ||
} | ||
|
||
const reactFlowNodeType = event.dataTransfer.getData("application/reactflow"); | ||
const actionNodeData = JSON.parse( | ||
event.dataTransfer.getData("application/json") | ||
) as ActionNodeData | ||
|
||
if (!actionNodeData || !reactFlowNodeType || !reactFlowInstance) return | ||
|
||
const reactFlowNodePosition = reactFlowInstance.screenToFlowPosition({ | ||
x: event.clientX, | ||
y: event.clientY, | ||
}) | ||
|
||
const newNode = { | ||
id: getActionNodeId(), | ||
type: reactFlowNodeType, | ||
position: reactFlowNodePosition, | ||
data: actionNodeData, | ||
} as Node<ActionNodeData> | ||
|
||
setNodes((nds) => nds.concat(newNode)) | ||
} | ||
|
||
return ( | ||
<div ref={reactFlowWrapper} style={{ height: "100%" }}> | ||
<ReactFlow | ||
nodes={nodes} | ||
edges={edges} | ||
onNodesChange={onNodesChange} | ||
onEdgesChange={onEdgesChange} | ||
defaultEdgeOptions={defaultEdgeOptions} | ||
onConnect={onConnect as OnConnect} | ||
onInit={setReactFlowInstance} | ||
onDrop={onDrop} | ||
onDragOver={onDragOver} | ||
nodeTypes={nodeTypes} | ||
fitViewOptions={{ maxZoom: 1 }} | ||
proOptions={{ hideAttribution: true }} | ||
> | ||
<Background /> | ||
<Controls /> | ||
</ReactFlow> | ||
</div> | ||
) | ||
} | ||
|
||
const WorkflowBuilder = ReactFlowProvider | ||
const useWorkflowBuilder = useReactFlow | ||
|
||
export { WorkflowBuilder, useWorkflowBuilder } | ||
export default Workflow |
Oops, something went wrong.