Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Enhance cleanup and error handling in drag-and-drop interactions

Muhammad Hamza Khan812d334903ef
diff --git a/packages/app/src/builder-ui/components/Component.class/index.ts b/packages/app/src/builder-ui/components/Component.class/index.ts
index 2fac8b47..5dc1c2a2 100644
--- a/packages/app/src/builder-ui/components/Component.class/index.ts
+++ b/packages/app/src/builder-ui/components/Component.class/index.ts
@@ -779,7 +779,14 @@ export class Component extends EventEmitter {
}
public destroy() {
this._destroyed = true;
- this.domElement.remove();
+ if (this.domElement) {
+ try {
+ interact(this.domElement).unset();
+ this.domElement.remove();
+ } catch (error) {
+ console.error(error);
+ }
+ }
}
public getSettingsSidebar() {
if (Component.curComponentSettings !== this) return null;
diff --git a/packages/app/src/builder-ui/pages/builder/components-menu.ts b/packages/app/src/builder-ui/pages/builder/components-menu.ts
index ea22d865..68592d18 100644
--- a/packages/app/src/builder-ui/pages/builder/components-menu.ts
+++ b/packages/app/src/builder-ui/pages/builder/components-menu.ts
@@ -610,11 +610,16 @@ function isDroppedInCanvasArea(
}
function setupBuilderMenuDragDrop() {
+ // Store interact instances
+ const interactInstances = [];
+
// Initialize the dummy dragging div
let dummyDiv = null;
- interact(
+ const leftMenuInteract = interact(
'#left-menu a[smt-component], #left-sidebar-integrations-menu a[smt-component]',
- ).draggable({
+ );
+ interactInstances.push(leftMenuInteract);
+ leftMenuInteract.draggable({
// Create the dummy div when starting the drag
onstart: function (event) {
if (workspace?.locked) return false;
@@ -688,7 +693,9 @@ function setupBuilderMenuDragDrop() {
},
});
- interact('#workspace-container').dropzone({
+ const workspaceContainerInteract = interact('#workspace-container');
+ interactInstances.push(workspaceContainerInteract);
+ workspaceContainerInteract.dropzone({
// Only accept elements matching this CSS selector
accept: 'a[smt-component]',
// Listen for drop related events
@@ -797,7 +804,11 @@ function setupBuilderMenuDragDrop() {
},
});
- interact('#right-sidebar,#embodiment-sidebar,#agent-settings-sidebar')
+ const rightSidebarInteract = interact(
+ '#right-sidebar,#embodiment-sidebar,#agent-settings-sidebar',
+ );
+ interactInstances.push(rightSidebarInteract);
+ rightSidebarInteract
.resizable({
margin: 5,
// Enable resize from right edge; preset cursor styles
@@ -857,7 +868,9 @@ function setupBuilderMenuDragDrop() {
});
// temporary debug console height: move to a better place
- interact('#bottom-bar')
+ const bottomBarInteract = interact('#bottom-bar');
+ interactInstances.push(bottomBarInteract);
+ bottomBarInteract
.resizable({
margin: 5,
edges: { left: false, right: false, bottom: false, top: true },
@@ -887,6 +900,17 @@ function setupBuilderMenuDragDrop() {
localStorage.setItem(`debug-console-height`, event.target.style.height);
});
+
+ // Add cleanup on page unload
+ window.addEventListener('beforeunload', () => {
+ try {
+ interactInstances.forEach((interactInstance) => {
+ interactInstance?.unset();
+ });
+ } catch (error) {
+ console.error(error);
+ }
+ });
}
function renderUpgradeModal() {
diff --git a/packages/app/views/pages/builder.ejs b/packages/app/views/pages/builder.ejs
index c65f26be..a5648841 100644
--- a/packages/app/views/pages/builder.ejs
+++ b/packages/app/views/pages/builder.ejs
@@ -428,5 +428,10 @@ include('partials/studio/feedback-modal') -%>
// --- Initial Layout Update ---
// Calls updateLayout once on load to set the initial layout.
updateLayout();
+
+ // Add cleanup on page unload
+ window.addEventListener('beforeunload', () => {
+ layoutObserver?.disconnect();
+ });
});
</script>