From 83f92d4b015507c7b88d67ee77fb1d29f783b925 Mon Sep 17 00:00:00 2001
From: Jerald-Golden-B <jerald@hexrfactory.com>
Date: Wed, 16 Apr 2025 10:16:54 +0530
Subject: [PATCH] feat: Enhance connection removal logic to handle deleted
 models and their points

---
 .../controls/selection/selectionControls.tsx  | 61 ++++++++++------
 .../modules/simulation/path/pathConnector.tsx | 73 ++++++++++---------
 2 files changed, 79 insertions(+), 55 deletions(-)

diff --git a/app/src/modules/scene/controls/selection/selectionControls.tsx b/app/src/modules/scene/controls/selection/selectionControls.tsx
index ece6924..8998bc9 100644
--- a/app/src/modules/scene/controls/selection/selectionControls.tsx
+++ b/app/src/modules/scene/controls/selection/selectionControls.tsx
@@ -270,6 +270,20 @@ const SelectionControls: React.FC = () => {
     };
 
     const removeConnections = (deletedModelUUIDs: string[]) => {
+
+        const deletedPointUUIDs = new Set<string>();
+        simulationStates.forEach(state => {
+            if (deletedModelUUIDs.includes(state.modeluuid)) {
+                if (state.type === "Conveyor" && state.points) {
+                    state.points.forEach(point => {
+                        deletedPointUUIDs.add(point.uuid);
+                    });
+                } else if (state.points && 'uuid' in state.points) {
+                    deletedPointUUIDs.add(state.points.uuid);
+                }
+            }
+        });
+
         const updatedStates = simulationStates.map((state) => {
             // Handle Conveyor
             if (state.type === "Conveyor") {
@@ -333,38 +347,41 @@ const SelectionControls: React.FC = () => {
                         ...state.points,
                         connections: {
                             ...state.points.connections,
-
                             targets: state.points.connections.targets.filter(
                                 (target: any) => !deletedModelUUIDs.includes(target.modelUUID)
                             ),
                         },
                         actions: {
                             ...state.points.actions,
-                            processes: (state.points.actions.processes =
-                                state.points.actions.processes?.filter((process) => {
-                                    const matchedStates = simulationStates.filter((s) => deletedModelUUIDs.includes(s.modeluuid));
+                            processes: state.points.actions.processes?.filter((process) => {
+                                // Check if trigger is from deleted model
+                                const matchedStates = simulationStates.filter((s) => deletedModelUUIDs.includes(s.modeluuid));
 
-                                    if (matchedStates.length > 0) {
-                                        if (matchedStates[0]?.type === "StaticMachine") {
-                                            const trigPoints = matchedStates[0]?.points;
-
-                                            return !(
-                                                process.triggerId === trigPoints?.triggers?.uuid
-                                            );
-                                        } else if (matchedStates[0]?.type === "Conveyor") {
-                                            const trigPoints = matchedStates[0]?.points;
-
-                                            if (Array.isArray(trigPoints)) {
-                                                const nonEmptyTriggers = trigPoints.filter((point) => point && point.triggers && point.triggers.length > 0);
-
-                                                const allTriggerUUIDs = nonEmptyTriggers.flatMap((point) => point.triggers).map((trigger) => trigger.uuid);
-
-                                                return !allTriggerUUIDs.includes(process.triggerId);
+                                if (matchedStates.length > 0) {
+                                    if (matchedStates[0]?.type === "StaticMachine") {
+                                        const trigPoints = matchedStates[0]?.points;
+                                        if (process.triggerId === trigPoints?.triggers?.uuid) {
+                                            return false;
+                                        }
+                                    } else if (matchedStates[0]?.type === "Conveyor") {
+                                        const trigPoints = matchedStates[0]?.points;
+                                        if (Array.isArray(trigPoints)) {
+                                            const nonEmptyTriggers = trigPoints.filter((point) => point && point.triggers && point.triggers.length > 0);
+                                            const allTriggerUUIDs = nonEmptyTriggers.flatMap((point) => point.triggers).map((trigger) => trigger.uuid);
+                                            if (allTriggerUUIDs.includes(process.triggerId)) {
+                                                return false;
                                             }
                                         }
                                     }
-                                    return true;
-                                })),
+                                }
+
+                                // Check if startPoint or endPoint is from deleted model
+                                if (deletedPointUUIDs.has(process.startPoint) || deletedPointUUIDs.has(process.endPoint)) {
+                                    return false;
+                                }
+
+                                return true;
+                            }),
                         },
                     },
                 };
diff --git a/app/src/modules/simulation/path/pathConnector.tsx b/app/src/modules/simulation/path/pathConnector.tsx
index 4ca12a2..3e35925 100644
--- a/app/src/modules/simulation/path/pathConnector.tsx
+++ b/app/src/modules/simulation/path/pathConnector.tsx
@@ -964,8 +964,21 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
     };
 
     const removeConnections = (deletedModelUUIDs: string[]) => {
-        const updatedStates = simulationStates.map((state) => {
 
+        const deletedPointUUIDs = new Set<string>();
+        simulationStates.forEach(state => {
+            if (deletedModelUUIDs.includes(state.modeluuid)) {
+                if (state.type === "Conveyor" && state.points) {
+                    state.points.forEach(point => {
+                        deletedPointUUIDs.add(point.uuid);
+                    });
+                } else if (state.points && 'uuid' in state.points) {
+                    deletedPointUUIDs.add(state.points.uuid);
+                }
+            }
+        });
+
+        const updatedStates = simulationStates.map((state) => {
             // Handle Conveyor
             if (state.type === "Conveyor") {
                 const updatedConveyor: SimulationTypes.ConveyorEventsSchema = {
@@ -1028,45 +1041,41 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
                         ...state.points,
                         connections: {
                             ...state.points.connections,
-
                             targets: state.points.connections.targets.filter(
                                 (target: any) => !deletedModelUUIDs.includes(target.modelUUID)
                             ),
                         },
                         actions: {
                             ...state.points.actions,
-                            processes: (state.points.actions.processes =
-                                state.points.actions.processes?.filter((process) => {
-                                    const matchedStates = simulationStates.filter((s) =>
-                                        deletedModelUUIDs.includes(s.modeluuid)
-                                    );
+                            processes: state.points.actions.processes?.filter((process) => {
+                                // Check if trigger is from deleted model
+                                const matchedStates = simulationStates.filter((s) => deletedModelUUIDs.includes(s.modeluuid));
 
-                                    if (matchedStates.length > 0) {
-                                        if (matchedStates[0]?.type === "StaticMachine") {
-                                            const trigPoints = matchedStates[0]?.points;
-
-                                            return !(
-                                                process.triggerId === trigPoints?.triggers?.uuid
-                                            );
-                                        } else if (matchedStates[0]?.type === "Conveyor") {
-                                            const trigPoints = matchedStates[0]?.points;
-
-                                            if (Array.isArray(trigPoints)) {
-                                                const nonEmptyTriggers = trigPoints.filter(
-                                                    (point) =>
-                                                        point && point.triggers && point.triggers.length > 0
-                                                );
-
-                                                const allTriggerUUIDs = nonEmptyTriggers
-                                                    .flatMap((point) => point.triggers)
-                                                    .map((trigger) => trigger.uuid);
-
-                                                return !allTriggerUUIDs.includes(process.triggerId);
+                                if (matchedStates.length > 0) {
+                                    if (matchedStates[0]?.type === "StaticMachine") {
+                                        const trigPoints = matchedStates[0]?.points;
+                                        if (process.triggerId === trigPoints?.triggers?.uuid) {
+                                            return false;
+                                        }
+                                    } else if (matchedStates[0]?.type === "Conveyor") {
+                                        const trigPoints = matchedStates[0]?.points;
+                                        if (Array.isArray(trigPoints)) {
+                                            const nonEmptyTriggers = trigPoints.filter((point) => point && point.triggers && point.triggers.length > 0);
+                                            const allTriggerUUIDs = nonEmptyTriggers.flatMap((point) => point.triggers).map((trigger) => trigger.uuid);
+                                            if (allTriggerUUIDs.includes(process.triggerId)) {
+                                                return false;
                                             }
                                         }
                                     }
-                                    return true;
-                                })),
+                                }
+
+                                // Check if startPoint or endPoint is from deleted model
+                                if (deletedPointUUIDs.has(process.startPoint) || deletedPointUUIDs.has(process.endPoint)) {
+                                    return false;
+                                }
+
+                                return true;
+                            }),
                         },
                     },
                 };
@@ -1076,9 +1085,7 @@ function PathConnector({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObje
             return state;
         });
 
-        const filteredStates = updatedStates.filter(
-            (state) => !deletedModelUUIDs.includes(state.modeluuid)
-        );
+        const filteredStates = updatedStates.filter((state) => !deletedModelUUIDs.includes(state.modeluuid));
 
         updateBackend(filteredStates);
         setSimulationStates(filteredStates);