🎉 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)
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
(into (d/ordered-set)
["legacy-2"
@ -1291,4 +1305,5 @@
"legacy-65"
"legacy-66"
"legacy-67"
"Add hidden theme"]))
"Add hidden theme"
"Add token theme id"]))

View file

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

View file

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

View file

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

View file

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