From f3c78fe73ddad9e5395f4c50476678684c726d09 Mon Sep 17 00:00:00 2001 From: StyleZhang Date: Tue, 6 Feb 2024 17:17:29 +0800 Subject: [PATCH] init --- web/app/(commonLayout)/workflow/page.tsx | 53 ++++++++++++++++- web/app/components/workflow/hooks.ts | 4 +- web/app/components/workflow/index.tsx | 76 +++++++----------------- 3 files changed, 77 insertions(+), 56 deletions(-) diff --git a/web/app/(commonLayout)/workflow/page.tsx b/web/app/(commonLayout)/workflow/page.tsx index 218c25fc12..e49afc1743 100644 --- a/web/app/(commonLayout)/workflow/page.tsx +++ b/web/app/(commonLayout)/workflow/page.tsx @@ -4,10 +4,61 @@ import type { FC } from 'react' import { memo } from 'react' import Workflow from '@/app/components/workflow' +const initialNodes = [ + { + id: '1', + type: 'custom', + position: { x: 330, y: 30 }, + data: { type: 'start' }, + }, + { + id: '2', + type: 'custom', + position: { x: 330, y: 212 }, + data: { type: 'start' }, + }, + { + id: '3', + type: 'custom', + position: { x: 150, y: 394 }, + data: { type: 'start' }, + }, + { + id: '4', + type: 'custom', + position: { x: 510, y: 394 }, + data: { type: 'start' }, + }, +] + +const initialEdges = [ + { + id: '1', + source: '1', + target: '2', + type: 'custom', + }, + { + id: '2', + source: '2', + target: '3', + type: 'custom', + }, + { + id: '3', + source: '2', + target: '4', + type: 'custom', + }, +] + const Page: FC = () => { return (
- +
) } diff --git a/web/app/components/workflow/hooks.ts b/web/app/components/workflow/hooks.ts index 5b2a191402..631d169f52 100644 --- a/web/app/components/workflow/hooks.ts +++ b/web/app/components/workflow/hooks.ts @@ -5,8 +5,8 @@ import { } from 'react' import type { Node } from './types' -export const useWorkflow = (nodes: Node[]) => { - const [selectedNodeId, setSelectedNodeId] = useState('') +export const useWorkflow = (nodes: Node[], initialSelectedNodeId?: string) => { + const [selectedNodeId, setSelectedNodeId] = useState(initialSelectedNodeId) const handleSelectedNodeIdChange = useCallback((nodeId: string) => setSelectedNodeId(nodeId), []) diff --git a/web/app/components/workflow/index.tsx b/web/app/components/workflow/index.tsx index 7edd0e2781..19ed9b9230 100644 --- a/web/app/components/workflow/index.tsx +++ b/web/app/components/workflow/index.tsx @@ -1,4 +1,5 @@ import type { FC } from 'react' +import type { Edge } from 'reactflow' import ReactFlow, { Background, ReactFlowProvider, @@ -16,6 +17,7 @@ import CustomNode, { Panel, } from './nodes' import CustomEdge from './custom-edge' +import type { Node } from './types' const nodeTypes = { custom: CustomNode, @@ -24,54 +26,6 @@ const edgeTypes = { custom: CustomEdge, } -const initialNodes = [ - { - id: '1', - type: 'custom', - position: { x: 330, y: 30 }, - data: { type: 'start' }, - }, - { - id: '2', - type: 'custom', - position: { x: 330, y: 212 }, - data: { type: 'start' }, - }, - { - id: '3', - type: 'custom', - position: { x: 150, y: 394 }, - data: { type: 'start' }, - }, - { - id: '4', - type: 'custom', - position: { x: 510, y: 394 }, - data: { type: 'start' }, - }, -] - -const initialEdges = [ - { - id: '1', - source: '1', - target: '2', - type: 'custom', - }, - { - id: '2', - source: '2', - target: '3', - type: 'custom', - }, - { - id: '3', - source: '2', - target: '4', - type: 'custom', - }, -] - const Workflow = () => { const { nodes, @@ -96,15 +50,23 @@ const Workflow = () => { ) } - -const WorkflowWrap: FC = () => { +type WorkflowWrapProps = { + selectedNodeId?: string + nodes: Node[] + edges: Edge[] +} +const WorkflowWrap: FC = ({ + nodes: initialNodes, + edges: initialEdges, + selectedNodeId: initialSelectedNodeId, +}) => { const [nodes] = useNodesState(initialNodes) const [edges] = useEdgesState(initialEdges) const { selectedNodeId, handleSelectedNodeIdChange, selectedNode, - } = useWorkflow(nodes) + } = useWorkflow(nodes, initialSelectedNodeId) return ( { ) } -const WorkflowWrapWithReactFlowProvider = () => { +const WorkflowWrapWithReactFlowProvider: FC = ({ + selectedNodeId, + nodes, + edges, +}) => { return ( - + ) }