|
3 | 3 | * SPDX-License-Identifier: Apache-2.0
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -import React from 'react'; |
| 6 | +import React, { useRef, useContext, useCallback, useEffect } from 'react'; |
| 7 | +import ReactFlow, { |
| 8 | + Controls, |
| 9 | + Background, |
| 10 | + useNodesState, |
| 11 | + useEdgesState, |
| 12 | + addEdge, |
| 13 | +} from 'reactflow'; |
7 | 14 | import { useSelector } from 'react-redux';
|
8 |
| -import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; |
9 |
| -import { AppState } from '../../../store'; |
10 |
| -import { WorkspaceComponent } from '../workspace_component'; |
| 15 | +import { EuiFlexItem, EuiFlexGroup } from '@elastic/eui'; |
| 16 | +import { AppState, rfContext } from '../../../store'; |
| 17 | +import { convertToReactFlowData } from '../../../../common'; |
11 | 18 |
|
12 |
| -export function Workspace() { |
13 |
| - const { components } = useSelector((state: AppState) => state.workspace); |
| 19 | +// styling |
| 20 | +import 'reactflow/dist/style.css'; |
| 21 | +import './reactflow-styles.scss'; |
| 22 | + |
| 23 | +// eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 24 | +interface WorkspaceProps {} |
| 25 | + |
| 26 | +export function Workspace(props: WorkspaceProps) { |
| 27 | + const reactFlowWrapper = useRef(null); |
| 28 | + const { reactFlowInstance, setReactFlowInstance } = useContext(rfContext); |
| 29 | + |
| 30 | + // Fetching workspace state to populate the initial nodes/edges. |
| 31 | + // Where/how the low-level ReactFlow JSON will be persisted is TBD. |
| 32 | + // TODO: update when that design is finalized |
| 33 | + const storedComponents = useSelector( |
| 34 | + (state: AppState) => state.workspace.components |
| 35 | + ); |
| 36 | + const { rfNodes, rfEdges } = convertToReactFlowData(storedComponents); |
| 37 | + const [nodes, setNodes, onNodesChange] = useNodesState(rfNodes); |
| 38 | + const [edges, setEdges, onEdgesChange] = useEdgesState(rfEdges); |
| 39 | + |
| 40 | + const onConnect = useCallback( |
| 41 | + (params) => { |
| 42 | + setEdges((eds) => addEdge(params, eds)); |
| 43 | + }, |
| 44 | + // TODO: add customized logic to prevent connections based on the node's |
| 45 | + // allowed inputs. If allowed, update that node state as well with the added |
| 46 | + // connection details. |
| 47 | + [setEdges] |
| 48 | + ); |
| 49 | + |
| 50 | + const onDragOver = useCallback((event) => { |
| 51 | + event.preventDefault(); |
| 52 | + event.dataTransfer.dropEffect = 'move'; |
| 53 | + }, []); |
| 54 | + |
| 55 | + const onDrop = useCallback( |
| 56 | + (event) => { |
| 57 | + event.preventDefault(); |
| 58 | + // Get the node info from the event metadata |
| 59 | + const nodeData = event.dataTransfer.getData('application/reactflow'); |
| 60 | + |
| 61 | + // check if the dropped element is valid |
| 62 | + if (typeof nodeData === 'undefined' || !nodeData) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Fetch bounds based on the ref'd div component, adjust as needed. |
| 67 | + // TODO: remove hardcoded bounds and fetch from a constant somewhere |
| 68 | + // @ts-ignore |
| 69 | + const reactFlowBounds = reactFlowWrapper.current.getBoundingClientRect(); |
| 70 | + // @ts-ignore |
| 71 | + const position = reactFlowInstance.project({ |
| 72 | + x: event.clientX - reactFlowBounds.left - 80, |
| 73 | + y: event.clientY - reactFlowBounds.top - 90, |
| 74 | + }); |
| 75 | + |
| 76 | + // TODO: remove hardcoded values when more component info is passed in the event. |
| 77 | + // Only keep the calculated 'positioning' field. |
| 78 | + const newNode = { |
| 79 | + // TODO: generate ID based on the node data maybe |
| 80 | + id: Date.now().toFixed(), |
| 81 | + type: nodeData.type, |
| 82 | + position, |
| 83 | + data: { label: nodeData.label }, |
| 84 | + style: { |
| 85 | + background: 'white', |
| 86 | + }, |
| 87 | + }; |
| 88 | + |
| 89 | + setNodes((nds) => nds.concat(newNode)); |
| 90 | + }, |
| 91 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 92 | + [reactFlowInstance] |
| 93 | + ); |
| 94 | + |
| 95 | + // Initialization hook |
| 96 | + useEffect(() => { |
| 97 | + // TODO: fetch the nodes/edges dynamically (loading existing flow, |
| 98 | + // creating fresh from template, creating blank template, etc.) |
| 99 | + // Will involve populating and/or fetching from redux store |
| 100 | + }, []); |
14 | 101 |
|
15 | 102 | return (
|
16 |
| - <EuiFlexGroup direction="row"> |
17 |
| - {components.map((component, idx) => { |
18 |
| - return ( |
19 |
| - <EuiFlexItem key={idx}> |
20 |
| - <WorkspaceComponent component={component} /> |
21 |
| - </EuiFlexItem> |
22 |
| - ); |
23 |
| - })} |
24 |
| - </EuiFlexGroup> |
| 103 | + <EuiFlexItem grow={true}> |
| 104 | + <EuiFlexGroup |
| 105 | + direction="column" |
| 106 | + gutterSize="m" |
| 107 | + justifyContent="spaceBetween" |
| 108 | + className="workspace" |
| 109 | + > |
| 110 | + <EuiFlexItem |
| 111 | + style={{ |
| 112 | + borderStyle: 'groove', |
| 113 | + borderColor: 'gray', |
| 114 | + borderWidth: '1px', |
| 115 | + }} |
| 116 | + > |
| 117 | + {/** |
| 118 | + * We have these wrapper divs & reactFlowWrapper ref to control and calculate the |
| 119 | + * ReactFlow bounds when calculating node positioning. |
| 120 | + */} |
| 121 | + <div className="reactflow-parent-wrapper"> |
| 122 | + <div className="reactflow-wrapper" ref={reactFlowWrapper}> |
| 123 | + <ReactFlow |
| 124 | + nodes={nodes} |
| 125 | + edges={edges} |
| 126 | + onNodesChange={onNodesChange} |
| 127 | + onEdgesChange={onEdgesChange} |
| 128 | + onConnect={onConnect} |
| 129 | + onInit={setReactFlowInstance} |
| 130 | + onDrop={onDrop} |
| 131 | + onDragOver={onDragOver} |
| 132 | + fitView |
| 133 | + > |
| 134 | + <Controls /> |
| 135 | + <Background /> |
| 136 | + </ReactFlow> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + </EuiFlexItem> |
| 140 | + </EuiFlexGroup> |
| 141 | + </EuiFlexItem> |
25 | 142 | );
|
26 | 143 | }
|
| 144 | + |
| 145 | +// TODO: remove later, leaving for reference |
| 146 | + |
| 147 | +// export function Workspace() { |
| 148 | +// const { components } = useSelector((state: AppState) => state.workspace); |
| 149 | + |
| 150 | +// return ( |
| 151 | +// <EuiFlexGroup direction="row"> |
| 152 | +// {components.map((component, idx) => { |
| 153 | +// return ( |
| 154 | +// <EuiFlexItem key={idx}> |
| 155 | +// <WorkspaceComponent component={component} /> |
| 156 | +// </EuiFlexItem> |
| 157 | +// ); |
| 158 | +// })} |
| 159 | +// </EuiFlexGroup> |
| 160 | +// ); |
| 161 | +// } |
0 commit comments