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

Code before:
  (ptk/reify ::unapply-token
    ptk/WatchEvent
    (watch [_ _ _]
      (rx/of
       (let [remove-token #(when % (cft/remove-attributes-for-token attributes token %))]
         (dwsh/update-shapes
          shape-ids
          (fn [shape]
            (update shape :applied-tokens remove-token))))))))

(defn toggle-token
  [{:keys [token attrs shape-ids expanded]}]
  (ptk/reify ::on-toggle-token
    ptk/WatchEvent
    (watch [_ state _]
      (let [objects (dsh/lookup-page-objects state)
            shapes (into [] (keep (d/getf objects)) shape-ids)
            shapes
            (if expanded
              (if (= 1 (count shapes))
                (let [shape (first shapes)]
                  (if (= (:type shape) :group)
                    (keep objects (:shapes shape))
                    [shape]))

                (mapcat (fn [shape]
                          (if (= (:type shape) :group)
                            (keep objects (:shapes shape))
                            [shape]))
                        shapes))
              shapes)
            {:keys [attributes all-attributes on-update-shape]}
            (get token-properties (:type token))
            unapply-tokens?
            (cft/shapes-token-applied? token shapes (or attrs all-attributes attributes))
            shape-ids (map :id shapes)]
        (if unapply-tokens?
          (rx/of
           (unapply-token {:attributes (or attrs all-attributes attributes)
                           :token token
                           :shape-ids shape-ids}))
          (rx/of
           (case (:type token)
             :spacing
             (apply-spacing-token {:token token
                                   :attr attrs
                                   :shapes shapes})
             (apply-token {:attributes (or attrs attributes)
                           :token token
                           :shape-ids shape-ids
                           :on-update-shape on-update-shape}))))))))

Review comment (suggestion):
I think we can simply this using `mapcat` always.

I suggest replace all this if with:

```clojure
(into []
      (mapcat (fn [shape]
                (if (= (:type shape) :group)
                  (keep objects (:shapes shape))
                  [shape])))
      shapes)
```

Code after:
  (ptk/reify ::unapply-token
    ptk/WatchEvent
    (watch [_ _ _]
      (rx/of
       (let [remove-token #(when % (cft/remove-attributes-for-token attributes token %))]
         (dwsh/update-shapes
          shape-ids
          (fn [shape]
            (update shape :applied-tokens remove-token))))))))

(defn toggle-token
  [{:keys [token attrs shape-ids expand-with-children]}]
  (ptk/reify ::on-toggle-token
    ptk/WatchEvent
    (watch [_ state _]
      (let [objects (dsh/lookup-page-objects state)
            shapes (into [] (keep (d/getf objects)) shape-ids)
            shapes
            (if expand-with-children
              (into []
                    (mapcat (fn [shape]
                              (if (= (:type shape) :group)
                                (keep objects (:shapes shape))
                                [shape])))
                    shapes)
              shapes)
            {:keys [attributes all-attributes on-update-shape]}
            (get token-properties (:type token))
            unapply-tokens?
            (cft/shapes-token-applied? token shapes (or attrs all-attributes attributes))
            shape-ids (map :id shapes)]
        (if unapply-tokens?
          (rx/of
           (unapply-token {:attributes (or attrs all-attributes attributes)
                           :token token
                           :shape-ids shape-ids}))
          (rx/of
           (case (:type token)
             :spacing
             (apply-spacing-token {:token token
                                   :attr attrs
                                   :shapes shapes})
             (apply-token {:attributes (or attrs attributes)
                           :token token
                           :shape-ids shape-ids
                           :on-update-shape on-update-shape}))))))))


(defn apply-token-on-selected
  [color-operations token]
  (ptk/reify ::apply-token-on-selected