Repository: penpot/penpot
File: frontend/src/app/main/data/workspace/pages.cljs (ClojureScript)

Code before:
                 objs

                 ;; If it is a variant-container or a variant component,
                 ;; update its values to new ids
                 (contains? variants-ids-map id)
                 (assoc objs id (update-variant-values shape))

                 ;; In other case, simply copy it
                 :else
                 (assoc objs id shape)))
             {}
             objects)

            ;; Remap parents and childs with the new ids generated for variants
            objects
            (reduce
             (fn [objs [_ shape]]
               (assoc objs
                      (get variants-ids-map (:id shape) (:id shape))
                      (-> shape
                          (assoc :id (get variants-ids-map (:id shape) (:id shape)))
                          (assoc :parent-id (get variants-ids-map (:parent-id shape) (:parent-id shape)))
                          (assoc :frame-id (get variants-ids-map (:frame-id shape) (:frame-id shape)))
                          (cond->
                           (contains? shape :shapes)
                            (assoc :shapes (mapv #(get variants-ids-map % %) (:shapes shape)))))))
             {}
             objects)

            page    (-> page
                        (assoc :name name)
                        (assoc :id new-page-id)
                        (assoc :objects
                               objects))

            changes (-> (pcb/empty-changes it)
                        (pcb/add-page new-page-id page)
                        (pcb/with-page page)
                        (pcb/with-objects objects)
                        ;; Create new components for each duplicated variant main
                        (as-> changes
                              (reduce add-comp changes variant-mains)))]
        (rx/of (dch/commit-changes changes))))))

(s/def ::rename-page
  (s/keys :req-un [::id ::name]))

(defn rename-page
  [id name]
  (dm/assert! (uuid? id))
  (dm/assert! (string? name))

Review comment (suggestion):
Several enhancements to this code:

- Use reduce-kv instead of reduce, ignoring the id in the same way but you don't need to use destructuring.
- Destructure the `:id` `:frame-id` and `:parent-id` so you don't need lookup these values on shape several times per iteration.

Code after:
                 objs

                 ;; If it is a variant-container or a variant component,
                 ;; update its values to new ids
                 (contains? variants-ids-map id)
                 (assoc objs id (update-variant-values shape))

                 ;; In other case, simply copy it
                 :else
                 (assoc objs id shape)))
             {}
             objects)

            ;; Remap parents and childs with the new ids generated for variants
            objects
            (reduce-kv
             (fn [objs _ {:keys [id parent-id frame-id] :as shape}]
               (assoc objs
                      (get variants-ids-map id id)
                      (-> shape
                          (assoc :id (get variants-ids-map id id))
                          (assoc :parent-id (get variants-ids-map parent-id parent-id))
                          (assoc :frame-id (get variants-ids-map frame-id frame-id))
                          (cond->
                           (contains? shape :shapes)
                            (assoc :shapes (mapv #(get variants-ids-map % %) (:shapes shape)))))))
             {}
             objects)

            page    (-> page
                        (assoc :name name)
                        (assoc :id new-page-id)
                        (assoc :objects
                               objects))

            changes (-> (pcb/empty-changes it)
                        (pcb/add-page new-page-id page)
                        (pcb/with-page page)
                        (pcb/with-objects objects)
                        ;; Create new components for each duplicated variant main
                        (as-> changes
                              (reduce add-comp changes variant-mains)))]
        (rx/of (dch/commit-changes changes))))))

(s/def ::rename-page
  (s/keys :req-un [::id ::name]))

(defn rename-page
  [id name]
  (dm/assert! (uuid? id))
  (dm/assert! (string? name))