2024-06-05 10:46:06 +08:00
|
|
|
import { useCallback } from 'react';
|
2024-04-28 19:03:54 +08:00
|
|
|
import ReactFlow, {
|
|
|
|
|
Background,
|
2024-06-11 19:31:52 +08:00
|
|
|
ConnectionMode,
|
2024-04-28 19:03:54 +08:00
|
|
|
Controls,
|
2024-05-23 18:53:04 +08:00
|
|
|
NodeMouseHandler,
|
2024-04-28 19:03:54 +08:00
|
|
|
} from 'reactflow';
|
|
|
|
|
import 'reactflow/dist/style.css';
|
|
|
|
|
|
2024-06-05 10:46:06 +08:00
|
|
|
import { ButtonEdge } from './edge';
|
2024-05-23 18:53:04 +08:00
|
|
|
|
|
|
|
|
import FlowDrawer from '../flow-drawer';
|
2024-05-29 10:01:39 +08:00
|
|
|
import {
|
|
|
|
|
useHandleDrop,
|
|
|
|
|
useHandleKeyUp,
|
2024-06-05 10:46:06 +08:00
|
|
|
useSelectCanvasData,
|
2024-05-29 10:01:39 +08:00
|
|
|
useShowDrawer,
|
2024-06-28 12:01:06 +08:00
|
|
|
useValidateConnection,
|
2024-05-29 10:01:39 +08:00
|
|
|
} from '../hooks';
|
2024-06-12 17:38:41 +08:00
|
|
|
import { RagNode } from './node';
|
2024-04-28 19:03:54 +08:00
|
|
|
|
2024-06-11 15:46:12 +08:00
|
|
|
import ChatDrawer from '../chat/drawer';
|
2024-06-05 10:46:06 +08:00
|
|
|
import styles from './index.less';
|
2024-06-27 14:57:40 +08:00
|
|
|
import { BeginNode } from './node/begin-node';
|
|
|
|
|
import { CategorizeNode } from './node/categorize-node';
|
2024-07-03 10:15:19 +08:00
|
|
|
import { RelevantNode } from './node/relevant-node';
|
2024-06-05 10:46:06 +08:00
|
|
|
|
2024-06-27 14:57:40 +08:00
|
|
|
const nodeTypes = {
|
|
|
|
|
ragNode: RagNode,
|
|
|
|
|
categorizeNode: CategorizeNode,
|
|
|
|
|
beginNode: BeginNode,
|
2024-07-03 10:15:19 +08:00
|
|
|
relevantNode: RelevantNode,
|
2024-06-27 14:57:40 +08:00
|
|
|
};
|
2024-04-28 19:03:54 +08:00
|
|
|
|
2024-06-05 10:46:06 +08:00
|
|
|
const edgeTypes = {
|
|
|
|
|
buttonEdge: ButtonEdge,
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-23 18:53:04 +08:00
|
|
|
interface IProps {
|
2024-06-11 15:46:12 +08:00
|
|
|
chatDrawerVisible: boolean;
|
|
|
|
|
hideChatDrawer(): void;
|
2024-05-23 18:53:04 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-13 09:09:34 +08:00
|
|
|
function FlowCanvas({ chatDrawerVisible, hideChatDrawer }: IProps) {
|
2024-06-05 10:46:06 +08:00
|
|
|
const {
|
|
|
|
|
nodes,
|
|
|
|
|
edges,
|
|
|
|
|
onConnect,
|
|
|
|
|
onEdgesChange,
|
|
|
|
|
onNodesChange,
|
|
|
|
|
onSelectionChange,
|
|
|
|
|
} = useSelectCanvasData();
|
2024-06-28 12:01:06 +08:00
|
|
|
const isValidConnection = useValidateConnection();
|
2024-05-29 10:01:39 +08:00
|
|
|
|
2024-06-05 10:46:06 +08:00
|
|
|
const { drawerVisible, hideDrawer, showDrawer, clickedNode } =
|
|
|
|
|
useShowDrawer();
|
2024-04-28 19:03:54 +08:00
|
|
|
|
2024-06-05 10:46:06 +08:00
|
|
|
const onNodeClick: NodeMouseHandler = useCallback(
|
|
|
|
|
(e, node) => {
|
|
|
|
|
showDrawer(node);
|
|
|
|
|
},
|
|
|
|
|
[showDrawer],
|
2024-04-28 19:03:54 +08:00
|
|
|
);
|
|
|
|
|
|
2024-07-05 10:44:14 +08:00
|
|
|
const onPaneClick = useCallback(() => {
|
|
|
|
|
hideDrawer();
|
|
|
|
|
}, [hideDrawer]);
|
|
|
|
|
|
2024-06-05 10:46:06 +08:00
|
|
|
const { onDrop, onDragOver, setReactFlowInstance } = useHandleDrop();
|
2024-05-29 10:01:39 +08:00
|
|
|
|
2024-06-05 10:46:06 +08:00
|
|
|
const { handleKeyUp } = useHandleKeyUp();
|
2024-04-28 19:03:54 +08:00
|
|
|
|
|
|
|
|
return (
|
2024-06-05 10:46:06 +08:00
|
|
|
<div className={styles.canvasWrapper}>
|
2024-07-02 11:43:57 +08:00
|
|
|
<svg
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
style={{ position: 'absolute', top: 10, left: 0 }}
|
|
|
|
|
>
|
|
|
|
|
<defs>
|
|
|
|
|
<marker
|
|
|
|
|
fill="rgb(157 149 225)"
|
|
|
|
|
id="logo"
|
|
|
|
|
viewBox="0 0 40 40"
|
|
|
|
|
refX="8"
|
|
|
|
|
refY="5"
|
|
|
|
|
markerUnits="strokeWidth"
|
|
|
|
|
markerWidth="20"
|
|
|
|
|
markerHeight="20"
|
|
|
|
|
orient="auto-start-reverse"
|
|
|
|
|
>
|
|
|
|
|
<path d="M 0 0 L 10 5 L 0 10 z" />
|
|
|
|
|
</marker>
|
|
|
|
|
</defs>
|
|
|
|
|
</svg>
|
2024-04-28 19:03:54 +08:00
|
|
|
<ReactFlow
|
2024-06-11 19:31:52 +08:00
|
|
|
connectionMode={ConnectionMode.Loose}
|
2024-04-28 19:03:54 +08:00
|
|
|
nodes={nodes}
|
|
|
|
|
onNodesChange={onNodesChange}
|
|
|
|
|
edges={edges}
|
|
|
|
|
onEdgesChange={onEdgesChange}
|
2024-05-23 18:53:04 +08:00
|
|
|
fitView
|
2024-04-28 19:03:54 +08:00
|
|
|
onConnect={onConnect}
|
|
|
|
|
nodeTypes={nodeTypes}
|
2024-06-05 10:46:06 +08:00
|
|
|
edgeTypes={edgeTypes}
|
2024-05-23 18:53:04 +08:00
|
|
|
onDrop={onDrop}
|
|
|
|
|
onDragOver={onDragOver}
|
|
|
|
|
onNodeClick={onNodeClick}
|
2024-07-05 10:44:14 +08:00
|
|
|
onPaneClick={onPaneClick}
|
2024-05-23 18:53:04 +08:00
|
|
|
onInit={setReactFlowInstance}
|
2024-05-29 10:01:39 +08:00
|
|
|
onKeyUp={handleKeyUp}
|
2024-06-05 10:46:06 +08:00
|
|
|
onSelectionChange={onSelectionChange}
|
|
|
|
|
nodeOrigin={[0.5, 0]}
|
2024-06-27 14:57:40 +08:00
|
|
|
isValidConnection={isValidConnection}
|
2024-06-07 13:46:50 +08:00
|
|
|
onChange={(...params) => {
|
|
|
|
|
console.info('params:', ...params);
|
|
|
|
|
}}
|
2024-06-05 10:46:06 +08:00
|
|
|
defaultEdgeOptions={{
|
|
|
|
|
type: 'buttonEdge',
|
2024-07-02 11:43:57 +08:00
|
|
|
markerEnd: 'logo',
|
|
|
|
|
// markerEnd: {
|
|
|
|
|
// type: MarkerType.ArrowClosed,
|
|
|
|
|
// color: 'rgb(157 149 225)',
|
|
|
|
|
// width: 20,
|
|
|
|
|
// height: 20,
|
|
|
|
|
// },
|
|
|
|
|
style: {
|
|
|
|
|
// edge style
|
|
|
|
|
strokeWidth: 2,
|
|
|
|
|
stroke: 'rgb(202 197 245)',
|
2024-06-05 10:46:06 +08:00
|
|
|
},
|
|
|
|
|
}}
|
2024-04-28 19:03:54 +08:00
|
|
|
>
|
|
|
|
|
<Background />
|
|
|
|
|
<Controls />
|
|
|
|
|
</ReactFlow>
|
2024-06-05 10:46:06 +08:00
|
|
|
<FlowDrawer
|
|
|
|
|
node={clickedNode}
|
|
|
|
|
visible={drawerVisible}
|
|
|
|
|
hideModal={hideDrawer}
|
|
|
|
|
></FlowDrawer>
|
2024-07-04 09:55:08 +08:00
|
|
|
{chatDrawerVisible && (
|
|
|
|
|
<ChatDrawer
|
|
|
|
|
visible={chatDrawerVisible}
|
|
|
|
|
hideModal={hideChatDrawer}
|
|
|
|
|
></ChatDrawer>
|
|
|
|
|
)}
|
2024-04-28 19:03:54 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FlowCanvas;
|