Commit f3c78fe7 authored by StyleZhang's avatar StyleZhang

init

parent a17c0e5b
...@@ -4,10 +4,61 @@ import type { FC } from 'react' ...@@ -4,10 +4,61 @@ import type { FC } from 'react'
import { memo } from 'react' import { memo } from 'react'
import Workflow from '@/app/components/workflow' 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 = () => { const Page: FC = () => {
return ( return (
<div className='min-w-[720px] w-full h-full overflow-x-auto'> <div className='min-w-[720px] w-full h-full overflow-x-auto'>
<Workflow /> <Workflow
nodes={initialNodes}
edges={initialEdges}
/>
</div> </div>
) )
} }
......
...@@ -5,8 +5,8 @@ import { ...@@ -5,8 +5,8 @@ import {
} from 'react' } from 'react'
import type { Node } from './types' import type { Node } from './types'
export const useWorkflow = (nodes: Node[]) => { export const useWorkflow = (nodes: Node[], initialSelectedNodeId?: string) => {
const [selectedNodeId, setSelectedNodeId] = useState('') const [selectedNodeId, setSelectedNodeId] = useState(initialSelectedNodeId)
const handleSelectedNodeIdChange = useCallback((nodeId: string) => setSelectedNodeId(nodeId), []) const handleSelectedNodeIdChange = useCallback((nodeId: string) => setSelectedNodeId(nodeId), [])
......
import type { FC } from 'react' import type { FC } from 'react'
import type { Edge } from 'reactflow'
import ReactFlow, { import ReactFlow, {
Background, Background,
ReactFlowProvider, ReactFlowProvider,
...@@ -16,6 +17,7 @@ import CustomNode, { ...@@ -16,6 +17,7 @@ import CustomNode, {
Panel, Panel,
} from './nodes' } from './nodes'
import CustomEdge from './custom-edge' import CustomEdge from './custom-edge'
import type { Node } from './types'
const nodeTypes = { const nodeTypes = {
custom: CustomNode, custom: CustomNode,
...@@ -24,54 +26,6 @@ const edgeTypes = { ...@@ -24,54 +26,6 @@ const edgeTypes = {
custom: CustomEdge, 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 Workflow = () => {
const { const {
nodes, nodes,
...@@ -96,15 +50,23 @@ const Workflow = () => { ...@@ -96,15 +50,23 @@ const Workflow = () => {
</div> </div>
) )
} }
type WorkflowWrapProps = {
const WorkflowWrap: FC = () => { selectedNodeId?: string
nodes: Node[]
edges: Edge[]
}
const WorkflowWrap: FC<WorkflowWrapProps> = ({
nodes: initialNodes,
edges: initialEdges,
selectedNodeId: initialSelectedNodeId,
}) => {
const [nodes] = useNodesState(initialNodes) const [nodes] = useNodesState(initialNodes)
const [edges] = useEdgesState(initialEdges) const [edges] = useEdgesState(initialEdges)
const { const {
selectedNodeId, selectedNodeId,
handleSelectedNodeIdChange, handleSelectedNodeIdChange,
selectedNode, selectedNode,
} = useWorkflow(nodes) } = useWorkflow(nodes, initialSelectedNodeId)
return ( return (
<WorkflowContext.Provider value={{ <WorkflowContext.Provider value={{
...@@ -119,10 +81,18 @@ const WorkflowWrap: FC = () => { ...@@ -119,10 +81,18 @@ const WorkflowWrap: FC = () => {
) )
} }
const WorkflowWrapWithReactFlowProvider = () => { const WorkflowWrapWithReactFlowProvider: FC<WorkflowWrapProps> = ({
selectedNodeId,
nodes,
edges,
}) => {
return ( return (
<ReactFlowProvider> <ReactFlowProvider>
<WorkflowWrap /> <WorkflowWrap
selectedNodeId={selectedNodeId}
nodes={nodes}
edges={edges}
/>
</ReactFlowProvider> </ReactFlowProvider>
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment