From 60761eec07942a4e057d177331de02782edf12f9 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Tue, 29 Oct 2024 17:06:32 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20renaming=20token=20to=20ot?= =?UTF-8?q?her=20namespace=20not=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/src/app/common/types/tokens_lib.cljc | 7 +-- .../app/main/ui/workspace/tokens/form.cljs | 45 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index 3a966bf68..aac99c0e9 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -147,9 +147,10 @@ Token references are strings delimited by curly braces. E.g.: {foo.bar.baz} -> foo.bar.baz" [token-value] - (some->> (re-seq #"\{([^}]*)\}" token-value) - (map second) - (into #{}))) + (when (string? token-value) + (some->> (re-seq #"\{([^}]*)\}" token-value) + (map second) + (into #{})))) (defn token-value-self-reference? "Check if the token is self referencing with its `token-name` in `token-value`. diff --git a/frontend/src/app/main/ui/workspace/tokens/form.cljs b/frontend/src/app/main/ui/workspace/tokens/form.cljs index 4ccd0f142..cb29a169c 100644 --- a/frontend/src/app/main/ui/workspace/tokens/form.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/form.cljs @@ -112,16 +112,20 @@ Token names should only contain letters and digits separated by . characters.")} (p/rejected {:errors [(wte/get-error-code :error.token/direct-self-reference)]}) :else - (-> (update tokens token-name merge {:value value - :name token-name - :type (:type token)}) - (sd/resolve-tokens+) - (p/then - (fn [resolved-tokens] - (let [{:keys [errors resolved-value] :as resolved-token} (get resolved-tokens token-name)] - (cond - resolved-value (p/resolved resolved-token) - :else (p/rejected {:errors (or errors (wte/get-error-code :error/unknown-error))}))))))))) + (let [tokens' (cond-> tokens + ;; Remove previous token when renaming a token + (not= name-value (:name token)) (dissoc (:name token)) + :always (update token-name #(ctob/make-token (merge % {:value value + :name token-name + :type (:type token)}))))] + (-> tokens' + (sd/resolve-tokens-interactive+) + (p/then + (fn [resolved-tokens] + (let [{:keys [errors resolved-value] :as resolved-token} (get resolved-tokens token-name)] + (cond + resolved-value (p/resolved resolved-token) + :else (p/rejected {:errors (or errors (wte/get-error-code :error/unknown-error))})))))))))) (defn use-debonced-resolve-callback "Resolves a token values using `StyleDictionary`. @@ -200,8 +204,7 @@ Token names should only contain letters and digits separated by . characters.")} (mf/defc form {::mf/wrap-props false} [{:keys [token token-type action selected-token-set-id]}] - (let [validate-name? (mf/use-state (not (:id token))) - token (or token {:type token-type}) + (let [token (or token {:type token-type}) color? (wtt/color-token? token) selected-set-tokens (mf/deref refs/workspace-selected-token-set-tokens) active-theme-tokens (mf/deref refs/workspace-active-theme-sets-tokens) @@ -218,6 +221,7 @@ Token names should only contain letters and digits separated by . characters.")} (d/dissoc-in token-path)))) ;; Name + touched-name? (mf/use-state false) name-ref (mf/use-var (:name token)) name-errors (mf/use-state nil) validate-name @@ -233,15 +237,14 @@ Token names should only contain letters and digits separated by . characters.")} (debounce (fn [e] (let [value (dom/get-target-val e) errors (validate-name value)] - ;; Prevent showing error when just going to another field on a new token - (when-not (and validate-name? (str/empty? value)) - (reset! validate-name? false) + (when touched-name? (reset! name-errors errors)))))) on-update-name (mf/use-fn (mf/deps on-update-name-debounced) (fn [e] + (reset! touched-name? true) (reset! name-ref (dom/get-target-val e)) (on-update-name-debounced e))) @@ -310,10 +313,10 @@ Token names should only contain letters and digits separated by . characters.")} (mf/deps validate-name validate-descripion token resolved-tokens) (fn [e] (dom/prevent-default e) - ;; We have to re-validate the current form values before submitting - ;; because the validation is asynchronous/debounced - ;; and the user might have edited a valid form to make it invalid, - ;; and press enter before the next validations could return. + ;; We have to re-validate the current form values before submitting + ;; because the validation is asynchronous/debounced + ;; and the user might have edited a valid form to make it invalid, + ;; and press enter before the next validations could return. (let [final-name (finalize-name @name-ref) valid-name?+ (-> (validate-name final-name) schema-validation->promise) final-value (finalize-value @value-ref) @@ -326,8 +329,8 @@ Token names should only contain letters and digits separated by . characters.")} :token token :tokens resolved-tokens})]) (p/finally (fn [result err] - ;; The result should be a vector of all resolved validations - ;; We do not handle the error case as it will be handled by the components validations + ;; The result should be a vector of all resolved validations + ;; We do not handle the error case as it will be handled by the components validations (when (and (seq result) (not err)) (st/emit! (dt/update-create-token {:token (ctob/make-token :name final-name :type (or (:type token) token-type)