mirror of
https://github.com/penpot/penpot.git
synced 2025-02-01 11:59:17 -05:00
Give new tokens without a name a temporary hardcoded string
This commit is contained in:
parent
1dcdddb2db
commit
dd62c7fe18
1 changed files with 34 additions and 17 deletions
|
@ -119,15 +119,18 @@
|
||||||
|
|
||||||
;; Hooks -----------------------------------------------------------------------
|
;; Hooks -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
(def new-token-temp-name
|
||||||
|
"TOKEN_STUDIO_SYSTEM.TEMP")
|
||||||
|
|
||||||
(defn use-debonced-resolve-callback
|
(defn use-debonced-resolve-callback
|
||||||
[tokens on-success & {:keys [cached timeout]
|
[name-ref token tokens callback & {:keys [cached timeout]
|
||||||
:or {cached {}
|
:or {cached {}
|
||||||
timeout 500}}]
|
timeout 500}}]
|
||||||
(let [timeout-id-ref (mf/use-ref nil)
|
(let [timeout-id-ref (mf/use-ref nil)
|
||||||
cache (mf/use-ref cached)
|
cache (mf/use-ref cached)
|
||||||
debounced-resolver-callback
|
debounced-resolver-callback
|
||||||
(mf/use-callback
|
(mf/use-callback
|
||||||
(mf/deps on-success tokens)
|
(mf/deps token callback tokens)
|
||||||
(fn [event]
|
(fn [event]
|
||||||
(let [input (dom/get-target-val event)
|
(let [input (dom/get-target-val event)
|
||||||
timeout-id (js/Symbol)]
|
timeout-id (js/Symbol)]
|
||||||
|
@ -135,19 +138,33 @@
|
||||||
(js/setTimeout
|
(js/setTimeout
|
||||||
(fn []
|
(fn []
|
||||||
(when (= (mf/ref-val timeout-id-ref) timeout-id)
|
(when (= (mf/ref-val timeout-id-ref) timeout-id)
|
||||||
(if-let [cached (-> (mf/ref-val cache)
|
(let [cached (-> (mf/ref-val cache)
|
||||||
(get tokens))]
|
(get tokens))
|
||||||
(on-success cached)
|
token-name (if (empty? @name-ref) new-token-temp-name @name-ref)]
|
||||||
(let [token-id (random-uuid)
|
(cond
|
||||||
new-tokens (assoc tokens token-id {:id token-id
|
cached (callback cached)
|
||||||
:value input
|
(token-self-reference? token-name input) (callback :error/token-self-reference)
|
||||||
:name "TEMP"})]
|
:else (let [token-id (or (:id token) (random-uuid))
|
||||||
(-> (resolve-tokens+ new-tokens)
|
new-tokens (update tokens token-id merge {:id token-id
|
||||||
(p/catch js/console.error)
|
:value input
|
||||||
(p/then (fn [resolved-tokens]
|
:name token-name})]
|
||||||
(mf/set-ref-val! cache (assoc (mf/ref-val cache) input resolved-tokens))
|
(-> (resolve-tokens+ new-tokens)
|
||||||
(when (= (mf/ref-val timeout-id-ref) timeout-id)
|
(p/finally
|
||||||
(on-success (get resolved-tokens token-id))))))))))
|
(fn [resolved-tokens _err]
|
||||||
|
(js/console.log "input" input (empty? (str/trim input)))
|
||||||
|
(cond
|
||||||
|
;; Ignore outdated callbacks because the user input changed since it tried to resolve
|
||||||
|
(not= (mf/ref-val timeout-id-ref) timeout-id) nil
|
||||||
|
(empty? (str/trim input)) (callback nil)
|
||||||
|
:else (let [resolved-token (get resolved-tokens token-id)]
|
||||||
|
(js/console.log "resolved-token" resolved-token)
|
||||||
|
(if (:resolved-value resolved-token)
|
||||||
|
(do
|
||||||
|
(mf/set-ref-val! cache (assoc (mf/ref-val cache) input resolved-tokens))
|
||||||
|
(callback resolved-token))
|
||||||
|
(callback :error/token-missing-reference))))))))))))
|
||||||
|
|
||||||
|
|
||||||
timeout))))]
|
timeout))))]
|
||||||
debounced-resolver-callback))
|
debounced-resolver-callback))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue