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

Code before:
    ptk/WatchEvent
    (watch [_ state _]
      (let [variants?             (features/active-feature? state "variants/v1")
            objects               (dsh/lookup-page-objects state)
            selected-ids          (dsh/lookup-selected state)
            selected-shapes       (map (d/getf objects) selected-ids)
            add-new-variant?      (and variants?
                                       (every? ctc/is-variant? selected-shapes))
            undo-id              (js/Symbol)]
        (if add-new-variant?
          (rx/concat
           (rx/of (dwu/start-undo-transaction undo-id))
           (rx/from (map add-new-variant selected-ids))
           (rx/of (dwu/commit-undo-transaction undo-id)))
          (rx/of (dws/duplicate-selected true)))))))


(defn rename-variant
  [variant-id name]
  (ptk/reify ::rename-variant

    ptk/WatchEvent
    (watch [_ state _]
      (let [data               (dsh/lookup-file-data state)
            objects            (dsh/lookup-page-objects state)
            variant-components (cfv/find-variant-components data objects variant-id)
            clean-name         (cfh/clean-path name)
            undo-id            (js/Symbol)]

        (rx/concat
         (rx/of (dwu/start-undo-transaction undo-id)
                (dwsh/update-shapes [variant-id] #(assoc % :name clean-name)))
         (rx/from (map
                   #(dwl/rename-component-and-main-instance (:id %) clean-name)
                   variant-components))
         (rx/of (dwu/commit-undo-transaction undo-id)))))))


(defn rename-comp-or-variant-and-main
  [component-id name]
  (ptk/reify ::rename-comp-or-variant-and-main

    ptk/WatchEvent
    (watch [_ state _]
      (let [data               (dsh/lookup-file-data state)
            component          (ctkl/get-component data component-id)]
        (if (ctc/is-variant? component)
          (rx/of (rename-variant (:variant-id component) name))
          (rx/of (dwl/rename-component-and-main-instance component-id name)))))))



Review comment (refactor):
And here you should avoid duplicate lookups using `dsh/get-page` + `(get page :objects)` istead of `(dsh/lookup-page-objects state)`

Code after:
      (let [variants?             (features/active-feature? state "variants/v1")
            objects               (dsh/lookup-page-objects state)
            selected-ids          (dsh/lookup-selected state)
            selected-shapes       (map (d/getf objects) selected-ids)
            add-new-variant?      (and variants?
                                       (every? ctc/is-variant? selected-shapes))
            undo-id              (js/Symbol)]
        (if add-new-variant?
          (rx/concat
           (rx/of (dwu/start-undo-transaction undo-id))
           (rx/from (map add-new-variant selected-ids))
           (rx/of (dwu/commit-undo-transaction undo-id)))
          (rx/of (dws/duplicate-selected true)))))))


(defn rename-variant
  [variant-id name]
  (ptk/reify ::rename-variant

    ptk/WatchEvent
    (watch [_ state _]
      (let [page-id            (:current-page-id state)
            data               (dsh/lookup-file-data state)
            objects            (-> (dsh/get-page data page-id)
                                   (get :objects))
            variant-components (cfv/find-variant-components data objects variant-id)
            clean-name         (cfh/clean-path name)
            undo-id            (js/Symbol)]

        (rx/concat
         (rx/of (dwu/start-undo-transaction undo-id)
                (dwsh/update-shapes [variant-id] #(assoc % :name clean-name)))
         (rx/from (map
                   #(dwl/rename-component-and-main-instance (:id %) clean-name)
                   variant-components))
         (rx/of (dwu/commit-undo-transaction undo-id)))))))


(defn rename-comp-or-variant-and-main
  [component-id name]
  (ptk/reify ::rename-comp-or-variant-and-main

    ptk/WatchEvent
    (watch [_ state _]
      (let [data               (dsh/lookup-file-data state)
            component          (ctkl/get-component data component-id)]
        (if (ctc/is-variant? component)
          (rx/of (rename-variant (:variant-id component) name))
          (rx/of (dwl/rename-component-and-main-instance component-id name)))))))