Repository: penpot/penpot
File: frontend/src/app/main/ui/comments.cljs (ClojureScript)

Code before:
(defn- inside-vbox?
  "Checks if a bubble or a bubble group is inside a viewbox"
  [thread-group wl]
  (let [vbox      (:vbox wl)
        positions (mapv :position thread-group)
        position  (gpt/center-points positions)
        pos-x     (:x position)
        pos-y     (:y position)
        x1        (:x vbox)
        y1        (:y vbox)
        x2        (+ x1 (:width vbox))
        y2        (+ y1 (:height vbox))]
    (and (> x2 pos-x x1) (> y2 pos-y y1))))

(defn- calculate-zoom-scale
  "Calculates the zoom level needed to ungroup the largest number of bubbles while
   keeping them all visible in the viewbox."
  [position zoom threads wl]
  (let [num-threads         (count threads)
        grouped-threads     (group-bubbles zoom threads)
        num-grouped-threads (count grouped-threads)
        zoom-scale-step     1.75
        scaled-zoom         (* zoom zoom-scale-step)
        zoomed-wl           (dwz/impl-update-zoom wl position scaled-zoom)]
    (if (or (= num-threads num-grouped-threads)
            (some #((comp not inside-vbox?) % zoomed-wl) grouped-threads))
      zoom
      (calculate-zoom-scale position scaled-zoom threads zoomed-wl))))

(mf/defc comment-floating-group*
  {::mf/wrap [mf/memo]}
  [{:keys [thread-group zoom position-modifier]}]
  (let [positions   (mapv :position thread-group)

        position    (gpt/center-points positions)
        position    (cond-> position
                      (some? position-modifier)
                      (gpt/transform position-modifier))
        pos-x       (* (:x position) zoom)
        pos-y       (* (:y position) zoom)

        unread?     (some #(pos? (:count-unread-comments %)) thread-group)
        num-threads (str (count thread-group))

        test-id     (str/join "-" (map :seqn (sort-by :seqn thread-group)))

        wl          (mf/deref refs/workspace-local)

        on-click
        (mf/use-fn
         (mf/deps thread-group position zoom wl)

Review comment (suggestion):
Consider define `(comp not inside-vbox?)` in a let binding and reuse, because with this code you are creating a new function on each iteration.

Also, this also can be simplified to `(complement inside-vbox?)`

Code after:
(defn- inside-vbox?
  "Checks if a bubble or a bubble group is inside a viewbox"
  [thread-group wl]
  (let [vbox      (:vbox wl)
        positions (mapv :position thread-group)
        position  (gpt/center-points positions)
        pos-x     (:x position)
        pos-y     (:y position)
        x1        (:x vbox)
        y1        (:y vbox)
        x2        (+ x1 (:width vbox))
        y2        (+ y1 (:height vbox))]
    (and (> x2 pos-x x1) (> y2 pos-y y1))))

(defn- calculate-zoom-scale
  "Calculates the zoom level needed to ungroup the largest number of bubbles while
   keeping them all visible in the viewbox."
  [position zoom threads wl]
  (let [num-threads         (count threads)
        grouped-threads     (group-bubbles zoom threads)
        num-grouped-threads (count grouped-threads)
        zoom-scale-step     1.75
        scaled-zoom         (* zoom zoom-scale-step)
        zoomed-wl           (dwz/impl-update-zoom wl position scaled-zoom)
        outside-vbox?       (complement inside-vbox?)]
    (if (or (= num-threads num-grouped-threads)
            (some #(outside-vbox? % zoomed-wl) grouped-threads))
      zoom
      (calculate-zoom-scale position scaled-zoom threads zoomed-wl))))

(mf/defc comment-floating-group*
  {::mf/wrap [mf/memo]}
  [{:keys [thread-group zoom position-modifier]}]
  (let [positions   (mapv :position thread-group)

        position    (gpt/center-points positions)
        position    (cond-> position
                      (some? position-modifier)
                      (gpt/transform position-modifier))
        pos-x       (* (:x position) zoom)
        pos-y       (* (:y position) zoom)

        unread?     (some #(pos? (:count-unread-comments %)) thread-group)
        num-threads (str (count thread-group))

        test-id     (str/join "-" (map :seqn (sort-by :seqn thread-group)))

        on-click
        (mf/use-fn
         (mf/deps thread-group position zoom)
         (fn []