Skip to content

Commit

Permalink
feat: fix the problem of form entries being deleted when adding a new…
Browse files Browse the repository at this point in the history
… line infiniflow#918 and clear the selection box to delete the corresponding edge (infiniflow#1301)

### What problem does this PR solve?
feat: clear the selection box to delete the corresponding edge. infiniflow#918
feat: fix the problem of form entries being deleted when adding a new
line infiniflow#918

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Jun 28, 2024
1 parent a319698 commit 017836b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 32 deletions.
43 changes: 28 additions & 15 deletions web/src/pages/flow/categorize-form/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ const buildCategorizeObjectFromList = (list: Array<ICategorizeItem>) => {
export const useHandleFormValuesChange = ({
onValuesChange,
form,
node,
nodeId,
}: IOperatorForm) => {
const edges = useGraphStore((state) => state.edges);
const getNode = useGraphStore((state) => state.getNode);
const node = getNode(nodeId);

const handleValuesChange = useCallback(
(changedValues: any, values: any) => {
Expand All @@ -94,32 +96,43 @@ export const useHandleFormValuesChange = ({
);

useEffect(() => {
const items = buildCategorizeListFromObject(
get(node, 'data.form.category_description', {}),
edges,
node,
);
form?.setFieldsValue({
items: buildCategorizeListFromObject(
get(node, 'data.form.category_description', {}),
edges,
node,
),
items,
});
}, [form, node, edges]);

return { handleValuesChange };
};

export const useHandleToSelectChange = (nodeId?: string) => {
const { addEdge } = useGraphStore((state) => state);
const { addEdge, deleteEdgeBySourceAndSourceHandle } = useGraphStore(
(state) => state,
);
const handleSelectChange = useCallback(
(name?: string) => (value?: string) => {
if (nodeId && value && name) {
addEdge({
source: nodeId,
target: value,
sourceHandle: name,
targetHandle: null,
});
if (nodeId && name) {
if (value) {
addEdge({
source: nodeId,
target: value,
sourceHandle: name,
targetHandle: null,
});
} else {
// clear selected value
deleteEdgeBySourceAndSourceHandle({
source: nodeId,
sourceHandle: name,
});
}
}
},
[addEdge, nodeId],
[addEdge, nodeId, deleteEdgeBySourceAndSourceHandle],
);

return { handleSelectChange };
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/flow/categorize-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const CategorizeForm = ({ form, onValuesChange, node }: IOperatorForm) => {
const { t } = useTranslate('flow');
const { handleValuesChange } = useHandleFormValuesChange({
form,
node,
nodeId: node?.id,
onValuesChange,
});
useSetLlmSetting(form);
Expand Down
8 changes: 1 addition & 7 deletions web/src/pages/flow/constant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,7 @@ export const CategorizeAnchorPointPositions = [
// key is the source of the edge, value is the target of the edge
// no connection lines are allowed between key and value
export const RestrictedUpstreamMap = {
[Operator.Begin]: [
Operator.Begin,
Operator.Answer,
Operator.Categorize,
Operator.Generate,
Operator.Retrieval,
],
[Operator.Begin]: [],
[Operator.Categorize]: [Operator.Begin, Operator.Categorize, Operator.Answer],
[Operator.Answer]: [],
[Operator.Retrieval]: [],
Expand Down
1 change: 1 addition & 0 deletions web/src/pages/flow/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IOperatorForm {
onValuesChange?(changedValues: any, values: any): void;
form?: FormInstance;
node?: Node<NodeData>;
nodeId?: string;
}

export interface IBeginForm {
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/flow/mock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const dsl = {
graph: {
nodes: [
{
id: 'begin',
id: 'Begin',
type: 'beginNode',
position: {
x: 50,
Expand Down
19 changes: 12 additions & 7 deletions web/src/pages/flow/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export type RFState = {
updateNodeForm: (nodeId: string, values: any) => void;
onSelectionChange: OnSelectionChangeFunc;
addNode: (nodes: Node) => void;
getNode: (id: string) => Node | undefined;
getNode: (id?: string) => Node | undefined;
addEdge: (connection: Connection) => void;
getEdge: (id: string) => Edge | undefined;
deletePreviousEdgeOfClassificationNode: (connection: Connection) => void;
duplicateNode: (id: string) => void;
deleteEdge: () => void;
deleteEdgeById: (id: string) => void;
deleteNodeById: (id: string) => void;
deleteEdgeBySourceAndTarget: (source: string, target: string) => void;
deleteEdgeBySourceAndSourceHandle: (connection: Partial<Connection>) => void;
findNodeByName: (operatorName: Operator) => Node | undefined;
updateMutableNodeFormItem: (id: string, field: string, value: any) => void;
};
Expand Down Expand Up @@ -87,7 +87,7 @@ const useGraphStore = create<RFState>()(
addNode: (node: Node) => {
set({ nodes: get().nodes.concat(node) });
},
getNode: (id: string) => {
getNode: (id?: string) => {
return get().nodes.find((x) => x.id === id);
},
addEdge: (connection: Connection) => {
Expand Down Expand Up @@ -150,12 +150,17 @@ const useGraphStore = create<RFState>()(
edges: edges.filter((edge) => edge.id !== id),
});
},
deleteEdgeBySourceAndTarget: (source: string, target: string) => {
deleteEdgeBySourceAndSourceHandle: ({
source,
sourceHandle,
}: Partial<Connection>) => {
const { edges } = get();
const nextEdges = edges.filter(
(edge) =>
edge.source !== source || edge.sourceHandle !== sourceHandle,
);
set({
edges: edges.filter(
(edge) => edge.target !== target && edge.source !== source,
),
edges: nextEdges,
});
},
deleteNodeById: (id: string) => {
Expand Down
3 changes: 2 additions & 1 deletion web/src/pages/flow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export const getOperatorTypeFromId = (id: string | null) => {

// restricted lines cannot be connected successfully.
export const isValidConnection = (connection: Connection) => {
return RestrictedUpstreamMap[
const ret = RestrictedUpstreamMap[
getOperatorTypeFromId(connection.source) as Operator
]?.every((x) => x !== getOperatorTypeFromId(connection.target));
return ret;
};

0 comments on commit 017836b

Please sign in to comment.