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-06-05 10:46:06 +08:00
|
|
|
MarkerType,
|
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-06-05 10:46:06 +08:00
|
|
|
|
2024-06-27 14:57:40 +08:00
|
|
|
const nodeTypes = {
|
|
|
|
|
ragNode: RagNode,
|
|
|
|
|
categorizeNode: CategorizeNode,
|
|
|
|
|
beginNode: BeginNode,
|
|
|
|
|
};
|
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-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-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}
|
|
|
|
|
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',
|
|
|
|
|
markerEnd: {
|
|
|
|
|
type: MarkerType.ArrowClosed,
|
|
|
|
|
},
|
|
|
|
|
}}
|
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-06-11 15:46:12 +08:00
|
|
|
<ChatDrawer
|
|
|
|
|
visible={chatDrawerVisible}
|
|
|
|
|
hideModal={hideChatDrawer}
|
|
|
|
|
></ChatDrawer>
|
2024-04-28 19:03:54 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FlowCanvas;
|