♻️ Refactor token set auto selection mechanism

This is a general purpose change that also allows perform
a best effort on selection sets when the name is changed
(per example by moving it into other group).
This commit is contained in:
Andrey Antukh 2025-02-06 13:27:06 +01:00
parent 5446464d7e
commit 4a4cd9492a
6 changed files with 82 additions and 106 deletions

View file

@ -108,6 +108,8 @@
(dch/commit-changes changes)
(wtu/update-workspace-tokens))))))
(declare set-selected-token-set-name)
(defn create-token-set [set-name token-set]
(let [new-token-set (-> token-set
(update :name #(if (empty? %) set-name (ctob/join-set-path [% set-name]))))]
@ -117,7 +119,7 @@
(let [changes (-> (pcb/empty-changes it)
(pcb/add-token-set new-token-set))]
(rx/of
(dwts/set-selected-token-set-name (:name new-token-set))
(set-selected-token-set-name (:name new-token-set))
(dch/commit-changes changes)))))))
(defn rename-token-set-group [set-group-path set-group-fname]
@ -138,7 +140,7 @@
changes (-> (pcb/empty-changes it)
(pcb/update-token-set token-set prev-token-set))]
(rx/of
(dwts/set-selected-token-set-name (:name token-set))
(set-selected-token-set-name (:name token-set))
(dch/commit-changes changes))))))
(defn toggle-token-set [{:keys [token-set-name]}]
@ -164,18 +166,11 @@
ptk/WatchEvent
(watch [it state _]
(let [data (dsh/lookup-file-data state)
update-token-set-change (some-> lib
(ctob/get-sets)
(first)
(:name)
(dwts/set-selected-token-set-name))
changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-tokens-lib lib))]
(rx/of
(dch/commit-changes changes)
update-token-set-change
(wtu/update-workspace-tokens))))))
(rx/of (dch/commit-changes changes)
(wtu/update-workspace-tokens))))))
(defn delete-token-set-path [group? path]
(ptk/reify ::delete-token-set-path
@ -221,20 +216,13 @@
(ptk/reify ::drop-token-set
ptk/WatchEvent
(watch [it state _]
(let [undo-id (js/Symbol)]
(try
(when-let [changes (clt/generate-move-token-set (pcb/empty-changes it) (get-tokens-lib state) drop-opts)]
(rx/of
(dwu/start-undo-transaction undo-id)
(dch/commit-changes changes)
(some-> (get-in changes [:redo-changes 0 :to-path])
(ctob/join-set-path)
(dwts/set-selected-token-set-name))
(wtu/update-workspace-tokens)
(dwu/commit-undo-transaction undo-id)))
(catch js/Error e
(rx/of
(drop-error (ex-data e)))))))))
(try
(when-let [changes (clt/generate-move-token-set (pcb/empty-changes it) (get-tokens-lib state) drop-opts)]
(rx/of (dch/commit-changes changes)
(wtu/update-workspace-tokens)))
(catch js/Error e
(rx/of
(drop-error (ex-data e))))))))
(defn update-create-token
[{:keys [token prev-token-name]}]
@ -267,7 +255,7 @@
(pcb/set-token set-name (or prev-token-name (:name token)) token)))]
(rx/of
(dwts/set-selected-token-set-name set-name)
(set-selected-token-set-name set-name)
(when-not prev-token-name
(ptk/event ::ev/event {::ev/name "create-tokens"}))
(dch/commit-changes changes))))))
@ -345,3 +333,11 @@
ptk/UpdateEvent
(update [_ state]
(assoc-in state [:workspace-local :token-set-context-menu] nil))))
(defn set-selected-token-set-name
[name]
(ptk/reify ::set-selected-token-set-name
ptk/UpdateEvent
(update [_ state]
(update state :workspace-local assoc :selected-token-set-name name))))

View file

@ -9,8 +9,7 @@
Will default to the first set."
(:require
[app.common.types.tokens-lib :as ctob]
[app.main.data.helpers :as dsh]
[potok.v2.core :as ptk]))
[app.main.data.helpers :as dsh]))
(defn get-selected-token-set-name [state]
(or (get-in state [:workspace-local :selected-token-set-name])
@ -33,10 +32,3 @@
(defn get-selected-token-set-tokens [state]
(some-> (get-selected-token-set state)
:tokens))
(defn set-selected-token-set-name
[name]
(ptk/reify ::set-selected-token-set-path-from-name
ptk/UpdateEvent
(update [_ state]
(update state :workspace-local assoc :selected-token-set-name name))))

View file

@ -12,7 +12,6 @@
[app.common.types.tokens-lib :as ctob]
[app.main.data.event :as ev]
[app.main.data.tokens :as wdt]
[app.main.data.workspace.tokens.selected-set :as dwts]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.ds.buttons.icon-button :refer [icon-button*]]
@ -34,7 +33,7 @@
(st/emit! (wdt/toggle-token-set-group group-path)))
(defn on-select-token-set-click [set-name]
(st/emit! (dwts/set-selected-token-set-name set-name)))
(st/emit! (wdt/set-selected-token-set-name set-name)))
(defn on-update-token-set [set-name token-set]
(st/emit! (wdt/update-token-set (:name token-set) (ctob/update-name token-set set-name))))
@ -449,7 +448,7 @@
[{:keys [tokens-lib selected-token-set-name]}]
(let [token-sets
(ctob/get-set-tree tokens-lib)
(some-> tokens-lib (ctob/get-set-tree))
token-set-selected?
(mf/use-fn

View file

@ -240,17 +240,17 @@
(mf/defc theme-sets-list*
{::mf/private true}
[{:keys [tokens-lib]}]
(let [token-sets
(ctob/get-sets tokens-lib)
(let [;; FIXME: This is an inneficient operation just for being
;; ability to check if there are some sets and lookup the
;; first one when no set is selected, should be REFACTORED; is
;; inneficient because instead of return the sets as-is (tree)
;; it firstly makes it a plain seq from tree.
token-sets
(some-> tokens-lib (ctob/get-sets))
selected-token-set-name
(mf/deref refs/selected-token-set-name)
selected-token-set-name
(if selected-token-set-name
selected-token-set-name
(-> token-sets first :name))
{:keys [new-path] :as ctx}
(sets-context/use-context)]
@ -309,15 +309,15 @@
selected-token-set-name
(mf/deref refs/selected-token-set-name)
selected-token-set
(when selected-token-set-name
(some-> tokens-lib (ctob/get-set selected-token-set-name)))
;; If we have not selected any set explicitly we just
;; select the first one from the list of sets
selected-token-set-tokens
(if selected-token-set-name
(-> (ctob/get-set tokens-lib selected-token-set-name)
(get :tokens))
(-> (ctob/get-sets tokens-lib)
(first)
(get :tokens)))
(when selected-token-set
(get selected-token-set :tokens))
tokens
(mf/with-memo [active-theme-tokens selected-token-set-tokens]
@ -340,6 +340,31 @@
(mf/with-memo [tokens-by-type]
(get-sorted-token-groups tokens-by-type))]
(mf/with-effect [tokens-lib selected-token-set-name]
(when tokens-lib
(if selected-token-set-name
;; WORKAROUND: because we don't have a stable reference (by
;; ID per example) to token sets, when a set is moved the
;; name/path of the set changes and now can point to not
;; existing object; on this cases we perform a best effort
;; search around all existing sets that matches the
;; name (and not the path) and select it if it is found
(when-not (ctob/get-set tokens-lib selected-token-set-name)
(let [selected-name (ctob/get-token-set-final-name selected-token-set-name)
match (->> (ctob/get-sets tokens-lib)
(map :name)
(filter (fn [name]
(let [path (ctob/split-token-set-name name)]
(= (peek path) selected-name))))
(first))]
(when match
(st/emit! (dt/set-selected-token-set-name match)))))
(let [match (->> (ctob/get-sets tokens-lib)
(first)
(:name))]
(st/emit! (dt/set-selected-token-set-name match))))))
[:*
[:& token-context-menu]
[:& title-bar {:all-clickable true

View file

@ -5,7 +5,6 @@
[app.common.logging :as l]
[app.common.transit :as t]
[app.common.types.tokens-lib :as ctob]
[app.main.refs :as refs]
[app.main.ui.workspace.tokens.errors :as wte]
[app.main.ui.workspace.tokens.tinycolor :as tinycolor]
[app.main.ui.workspace.tokens.token :as wtt]