Skip to content

Commit

Permalink
fix: 修复transition问题
Browse files Browse the repository at this point in the history
  • Loading branch information
myshell-joe committed Sep 19, 2024
1 parent ca439fa commit 4d52b82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 42 deletions.
42 changes: 13 additions & 29 deletions web/apps/web/src/components/app/transition-sheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import { useReactFlowStore, getColor } from '@shellagent/flow-engine';
import { TValues } from '@shellagent/form-engine';
import { FormRef, Drawer } from '@shellagent/ui';
import { Drawer } from '@shellagent/ui';
import { useInjection } from 'inversify-react';
import { useEffect, useRef, useMemo, useCallback, memo } from 'react';
import { useMemo, useCallback, memo } from 'react';

import {
ICondition,
Expand Down Expand Up @@ -45,19 +45,13 @@ const TransitionSheet: React.FC<{}> = () => {
runDrawerWidth: state.runDrawerWidth,
}));

const formRef = useRef<FormRef>(null);
const loaded = useRef<boolean>(false);

useEffect(() => {
if (!loaded.current && transitionSheetOpen) {
const transitions = edgeData2Form(edges as ICustomEdge[], sourceHandle);
formRef.current?.reset({ transitions });
loaded.current = true;
}
}, [edges, sourceHandle, transitionSheetOpen]);
const handleClose = useCallback(() => {
setTransitionSheetOpen({ open: false, source: '', sourceHandle: '' });
}, [setTransitionSheetOpen]);

const handleTransitionsChange = useCallback(
(transitions: ICondition[] = []) => {
const handleChange = useCallback(
(values: TValues) => {
const transitions = (values?.transitions || []) as ICondition[];
const { edgesToAdd, edgesToDelete, edgesToModify } = form2EdgeData({
edges: edges as ICustomEdge[],
values: transitions.map(transition => ({
Expand Down Expand Up @@ -130,22 +124,12 @@ const TransitionSheet: React.FC<{}> = () => {
}),
);
},
[edges, onConnect, setEdges, sourceHandle, source],
);

const handleClose = useCallback(() => {
setTransitionSheetOpen({ open: false, source: '', sourceHandle: '' });
}, [setTransitionSheetOpen]);

const handleChange = useCallback(
(values: TValues) => {
handleTransitionsChange(values?.transitions || []);
},
[handleTransitionsChange],
[edges, onConnect, setEdges, sourceHandle],
);

const values = useMemo<ICondition[]>(() => {
return edgeData2Form(edges as ICustomEdge[], sourceHandle);
const values = useMemo(() => {
const transitions = edgeData2Form(edges as ICustomEdge[], sourceHandle);
return { transitions };
}, [edges, sourceHandle]);

return (
Expand All @@ -167,7 +151,7 @@ const TransitionSheet: React.FC<{}> = () => {
push={false}>
<VariableProvider id={source} eventKey={currentEdegData?.event_key}>
<NodeForm
ref={formRef}
key={sourceHandle}
schema={transitionConfigSchema}
values={values}
onChange={handleChange}
Expand Down
24 changes: 11 additions & 13 deletions web/apps/web/src/components/app/transition-sheet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export const form2EdgeData = ({
const edgesToDelete: ICustomEdge[] = [];
const edgesToModify: { edge: ICustomEdge; newData: ICondition }[] = [];

const edgeMap = new Map<string, ICustomEdge>();
edges.forEach(edge => {
if (edge.sourceHandle === sourceHandle) {
edgeMap.set(edge.target, edge);
}
});

values.forEach(v => {
const id = `${sourceHandle}-${v.target}`;
if (!result[id] || !result[id]?.conditions) {
Expand All @@ -53,26 +60,17 @@ export const form2EdgeData = ({
}
result[id].conditions?.push(v.condition);

// 检查是否需要添加新边
const existingEdge = edges.find(
edge => edge.sourceHandle === sourceHandle && edge.target === v.target,
);
const existingEdge = edgeMap.get(v.target);
if (!existingEdge) {
edgesToAdd.push(v);
} else if (JSON.stringify(existingEdge.data) !== JSON.stringify(v)) {
edgesToModify.push({ edge: existingEdge, newData: v });
}
});

// 检查需要删除的边
edges.forEach(edge => {
if (edge.sourceHandle === sourceHandle) {
const matchingNewTransition = values.find(
nt => nt.target === edge.target,
);
if (!matchingNewTransition) {
edgesToDelete.push(edge);
}
edgeMap.forEach((edge, target) => {
if (!values.some(v => v.target === target)) {
edgesToDelete.push(edge);
}
});

Expand Down

0 comments on commit 4d52b82

Please sign in to comment.