From 9e40b4551d439c71a770a13c6668949cc19c9788 Mon Sep 17 00:00:00 2001
From: "alonso.torres" <alonso.torres@kaleidos.net>
Date: Tue, 9 Jan 2024 22:29:27 +0100
Subject: [PATCH] :bug: Fix problem with swap component

---
 .../src/app/main/data/workspace/libraries.cljs | 18 +++++++++++++-----
 .../main/data/workspace/libraries_helpers.cljs |  6 +++++-
 .../src/app/main/ui/workspace/sidebar.cljs     |  4 +---
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs
index 091cbe92c..292265dd9 100644
--- a/frontend/src/app/main/data/workspace/libraries.cljs
+++ b/frontend/src/app/main/data/workspace/libraries.cljs
@@ -20,6 +20,7 @@
    [app.common.types.components-list :as ctkl]
    [app.common.types.container :as ctn]
    [app.common.types.file :as ctf]
+   [app.common.types.shape.layout :as ctl]
    [app.common.types.typography :as ctt]
    [app.common.uuid :as uuid]
    [app.main.data.events :as ev]
@@ -826,7 +827,7 @@
             0)))))
 
 (defn- add-component-for-swap
-  [shape file-id id-new-component]
+  [shape file-id id-new-component target-cell]
   (dm/assert! (uuid? id-new-component))
   (dm/assert! (uuid? file-id))
   (ptk/reify ::add-component-for-swap
@@ -840,6 +841,7 @@
             position  (gpt/point (:x shape) (:y shape))
             changes   (-> (pcb/empty-changes it (:id page))
                           (pcb/with-objects objects))
+            position  (-> position (with-meta {:cell target-cell}))
 
             [new-shape changes]
             (dwlh/generate-instantiate-component changes
@@ -867,12 +869,18 @@
   (dm/assert! (uuid? file-id))
   (ptk/reify ::component-swap
     ptk/WatchEvent
-    (watch [_ _ _]
+    (watch [_ state _]
       ;; First delete shapes so we have space in the layout otherwise we can have problems
       ;; in the grid creating new rows/columns to make space
-      (rx/of (dwsh/delete-shapes nil (d/ordered-set (:id shape)) {:component-swap true})
-             (add-component-for-swap shape file-id id-new-component)
-             (ptk/data-event :layout/update [(:parent-id shape)])))))
+      (let [objects (wsh/lookup-page-objects state)
+            parent (get objects (:parent-id shape))
+
+            ;; If the target parent is a grid layout we need to pass the target cell
+            target-cell (when (ctl/grid-layout? parent)
+                          (ctl/get-cell-by-shape-id parent (:id shape)))]
+        (rx/of (dwsh/delete-shapes nil (d/ordered-set (:id shape)) {:component-swap true})
+               (add-component-for-swap shape file-id id-new-component target-cell)
+               (ptk/data-event :layout/update [(:parent-id shape)]))))))
 
 (defn component-multi-swap
   "Swaps several components with another one"
diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs
index 937079b2b..1d9c11318 100644
--- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs
+++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs
@@ -177,7 +177,11 @@
 
          changes
          (if (ctl/grid-layout? objects (:parent-id first-shape))
-           (let [[row column] (gslg/get-drop-cell (:parent-id first-shape) objects position)]
+           (let [target-cell (-> position meta :cell)
+                 [row column]
+                 (if (some? target-cell)
+                   [(:row target-cell) (:column target-cell)]
+                   (gslg/get-drop-cell (:parent-id first-shape) objects position))]
              (-> changes
                  (pcb/update-shapes
                   [(:parent-id first-shape)]
diff --git a/frontend/src/app/main/ui/workspace/sidebar.cljs b/frontend/src/app/main/ui/workspace/sidebar.cljs
index d0fba4fc2..faa27b292 100644
--- a/frontend/src/app/main/ui/workspace/sidebar.cljs
+++ b/frontend/src/app/main/ui/workspace/sidebar.cljs
@@ -55,9 +55,7 @@
         (mf/use-fn #(st/emit! (dw/toggle-layout-flag :collapse-left-sidebar)))
 
         on-tab-change
-        (mf/use-fn #(do
-                      (prn "on-tab-change" %)
-                      (st/emit! (dw/go-to-layout %))))]
+        (mf/use-fn #(st/emit! (dw/go-to-layout %)))]
 
     [:aside {:ref parent-ref
              :id "left-sidebar-aside"