-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
options.cljs
33 lines (27 loc) · 1.07 KB
/
options.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns portal.ui.options
(:require ["react" :as react]
[clojure.edn :as edn]
[portal.ui.state :as state]
[reagent.core :as r]))
(defn- get-extension-options []
(when-let [options (.getItem js/sessionStorage "PORTAL_EXTENSION_OPTIONS")]
(edn/read-string options)))
(defonce ^:private extension-options (r/atom (get-extension-options)))
(defn ^:export ^:no-doc patch
"Function for extensions to patch options after init."
[edn-string]
(reset! extension-options (edn/read-string edn-string)))
(defonce ^:private options-context (react/createContext nil))
(defn with-options [& children]
(let [[options set-options!] (react/useState ::loading)]
(react/useEffect
(fn []
(-> (state/invoke `portal.runtime/get-options)
(.then set-options!)))
#js [])
(into [:r> (.-Provider options-context)
#js {:value (if (= options ::loading)
options
(merge options @extension-options))}]
children)))
(defn use-options [] (react/useContext options-context))