Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): SubWork Flow Multi Select Deletion #743

Merged
merged 11 commits into from
Jan 9, 2025
43 changes: 26 additions & 17 deletions ui/src/lib/yjs/useYWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,38 @@ export default ({
(nodeIds: string[]) =>
undoTrackerActionWrapper(() => {
const workflowIds: string[] = [];

const removeNodes = (nodeIds: string[]) => {
nodeIds.forEach((nid) => {
if (nid === DEFAULT_ENTRY_GRAPH_ID) return;

const index = rawWorkflows.findIndex((w) => w.id === nid);
if (index === -1) return;

// Loop over workflow at current index and remove any subworkflow nodes
(rawWorkflows[index].nodes as Node[]).forEach((node) => {
nodeIds.forEach((nid) => {
if (nid === DEFAULT_ENTRY_GRAPH_ID) return;
workflowIds.push(nid);
const workflow = rawWorkflows.find((w) => w.id === nid);
const nodes = workflow?.nodes;
// Loop over workflow and remove any subworkflow nodes
if (nodes && Array.isArray(nodes)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change iterates on one level deep, and then stops. This is why removeNodes was using itself, to be able to reach ALL subworkflows, no matter how deep.
If you can console.log rawWorkflows and test to make sure when deleting subworkflows that contain subworkflows, that all appropriate workflows are removed on subworkflow node deletion 🙏🏻

(nodes as Node[]).forEach((node) => {
if (node.type === "subworkflow") {
removeNodes([node.id]);
workflowIds.push(node.id);
}
});
}
});

workflowIds.push(nid);
yWorkflows.delete(index);
});
};
// Indexes in descending order to avoid index shifting problems
const indexesToRemove = workflowIds
.map((id) => rawWorkflows.findIndex((w) => w.id === id))
.filter((index) => index !== -1)
.sort((a, b) => b - a);

indexesToRemove.forEach((index) => {
yWorkflows.delete(index);
});

removeNodes(nodeIds);
setWorkflows((currentWorkflows) => {
const remainingWorkflows = currentWorkflows.filter(
(w) => !workflowIds.includes(w.id),
);
return remainingWorkflows;
});

setWorkflows((w) => w.filter((w) => !workflowIds.includes(w.id)));
setOpenWorkflowIds((ids) =>
ids.filter((id) => !workflowIds.includes(id)),
);
Expand Down
Loading