🎉 Add id to token theme (#6044)

* 🎉 Add id to token theme

* 📎 Fix tests

* 📎 Fixes from review
This commit is contained in:
Eva Marco 2025-03-10 18:38:52 +01:00 committed by GitHub
parent 46c89a1bcf
commit 0c3fd8a6d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 35 additions and 4 deletions

View file

@ -1237,6 +1237,20 @@
(update data :tokens-lib update-tokens-lib) (update data :tokens-lib update-tokens-lib)
data))) data)))
(defmethod migrate-data "Add token theme id"
[data _]
(letfn [(update-tokens-lib [tokens-lib]
(let [themes (ctob/get-themes tokens-lib)]
(reduce (fn [lib theme]
(if (:id theme)
lib
(ctob/update-theme lib (:group theme) (:name theme) #(assoc % :id (str (uuid/next))))))
tokens-lib
themes)))]
(if (contains? data :tokens-lib)
(update data :tokens-lib update-tokens-lib)
data)))
(def available-migrations (def available-migrations
(into (d/ordered-set) (into (d/ordered-set)
["legacy-2" ["legacy-2"
@ -1291,4 +1305,5 @@
"legacy-65" "legacy-65"
"legacy-66" "legacy-66"
"legacy-67" "legacy-67"
"Add hidden theme"])) "Add hidden theme"
"Add token theme id"]))

View file

@ -15,6 +15,7 @@
[:group :string] [:group :string]
[:description [:maybe :string]] [:description [:maybe :string]]
[:is-source :boolean] [:is-source :boolean]
[:id :string]
[:modified-at {:optional true} ::sm/inst] [:modified-at {:optional true} ::sm/inst]
[:sets :any]]) [:sets :any]])

View file

@ -13,6 +13,7 @@
[app.common.time :as dt] [app.common.time :as dt]
[app.common.transit :as t] [app.common.transit :as t]
[app.common.types.token :as cto] [app.common.types.token :as cto]
[app.common.uuid :as uuid]
[clojure.set :as set] [clojure.set :as set]
[clojure.walk :as walk] [clojure.walk :as walk]
[cuerdas.core :as str])) [cuerdas.core :as str]))
@ -496,13 +497,14 @@
(theme-matches-group-name [_ group name] "if a theme matches the given group & name") (theme-matches-group-name [_ group name] "if a theme matches the given group & name")
(hidden-temporary-theme? [_] "if a theme is the (from the user ui) hidden temporary theme")) (hidden-temporary-theme? [_] "if a theme is the (from the user ui) hidden temporary theme"))
(defrecord TokenTheme [name group description is-source modified-at sets] (defrecord TokenTheme [name group description is-source id modified-at sets]
ITokenTheme ITokenTheme
(set-sets [_ set-names] (set-sets [_ set-names]
(TokenTheme. name (TokenTheme. name
group group
description description
is-source is-source
id
(dt/now) (dt/now)
set-names)) set-names))
@ -529,6 +531,7 @@
group group
description description
is-source is-source
id
(dt/now) (dt/now)
(conj (disj sets prev-set-name) set-name)) (conj (disj sets prev-set-name) set-name))
this)) this))
@ -553,6 +556,7 @@
[:group :string] [:group :string]
[:description [:maybe :string]] [:description [:maybe :string]]
[:is-source [:maybe :boolean]] [:is-source [:maybe :boolean]]
[:id :string]
[:modified-at ::sm/inst] [:modified-at ::sm/inst]
[:sets [:set {:gen/max 5} :string]]]) [:sets [:set {:gen/max 5} :string]]])
@ -583,10 +587,11 @@
[& {:as attrs}] [& {:as attrs}]
(-> attrs (-> attrs
(update :group d/nilv top-level-theme-group-name) (update :group d/nilv top-level-theme-group-name)
(update :description d/nilv "")
(update :is-source d/nilv false) (update :is-source d/nilv false)
(update :id d/nilv (str (uuid/next)))
(update :modified-at #(or % (dt/now))) (update :modified-at #(or % (dt/now)))
(update :sets set) (update :sets set)
(update :description d/nilv "")
(check-token-theme-attrs) (check-token-theme-attrs)
(map->TokenTheme))) (map->TokenTheme)))
@ -595,6 +600,7 @@
(-> attrs (-> attrs
(assoc :group hidden-token-theme-group) (assoc :group hidden-token-theme-group)
(assoc :name hidden-token-theme-name) (assoc :name hidden-token-theme-name)
(assoc :id (str uuid/zero))
(make-token-theme))) (make-token-theme)))
;; === TokenThemes (collection) ;; === TokenThemes (collection)
@ -1269,11 +1275,12 @@ Will return a value that matches this schema:
(if-let [themes-data (seq themes-data)] (if-let [themes-data (seq themes-data)]
(as-> lib' $ (as-> lib' $
(reduce (reduce
(fn [lib {:strs [name group description is-source modified-at sets]}] (fn [lib {:strs [name group description is-source id modified-at sets]}]
(add-theme lib (TokenTheme. name (add-theme lib (TokenTheme. name
(or group "") (or group "")
description description
(some? is-source) (some? is-source)
(or id (str (uuid/next)))
(or (some-> modified-at (or (some-> modified-at
(dt/parse-instant)) (dt/parse-instant))
(dt/now)) (dt/now))

View file

@ -1257,6 +1257,7 @@
:value "{accent.default}"})})) :value "{accent.default}"})}))
(ctob/add-theme (ctob/make-token-theme :name "theme-1" (ctob/add-theme (ctob/make-token-theme :name "theme-1"
:group "group-1" :group "group-1"
:id "test-id-00"
:modified-at now :modified-at now
:sets #{"core"}))) :sets #{"core"})))
result (ctob/encode-dtcg tokens-lib) result (ctob/encode-dtcg tokens-lib)
@ -1264,6 +1265,7 @@
"group" "group-1" "group" "group-1"
"is-source" false "is-source" false
"modified-at" now "modified-at" now
"id" "test-id-00"
"name" "theme-1" "name" "theme-1"
"selectedTokenSets" {"core" "enabled"}}] "selectedTokenSets" {"core" "enabled"}}]
"$metadata" {"tokenSetOrder" ["core"] "$metadata" {"tokenSetOrder" ["core"]
@ -1304,6 +1306,7 @@
{:name "button.primary.background" {:name "button.primary.background"
:type :color :type :color
:value "{accent.default}"})}))) :value "{accent.default}"})})))
encoded (ctob/encode-dtcg tokens-lib) encoded (ctob/encode-dtcg tokens-lib)
with-prev-tokens-lib (ctob/decode-dtcg-json tokens-lib encoded) with-prev-tokens-lib (ctob/decode-dtcg-json tokens-lib encoded)
with-empty-tokens-lib (ctob/decode-dtcg-json (ctob/ensure-tokens-lib nil) encoded)] with-empty-tokens-lib (ctob/decode-dtcg-json (ctob/ensure-tokens-lib nil) encoded)]
@ -1374,6 +1377,7 @@
:value "{accent.default}"})})) :value "{accent.default}"})}))
(ctob/add-theme (ctob/make-token-theme :name "theme-1" (ctob/add-theme (ctob/make-token-theme :name "theme-1"
:group "group-1" :group "group-1"
:id "test-id-01"
:modified-at now :modified-at now
:sets #{"core"})) :sets #{"core"}))
(ctob/toggle-theme-active? "group-1" "theme-1")) (ctob/toggle-theme-active? "group-1" "theme-1"))
@ -1382,6 +1386,7 @@
"group" "group-1" "group" "group-1"
"is-source" false "is-source" false
"modified-at" now "modified-at" now
"id" "test-id-01"
"name" "theme-1" "name" "theme-1"
"selectedTokenSets" {"core" "enabled"}}] "selectedTokenSets" {"core" "enabled"}}]
"$metadata" {"tokenSetOrder" ["core"] "$metadata" {"tokenSetOrder" ["core"]

View file

@ -1748,6 +1748,7 @@
"~:group": "Core", "~:group": "Core",
"~:description": null, "~:description": null,
"~:is-source": false, "~:is-source": false,
"~:id": "core-light",
"~:modified-at": "~m1737542746842", "~:modified-at": "~m1737542746842",
"~:sets": { "~:sets": {
"~#set": ["LightDark/light", "theme", "core"] "~#set": ["LightDark/light", "theme", "core"]
@ -1763,6 +1764,7 @@
"~:group": "Core", "~:group": "Core",
"~:description": null, "~:description": null,
"~:is-source": false, "~:is-source": false,
"~:id": "core-dark",
"~:modified-at": "~m1737542746842", "~:modified-at": "~m1737542746842",
"~:sets": { "~:sets": {
"~#set": ["LightDark/dark", "theme", "core"] "~#set": ["LightDark/dark", "theme", "core"]
@ -1785,6 +1787,7 @@
"~:group": "", "~:group": "",
"~:description": null, "~:description": null,
"~:is-source": false, "~:is-source": false,
"~:id": "hidden-theme",
"~:modified-at": "~m1737542683555", "~:modified-at": "~m1737542683555",
"~:sets": { "~:sets": {
"~#set": ["LightDark/light", "theme", "core"] "~#set": ["LightDark/light", "theme", "core"]