Simplify access to can-edit? using react context

This commit is contained in:
Andrey Antukh 2025-02-06 15:02:44 +01:00
parent 4a4cd9492a
commit efe204c346
3 changed files with 15 additions and 14 deletions

View file

@ -133,13 +133,14 @@
(fn [] (fn []
(st/emit! (dtm/finalize-team team-id)))) (st/emit! (dtm/finalize-team team-id))))
(let [team (mf/deref refs/team)] (let [{:keys [permissions] :as team} (mf/deref refs/team)]
(when (= team-id (:id team)) (when (= team-id (:id team))
[:& (mf/provider ctx/current-team-id) {:value team-id} [:> (mf/provider ctx/current-team-id) {:value team-id}
[:& (mf/provider ctx/permissions) {:value (:permissions team)} [:> (mf/provider ctx/permissions) {:value permissions}
[:> (mf/provider ctx/can-edit?) {:value (:can-edit permissions)}
;; The `:key` is mandatory here because we want to reinitialize ;; The `:key` is mandatory here because we want to reinitialize
;; all dom tree instead of simple rerender. ;; all dom tree instead of simple rerender.
[:* {:key (str team-id)} children]]]))) [:* {:key (str team-id)} children]]]])))
(mf/defc page* (mf/defc page*
{::mf/props :obj {::mf/props :obj

View file

@ -34,3 +34,4 @@
(def sidebar (mf/create-context nil)) (def sidebar (mf/create-context nil))
(def permissions (mf/create-context nil)) (def permissions (mf/create-context nil))
(def can-edit? (mf/create-context nil))

View file

@ -9,6 +9,7 @@
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.json :as json]
[app.common.types.tokens-lib :as ctob] [app.common.types.tokens-lib :as ctob]
[app.main.data.event :as ev] [app.main.data.event :as ev]
[app.main.data.modal :as modal] [app.main.data.modal :as modal]
@ -72,6 +73,9 @@
(let [{:keys [modal title]} (let [{:keys [modal title]}
(get wtch/token-properties type) (get wtch/token-properties type)
can-edit?
(mf/use-ctx ctx/can-edit?)
tokens tokens
(mf/with-memo [tokens] (mf/with-memo [tokens]
(vec (sort-by :name tokens))) (vec (sort-by :name tokens)))
@ -115,14 +119,12 @@
(dom/stop-propagation event) (dom/stop-propagation event)
(when (seq selected-shapes) (when (seq selected-shapes)
(st/emit! (wtch/toggle-token {:token token (st/emit! (wtch/toggle-token {:token token
:shapes selected-shapes}))))) :shapes selected-shapes})))))]
tokens-count (count tokens)
can-edit? (:can-edit (deref refs/permissions))]
[:div {:on-click on-toggle-open-click} [:div {:on-click on-toggle-open-click}
[:& cmm/asset-section {:icon (token-section-icon type) [:& cmm/asset-section {:icon (token-section-icon type)
:title title :title title
:assets-count tokens-count :assets-count (count tokens)
:open? is-open} :open? is-open}
[:& cmm/asset-section-block {:role :title-button} [:& cmm/asset-section-block {:role :title-button}
(when can-edit? (when can-edit?
@ -168,11 +170,8 @@
(let [ordered-themes (let [ordered-themes
(mf/deref refs/workspace-token-themes-no-hidden) (mf/deref refs/workspace-token-themes-no-hidden)
permissions
(mf/use-ctx ctx/permissions)
can-edit? can-edit?
(get permissions :can-edit) (mf/use-ctx ctx/can-edit?)
open-modal open-modal
(mf/use-fn (mf/use-fn