Browse Source

replace derived-atom with flow

Tienson Qin 8 months ago
parent
commit
60bcefb979

+ 228 - 206
src/main/frontend/components/block.cljs

@@ -10,7 +10,6 @@
             [datascript.impl.entity :as e]
             [dommy.core :as dom]
             [electron.ipc :as ipc]
-            [frontend.common.missionary :as c.m]
             [frontend.components.block.macros :as block-macros]
             [frontend.components.file-based.block :as file-block]
             [frontend.components.icon :as icon-component]
@@ -90,7 +89,6 @@
             [logseq.shui.dialog.core :as shui-dialog]
             [logseq.shui.ui :as shui]
             [medley.core :as medley]
-            [missionary.core :as m]
             [promesa.core :as p]
             [reitit.frontend.easy :as rfe]
             [rum.core :as rum]
@@ -1131,63 +1129,73 @@
 
 (declare block-container)
 
-(rum/defc block-embed < rum/reactive
-  {:init (fn [state]
-           (let [block-id (second (:rum/args state))]
-             (db-async/<get-block (state/get-current-repo) block-id))
-           state)}
-  [config uuid]
-  (if (state/sub-async-query-loading (str uuid))
-    [:span "Loading..."]
-    (when-let [block (db/entity [:block/uuid uuid])]
+(rum/defc block-embed
+  [config block-uuid]
+  (let [[block set-block!] (hooks/use-state (db/entity [:block/uuid block-uuid]))]
+    (hooks/use-effect!
+     (fn []
+       (p/let [block (db-async/<get-block (state/get-current-repo)
+                                          block-uuid
+                                          {:children? false
+                                           :skip-refresh? true})]
+         (set-block! block)))
+     [])
+    (when block
       [:div.color-level.embed-block.bg-base-2
        {:style {:z-index 2}
         :on-pointer-down (fn [e] (.stopPropagation e))}
        [:div.px-3.pt-1.pb-2
         (let [config' (assoc config
                              :db/id (:db/id block)
-                             :id (str uuid)
-                             :embed-id uuid
+                             :id (str block-uuid)
+                             :embed-id block-uuid
                              :embed? true
                              :embed-parent (:block config)
                              :ref? false)]
-          (blocks-container config' [block]))]])))
+          (block-container config' block))]])))
 
-(rum/defc page-embed < rum/reactive db-mixins/query
-  {:init (fn [state]
-           (let [page-name (second (:rum/args state))
-                 page-name' (util/page-name-sanity-lc (string/trim page-name))]
-             (db-async/<get-block (state/get-current-repo) page-name'))
-           state)}
+(rum/defc page-embed-aux < rum/reactive db-mixins/query
+  [config block]
+  (let [current-page (state/get-current-page)
+        block (db/sub-block (:db/id block))
+        whiteboard-page? (model/whiteboard-page? block)
+        page-name (:block/name block)]
+    [:div.color-level.embed.embed-page.bg-base-2
+     {:class (when (:sidebar? config) "in-sidebar")
+      :on-pointer-down #(.stopPropagation %)}
+     [:section.flex.items-center.p-1.embed-header
+      [:div.mr-3 svg/page]
+      (page-cp config block)]
+     (when (and
+            (not= (util/page-name-sanity-lc (or current-page ""))
+                  page-name)
+            (not= (util/page-name-sanity-lc (get config :id ""))
+                  page-name))
+       (if whiteboard-page?
+         ((state/get-component :whiteboard/tldraw-preview) (:block/uuid block))
+         (let [blocks (ldb/get-children block)
+               config' (assoc config
+                              :db/id (:db/id block)
+                              :id page-name
+                              :embed? true
+                              :page-embed? true
+                              :ref? false)]
+           (blocks-container config' blocks))))]))
+
+(rum/defc page-embed
   [config page-name]
-  (let [page-name (util/page-name-sanity-lc (string/trim page-name))
-        current-page (state/get-current-page)]
-    (if (and page-name (state/sub-async-query-loading page-name))
-      (ui/loading "embed")
-      (let [block (model/get-page page-name)
-            block (db/sub-block (:db/id block))
-            whiteboard-page? (model/whiteboard-page? block)]
-        [:div.color-level.embed.embed-page.bg-base-2
-         {:class (when (:sidebar? config) "in-sidebar")
-          :on-pointer-down #(.stopPropagation %)}
-         [:section.flex.items-center.p-1.embed-header
-          [:div.mr-3 svg/page]
-          (page-cp config block)]
-         (when (and
-                (not= (util/page-name-sanity-lc (or current-page ""))
-                      page-name)
-                (not= (util/page-name-sanity-lc (get config :id ""))
-                      page-name))
-           (if whiteboard-page?
-             ((state/get-component :whiteboard/tldraw-preview) (:block/uuid block))
-             (let [blocks (ldb/get-children block)
-                   config' (assoc config
-                                  :db/id (:db/id block)
-                                  :id page-name
-                                  :embed? true
-                                  :page-embed? true
-                                  :ref? false)]
-               (blocks-container config' blocks))))]))))
+  (let [page-name (util/page-name-sanity-lc (string/trim page-name))]
+    (let [[block set-block!] (hooks/use-state nil)]
+      (hooks/use-effect!
+       (fn []
+         (p/let [block (db-async/<get-block (state/get-current-repo)
+                                            page-name
+                                            {:children? true
+                                             :skip-refresh? true})]
+           (set-block! block)))
+       [])
+      (when block
+        (page-embed-aux config block)))))
 
 (defn- get-label-text
   [label]
@@ -1237,85 +1245,93 @@
                          :*timer *timer :*timer1 *timer1
                          :render render})))
 
-(rum/defc block-reference < rum/reactive db-mixins/query
-  {:init (fn [state]
-           (let [block-id (second (:rum/args state))]
-             (db-async/<get-block (state/get-current-repo) block-id :children? false))
-           state)}
-  [config id label]
-  (if (and id (= (:block/uuid (:block config)) id))
-    [:span.warning.text-sm "Self reference"]
-    (if-let [block-id (and id (if (uuid? id) id (parse-uuid id)))]
-      (if (state/sub-async-query-loading (str block-id))
-        [:span "Loading..."]
-        (let [block (db/entity [:block/uuid block-id])
-              db-id (:db/id block)
-              block (when db-id (db/sub-block db-id))
-              block-type (keyword (pu/lookup block :logseq.property/ls-type))
-              hl-type (pu/lookup block :logseq.property.pdf/hl-type)
-              repo (state/get-current-repo)
-              stop-inner-events? (= block-type :whiteboard-shape)]
-          (if (and block (:block/title block))
-            (let [content-cp (block-content (assoc config :block-ref? true :stop-events? stop-inner-events?)
-                                            block nil (:block/uuid block)
-                                            (:slide? config))
-                  display-type (:logseq.property.node/display-type block)]
-              (if (and display-type (not (contains? #{:quote :math} display-type)))
-                content-cp
-                (let [title [:span.block-ref content-cp]
-                      inner (cond
-                              (seq label)
-                              (->elem
-                               :span.block-ref
-                               (map-inline config label))
-                              :else
-                              title)]
-                  [:div.block-ref-wrap.inline
-                   {:data-type    (name (or block-type :default))
-                    :data-hl-type hl-type
-                    :on-pointer-down
-                    (fn [^js/MouseEvent e]
-                      (if (util/right-click? e)
-                        (state/set-state! :block-ref/context {:block (:block config)
-                                                              :block-ref block-id})
-                        (when (and
-                               (or (gobj/get e "shiftKey")
-                                   (not (.. e -target (closest ".blank"))))
-                               (not (util/right-click? e)))
-                          (util/stop e)
-
-                          (cond
-                            (gobj/get e "shiftKey")
-                            (state/sidebar-add-block!
-                             (state/get-current-repo)
-                             (:db/id block)
-                             :block-ref)
-
-                            (and (util/meta-key? e) (whiteboard-handler/inside-portal? (.-target e)))
-                            (whiteboard-handler/add-new-block-portal-shape!
-                             (:block/uuid block)
-                             (whiteboard-handler/closest-shape (.-target e)))
-
-                            :else
-                            (match [block-type (util/electron?)]
+(rum/defc block-reference-aux < rum/reactive db-mixins/query
+  [config block-id label]
+  (let [block (db/entity [:block/uuid block-id])
+        db-id (:db/id block)
+        block (when db-id (db/sub-block db-id))
+        block-type (keyword (pu/lookup block :logseq.property/ls-type))
+        hl-type (pu/lookup block :logseq.property.pdf/hl-type)
+        repo (state/get-current-repo)
+        stop-inner-events? (= block-type :whiteboard-shape)]
+    (if (and block (:block/title block))
+      (let [content-cp (block-content (assoc config :block-ref? true :stop-events? stop-inner-events?)
+                                      block nil (:block/uuid block)
+                                      (:slide? config))
+            display-type (:logseq.property.node/display-type block)]
+        (if (and display-type (not (contains? #{:quote :math} display-type)))
+          content-cp
+          (let [title [:span.block-ref content-cp]
+                inner (cond
+                        (seq label)
+                        (->elem
+                         :span.block-ref
+                         (map-inline config label))
+                        :else
+                        title)]
+            [:div.block-ref-wrap.inline
+             {:data-type    (name (or block-type :default))
+              :data-hl-type hl-type
+              :on-pointer-down
+              (fn [^js/MouseEvent e]
+                (if (util/right-click? e)
+                  (state/set-state! :block-ref/context {:block (:block config)
+                                                        :block-ref block-id})
+                  (when (and
+                         (or (gobj/get e "shiftKey")
+                             (not (.. e -target (closest ".blank"))))
+                         (not (util/right-click? e)))
+                    (util/stop e)
+
+                    (cond
+                      (gobj/get e "shiftKey")
+                      (state/sidebar-add-block!
+                       (state/get-current-repo)
+                       (:db/id block)
+                       :block-ref)
+
+                      (and (util/meta-key? e) (whiteboard-handler/inside-portal? (.-target e)))
+                      (whiteboard-handler/add-new-block-portal-shape!
+                       (:block/uuid block)
+                       (whiteboard-handler/closest-shape (.-target e)))
+
+                      :else
+                      (match [block-type (util/electron?)]
                           ;; pdf annotation
-                              [:annotation true] (pdf-assets/open-block-ref! block)
+                        [:annotation true] (pdf-assets/open-block-ref! block)
 
-                              [:whiteboard-shape true] (route-handler/redirect-to-page!
-                                                        (get-in block [:block/page :block/uuid]) {:block-id block-id})
+                        [:whiteboard-shape true] (route-handler/redirect-to-page!
+                                                  (get-in block [:block/page :block/uuid]) {:block-id block-id})
 
                           ;; default open block page
-                              :else (route-handler/redirect-to-page! id))))))}
-
-                   (if (and (not (util/mobile?))
-                            (not (:preview? config))
-                            (not (shui-dialog/has-modal?))
-                            (nil? block-type))
-                     (block-reference-preview inner
-                                              {:repo repo :config config :id block-id})
-                     inner)])))
-            (invalid-node-ref id))))
-      (invalid-node-ref id))))
+                        :else (route-handler/redirect-to-page! block-id))))))}
+
+             (if (and (not (util/mobile?))
+                      (not (:preview? config))
+                      (not (shui-dialog/has-modal?))
+                      (nil? block-type))
+               (block-reference-preview inner
+                                        {:repo repo :config config :id block-id})
+               inner)])))
+      (invalid-node-ref block-id))))
+
+(rum/defc block-reference
+  [config id label]
+  (let [block-id (and id (if (uuid? id) id (parse-uuid id)))
+        [block set-block!] (hooks/use-state (db/entity [:block/uuid block-id]))]
+    (hooks/use-effect!
+     (fn []
+       (p/let [block (db-async/<get-block (state/get-current-repo)
+                                          block-id
+                                          {:children? false
+                                           :skip-refresh? true})]
+         (set-block! block)))
+     [])
+    (if (and id (= (:block/uuid (:block config)) id))
+      [:span.warning.text-sm "Self reference"]
+      (if block
+        (block-reference-aux config id label)
+        (invalid-node-ref id)))))
 
 (defn- render-macro
   [config name arguments macro-content format]
@@ -3112,81 +3128,88 @@
                             :class "opacity-50 mx-1"}))
 
 ;; "block-id - uuid of the target block of breadcrumb. page uuid is also acceptable"
-(rum/defc breadcrumb < rum/reactive
-  {:init (fn [state]
-           (let [args (:rum/args state)
-                 block-id (nth args 2)
-                 depth (:level-limit (last args))]
-             (p/let [id (:db/id (db/entity [:block/uuid block-id]))
-                     _block (db-async/<get-block (state/get-current-repo) block-id
-                                                 {:children? false})]
-               (when id (db-async/<get-block-parents (state/get-current-repo) id depth)))
-             state))}
+(rum/defc breadcrumb-aux < rum/reactive
   [config repo block-id {:keys [show-page? indent? end-separator? level-limit _navigating-block]
                          :or {show-page? true
                               level-limit 3}
                          :as opts}]
-  (when block-id
-    (let [_ (state/sub-async-query-loading (str block-id "-parents"))
-          from-property (when (and block-id (config/db-based-graph? repo))
-                          (:logseq.property/created-from-property (db/entity [:block/uuid block-id])))
-          parents (db/get-block-parents repo block-id {:depth (inc level-limit)})
-          parents (remove nil? (concat parents [from-property]))
-          page (or (db/get-block-page repo block-id) ;; only return for block uuid
-                   (model/query-block-by-uuid block-id)) ;; return page entity when received page uuid
-          page-name (:block/name page)
-          page-title (:block/title page)
-          show? (or (seq parents) show-page? page-name)
-          parents (if (= page-name (:block/name (first parents)))
-                    (rest parents)
-                    parents)
-          more? (> (count parents) level-limit)
-          parents (if more? (take-last level-limit parents) parents)
-          config (assoc config :breadcrumb? true)]
-      (when show?
-        (let [page-name-props (when show-page?
-                                [page
-                                 (page-cp (dissoc config :breadcrumb? true) page)
-                                 {:block/name (or page-title page-name)}])
-              parents-props (doall
-                             (for [{:block/keys [uuid name title] :as block} parents]
-                               (if name
-                                 [block (page-cp {} block)]
-                                 (let [result (block/parse-title-and-body
-                                               uuid
-                                               (get block :block/format :markdown)
-                                               (:block/pre-block? block)
-                                               title)
-                                       ast-body (:block.temp/ast-body result)
-                                       ast-title (:block.temp/ast-title result)
-                                       config (assoc config :block/uuid uuid)]
-                                   [block
-                                    (when ast-title
-                                      (if (seq ast-title)
-                                        (->elem :span.inline-wrap (map-inline config ast-title))
-                                        (->elem :div (markup-elements-cp config ast-body))))]))))
-              breadcrumbs (->> (into [] parents-props)
-                               (concat [page-name-props] (when more? [:more]))
-                               (filterv identity)
-                               (map (fn [x]
-                                      (if (and (vector? x) (second x))
-                                        (let [[block label] x]
-                                          (rum/with-key (breadcrumb-fragment config block label opts)
-                                            (str (:block/uuid block))))
-                                        [:span.opacity-70 {:key "dots"} "⋯"])))
-                               (interpose (rum/with-key (breadcrumb-separator) "icon")))]
-          (when (seq breadcrumbs)
-            [:div.breadcrumb.block-parents
-             {:class (when (seq breadcrumbs)
-                       (str (when-not (or (:search? config) (:list-view? config))
-                              " my-2")
-                            (when indent?
-                              " ml-4")))}
-             (when (and (false? (:top-level? config))
-                        (seq parents))
-               (breadcrumb-separator))
-             breadcrumbs
-             (when end-separator? (breadcrumb-separator))]))))))
+  (let [from-property (when (and block-id (config/db-based-graph? repo))
+                        (:logseq.property/created-from-property (db/entity [:block/uuid block-id])))
+        parents (db/get-block-parents repo block-id {:depth (inc level-limit)})
+        parents (remove nil? (concat parents [from-property]))
+        page (or (db/get-block-page repo block-id) ;; only return for block uuid
+                 (model/query-block-by-uuid block-id)) ;; return page entity when received page uuid
+        page-name (:block/name page)
+        page-title (:block/title page)
+        show? (or (seq parents) show-page? page-name)
+        parents (if (= page-name (:block/name (first parents)))
+                  (rest parents)
+                  parents)
+        more? (> (count parents) level-limit)
+        parents (if more? (take-last level-limit parents) parents)
+        config (assoc config :breadcrumb? true)]
+    (when show?
+      (let [page-name-props (when show-page?
+                              [page
+                               (page-cp (dissoc config :breadcrumb? true) page)
+                               {:block/name (or page-title page-name)}])
+            parents-props (doall
+                           (for [{:block/keys [uuid name title] :as block} parents]
+                             (if name
+                               [block (page-cp {} block)]
+                               (let [result (block/parse-title-and-body
+                                             uuid
+                                             (get block :block/format :markdown)
+                                             (:block/pre-block? block)
+                                             title)
+                                     ast-body (:block.temp/ast-body result)
+                                     ast-title (:block.temp/ast-title result)
+                                     config (assoc config :block/uuid uuid)]
+                                 [block
+                                  (when ast-title
+                                    (if (seq ast-title)
+                                      (->elem :span.inline-wrap (map-inline config ast-title))
+                                      (->elem :div (markup-elements-cp config ast-body))))]))))
+            breadcrumbs (->> (into [] parents-props)
+                             (concat [page-name-props] (when more? [:more]))
+                             (filterv identity)
+                             (map (fn [x]
+                                    (if (and (vector? x) (second x))
+                                      (let [[block label] x]
+                                        (rum/with-key (breadcrumb-fragment config block label opts)
+                                          (str (:block/uuid block))))
+                                      [:span.opacity-70 {:key "dots"} "⋯"])))
+                             (interpose (rum/with-key (breadcrumb-separator) "icon")))]
+        (when (seq breadcrumbs)
+          [:div.breadcrumb.block-parents
+           {:class (when (seq breadcrumbs)
+                     (str (when-not (or (:search? config) (:list-view? config))
+                            " my-2")
+                          (when indent?
+                            " ml-4")))}
+           (when (and (false? (:top-level? config))
+                      (seq parents))
+             (breadcrumb-separator))
+           breadcrumbs
+           (when end-separator? (breadcrumb-separator))])))))
+
+(rum/defc breadcrumb
+  [config repo block-id {:keys [_show-page? _indent? _end-separator? level-limit _navigating-block]
+                         :or {level-limit 3}
+                         :as opts}]
+  (let [[block set-block!] (hooks/use-state (db/entity [:block/uuid block-id]))]
+    (hooks/use-effect!
+     (fn []
+       (p/let [block (db-async/<get-block (state/get-current-repo)
+                                          block-id
+                                          {:children? false
+                                           :skip-refresh? true})
+               _ (when-let [id (:db/id block)]
+                   (db-async/<get-block-parents (state/get-current-repo) id level-limit))]
+         (set-block! block)))
+     [])
+    (when block
+      (breadcrumb-aux config repo block-id opts))))
 
 (defn- block-drag-over
   [event uuid top? block-id *move-to']
@@ -3709,21 +3732,20 @@
 
 (rum/defc block-container
   [config block* & {:as opts}]
-  (let [[block set-block!] (hooks/use-state block*)]
+  (let [[block set-block!] (hooks/use-state block*)
+        id (or (:db/id block*) (:block/uuid block*))]
     (when-not (or (:page-title? config)
                   (:view? config))
       (hooks/use-effect!
-       #(c.m/run-task*
-         (m/sp
-           (c.m/<?
-            (db-async/<get-block (state/get-current-repo)
-                                 (:db/id block*)
-                                 {:children? (not
-                                              (if-some [result (state/get-block-collapsed (:block/uuid block))]
-                                                result
-                                                (:block/collapsed? block)))
-                                  :skip-refresh? true}))
-           (set-block! (some-> (:db/id block*) db/entity))))
+       (fn []
+         (p/let [block (db-async/<get-block (state/get-current-repo)
+                                            id
+                                            {:children? (not
+                                                         (if-some [result (state/get-block-collapsed (:block/uuid block))]
+                                                           result
+                                                           (:block/collapsed? block)))
+                                             :skip-refresh? true})]
+           (set-block! block)))
        []))
     (when (or (:view? config) (:block/title block))
       (loaded-block-container config block opts))))

+ 0 - 1
src/main/frontend/components/imports.cljs

@@ -45,7 +45,6 @@
   []
   (notification/show! "Import finished!" :success)
   (shui/dialog-close! :import-indicator)
-  (state/clear-async-query-state!)
   (ui-handler/re-render-root!)
   (route-handler/redirect-to-home!)
   (js/setTimeout ui-handler/re-render-root! 500))

+ 28 - 19
src/main/frontend/components/reference.cljs

@@ -16,30 +16,39 @@
             [logseq.db.common.view :as db-view]
             [logseq.shui.ui :as shui]
             [missionary.core :as m]
+            [promesa.core :as p]
             [rum.core :as rum]))
 
 ;; TODO: merge both page and block linked refs
-(rum/defc block-linked-references < rum/reactive db-mixins/query
-  {:init (fn [state]
-           (when-let [e (db/entity [:block/uuid (first (:rum/args state))])]
-             (db-async/<get-block-refs (state/get-current-repo) (:db/id e)))
-           state)}
+(rum/defc block-linked-references-aux < rum/reactive db-mixins/query
+  [e]
+  (let [block-id (:block/uuid e)
+        ref-blocks (-> (db/get-referenced-blocks (:db/id e))
+                       db-utils/group-by-page)]
+    (when (> (count ref-blocks) 0)
+      (let [ref-hiccup (block/->hiccup ref-blocks
+                                       {:id (str block-id)
+                                        :ref? true
+                                        :breadcrumb-show? true
+                                        :group-by-page? true
+                                        :editor-box editor/box}
+                                       {})]
+        [:div.references-blocks
+         (content/content block-id
+                          {:hiccup ref-hiccup})]))))
+
+(rum/defc block-linked-references
   [block-id]
   (when-let [e (db/entity [:block/uuid block-id])]
-    (when-not (state/sub-async-query-loading (str (:db/id e) "-refs"))
-      (let [ref-blocks (-> (db/get-referenced-blocks (:db/id e))
-                           db-utils/group-by-page)]
-        (when (> (count ref-blocks) 0)
-          (let [ref-hiccup (block/->hiccup ref-blocks
-                                           {:id (str block-id)
-                                            :ref? true
-                                            :breadcrumb-show? true
-                                            :group-by-page? true
-                                            :editor-box editor/box}
-                                           {})]
-            [:div.references-blocks
-             (content/content block-id
-                              {:hiccup ref-hiccup})]))))))
+    (let [[loading? set-loading!] (hooks/use-state true)]
+      (hooks/use-effect!
+       (fn []
+         (p/do!
+          (db-async/<get-block-refs (state/get-current-repo) (:db/id e))
+          (set-loading! false)))
+       [])
+      (when-not loading?
+        (block-linked-references-aux e)))))
 
 (rum/defc references-cp
   [page-entity]

+ 20 - 19
src/main/frontend/components/whiteboard.cljs

@@ -4,24 +4,25 @@
             [frontend.components.onboarding.quick-tour :as quick-tour]
             [frontend.components.page :as page]
             [frontend.components.reference :as reference]
+            [frontend.config :as config]
             [frontend.context.i18n :refer [t]]
+            [frontend.db :as db]
             [frontend.db-mixins :as db-mixins]
+            [frontend.db.async :as db-async]
             [frontend.db.model :as model]
             [frontend.handler.route :as route-handler]
             [frontend.handler.whiteboard :as whiteboard-handler]
+            [frontend.hooks :as hooks]
             [frontend.modules.shortcut.core :as shortcut]
             [frontend.rum :refer [use-bounding-client-rect use-breakpoint]]
             [frontend.state :as state]
             [frontend.ui :as ui]
             [frontend.util :as util]
+            [logseq.common.util :as common-util]
+            [logseq.shui.ui :as shui]
             [promesa.core :as p]
             [rum.core :as rum]
-            [shadow.loader :as loader]
-            [frontend.config :as config]
-            [frontend.db.async :as db-async]
-            [logseq.common.util :as common-util]
-            [frontend.db :as db]
-            [logseq.shui.ui :as shui]))
+            [shadow.loader :as loader]))
 
 (defonce tldraw-loaded? (atom false))
 (rum/defc tldraw-app < rum/reactive
@@ -36,21 +37,21 @@
     (when draw-component
       (draw-component page-uuid shape-id))))
 
-(rum/defc tldraw-preview < rum/reactive
-  {:init (fn [state]
-           (p/let [_ (loader/load :tldraw)]
-             (reset! tldraw-loaded? true))
-           (let [page-uuid (first (:rum/args state))]
-             (db-async/<get-block (state/get-current-repo) page-uuid))
-           state)}
+(rum/defc tldraw-preview
   [page-uuid]
   (when page-uuid
-    (let [loaded? (rum/react tldraw-loaded?)
-          tldr (whiteboard-handler/get-page-tldr page-uuid)
-          generate-preview (when loaded?
-                             (resolve 'frontend.extensions.tldraw/generate-preview))]
-      (when (and generate-preview (not (state/sub-async-query-loading page-uuid)))
-        (generate-preview tldr)))))
+    (let [[loading? set-loading!] (hooks/use-state true)]
+      (hooks/use-effect!
+       (p/do!
+        (loader/load :tldraw)
+        (db-async/<get-block (state/get-current-repo) page-uuid)
+        (set-loading! false))
+       [])
+      (when-not loading?
+        (let [tldr (whiteboard-handler/get-page-tldr page-uuid)
+              generate-preview (resolve 'frontend.extensions.tldraw/generate-preview)]
+          (when generate-preview
+            (generate-preview tldr)))))))
 
 ;; TODO: move to frontend.components.reference
 (rum/defcs references-count < rum/reactive db-mixins/query

+ 13 - 20
src/main/frontend/db/async.cljs

@@ -124,22 +124,19 @@
       (p/promise e)
 
       :else
-      (do
-        (state/update-state! :db/async-query-loading (fn [s] (conj s name')))
-        (p/let [result (state/<invoke-db-worker :thread-api/get-blocks graph
-                                                [{:id id :opts opts}])
-                {:keys [block children]} (first result)]
-          (state/update-state! :db/async-query-loading (fn [s] (disj s name')))
-          (when-not skip-transact?
-            (let [conn (db/get-db graph false)
-                  block-and-children (if block (cons block children) children)
-                  affected-keys [[:frontend.worker.react/block (:db/id block)]]
-                  tx-data (remove (fn [b] (:block.temp/fully-loaded? (db/entity (:db/id b)))) block-and-children)]
-              (when (seq tx-data) (d/transact! conn tx-data))
-              (when-not skip-refresh?
-                (react/refresh-affected-queries! graph affected-keys))))
+      (p/let [result (state/<invoke-db-worker :thread-api/get-blocks graph
+                                              [{:id id :opts opts}])
+              {:keys [block children]} (first result)]
+        (when-not skip-transact?
+          (let [conn (db/get-db graph false)
+                block-and-children (if block (cons block children) children)
+                affected-keys [[:frontend.worker.react/block (:db/id block)]]
+                tx-data (remove (fn [b] (:block.temp/fully-loaded? (db/entity (:db/id b)))) block-and-children)]
+            (when (seq tx-data) (d/transact! conn tx-data))
+            (when-not skip-refresh?
+              (react/refresh-affected-queries! graph affected-keys))))
 
-          (if children-only? children block))))))
+        (if children-only? children block)))))
 
 (defn <get-blocks
   [graph ids* & {:as opts}]
@@ -159,22 +156,18 @@
 (defn <get-block-parents
   [graph id depth]
   (assert (integer? id))
-  (when-let [block-id (:block/uuid (db/entity graph id))]
-    (state/update-state! :db/async-query-loading (fn [s] (conj s (str block-id "-parents"))))
+  (when (:block/uuid (db/entity graph id))
     (p/let [result (state/<invoke-db-worker :thread-api/get-block-parents graph id depth)
             conn (db/get-db graph false)
             _ (d/transact! conn result)]
-      (state/update-state! :db/async-query-loading (fn [s] (disj s (str block-id "-parents"))))
       result)))
 
 (defn <get-block-refs
   [graph eid]
   (assert (integer? eid))
-  (state/update-state! :db/async-query-loading (fn [s] (conj s (str eid "-refs"))))
   (p/let [result (state/<invoke-db-worker :thread-api/get-block-refs graph eid)
           conn (db/get-db graph false)
           _ (d/transact! conn result)]
-    (state/update-state! :db/async-query-loading (fn [s] (disj s (str eid "-refs"))))
     result))
 
 (defn <get-block-refs-count

+ 22 - 20
src/main/frontend/extensions/tldraw.cljs

@@ -233,27 +233,29 @@
             :onPersist #(on-persist page-uuid %1 %2)
             :model data})])
 
-(rum/defc tldraw-app-inner < rum/reactive
-  {:init (fn [state]
-           (let [page-uuid (first (:rum/args state))]
-             (db-async/<get-block (state/get-current-repo) page-uuid)
-             state))}
+(rum/defc tldraw-app-inner
   [page-uuid block-id loaded-app set-loaded-app]
-  (when-not (state/sub-async-query-loading (str page-uuid))
-    (let [populate-onboarding? (whiteboard-handler/should-populate-onboarding-whiteboard? page-uuid)
-          on-mount (fn [^js tln]
-                     (when tln
-                       (set! (.-appUndo tln) undo)
-                       (set! (.-appRedo tln) redo)
-                       (when-let [^js api (gobj/get tln "api")]
-                         (p/then (when populate-onboarding?
-                                   (whiteboard-handler/populate-onboarding-whiteboard api))
-                                 #(do (whiteboard-handler/cleanup! (.-currentPage tln))
-                                      (state/focus-whiteboard-shape tln block-id)
-                                      (set-loaded-app tln))))))
-          data (whiteboard-handler/get-page-tldr page-uuid)]
-      (when data
-        (tldraw-inner page-uuid data populate-onboarding? loaded-app on-mount)))))
+  (let [[loading? set-loading!] (hooks/use-state true)]
+    (hooks/use-effect!
+     (p/do!
+      (db-async/<get-block (state/get-current-repo) page-uuid)
+      (set-loading! false))
+     [])
+    (when-not loading?
+      (let [populate-onboarding? (whiteboard-handler/should-populate-onboarding-whiteboard? page-uuid)
+            on-mount (fn [^js tln]
+                       (when tln
+                         (set! (.-appUndo tln) undo)
+                         (set! (.-appRedo tln) redo)
+                         (when-let [^js api (gobj/get tln "api")]
+                           (p/then (when populate-onboarding?
+                                     (whiteboard-handler/populate-onboarding-whiteboard api))
+                                   #(do (whiteboard-handler/cleanup! (.-currentPage tln))
+                                        (state/focus-whiteboard-shape tln block-id)
+                                        (set-loaded-app tln))))))
+            data (whiteboard-handler/get-page-tldr page-uuid)]
+        (when data
+          (tldraw-inner page-uuid data populate-onboarding? loaded-app on-mount))))))
 
 (rum/defc tldraw-app
   [page-uuid block-id]

+ 1 - 2
src/main/frontend/handler/db_based/import.cljs

@@ -125,7 +125,6 @@
                   (when (seq misc-tx)
                     (db/transact! repo misc-tx tx-meta))
                   (when-not import-block?
-                    (state/clear-async-query-state!)
                     (ui-handler/re-render-root!)
                     (notification/show! "Import successful!" :success)))
                 (p/catch (fn [e]
@@ -151,4 +150,4 @@
                       :on-change (fn [^js e] (swap! import-inputs assoc :import-data (util/evalue e)))})
       (shui/button {:class "mt-3"
                     :on-click (partial import-edn-data-from-form import-inputs)}
-                   "Import")])))
+                   "Import")])))

+ 0 - 1
src/main/frontend/handler/events.cljs

@@ -199,7 +199,6 @@
 (defmethod handle :graph/switch [[_ graph opts]]
   (export/cancel-db-backup!)
   (persist-db/export-current-graph!)
-  (state/set-state! :db/async-query-loading #{})
   (state/set-state! :db/async-queries {})
   (st/refresh!)
 

+ 0 - 15
src/main/frontend/rum.cljs

@@ -1,12 +1,10 @@
 (ns frontend.rum
   "Utility fns for rum"
   (:require [cljs-bean.core :as bean]
-            [cljs.cache :as cache]
             [clojure.set :as set]
             [clojure.string :as string]
             [clojure.walk :as w]
             [daiquiri.interpreter :as interpreter]
-            [frontend.common.cache :as common.cache]
             [frontend.hooks :as hooks]
             [rum.core :refer [use-state] :as rum]))
 
@@ -145,16 +143,3 @@
    (let [[ref rect] (use-bounding-client-rect tick)
          bp (->breakpoint (when (some? rect) (.-width rect)))]
      [ref bp])))
-
-(defonce *key->atom-cache (volatile! (cache/lru-cache-factory {} :threshold 3000)))
-
-(defn- gen-cached-derived-atom
-  [ref key' f]
-  (rum/derived-atom [ref] key' f))
-
-(def cached-derived-atom
-  (common.cache/cache-fn
-   *key->atom-cache
-   (fn [ref key' f]
-     [key' [ref key' f]])
-   gen-cached-derived-atom))

+ 20 - 44
src/main/frontend/state.cljs

@@ -9,13 +9,11 @@
             [datascript.core :as d]
             [dommy.core :as dom]
             [electron.ipc :as ipc]
-            [frontend.common.missionary :as c.m]
             [frontend.db.conn-state :as db-conn-state]
             [frontend.db.transact :as db-transact]
             [frontend.flows :as flows]
             [frontend.hooks :as hooks]
             [frontend.mobile.util :as mobile-util]
-            [frontend.rum :as r]
             [frontend.spec.storage :as storage-spec]
             [frontend.storage :as storage]
             [frontend.util :as util]
@@ -346,7 +344,6 @@
       :ui/highlight-recent-days              (atom (or (storage/get :ui/highlight-recent-days)
                                                        3))
       :favorites/updated?                    (atom 0)
-      :db/async-query-loading                (atom #{})
       :db/async-queries                      (atom {})
       :db/latest-transacted-entity-uuids     (atom {})})))
 
@@ -689,24 +686,29 @@ Similar to re-frame subscriptions"
   [container-block]
   (reset! (:editor/editing? @state) {container-block true}))
 
-(def ^:private editing-flow
-  (m/watch (:editor/editing? @state)))
-
-(defn sub-editing?
-  [container-block]
-  (let [checkf (hooks/use-callback
-                (fn [s] (boolean (get s container-block)))
-                [container-block])
-        init-value (checkf @(:editor/editing? @state))
+(defn- sub-flow-state
+  [flow watch-ref sub-value-f deps]
+  (let [checkf (hooks/use-callback sub-value-f deps)
+        init-value (checkf @watch-ref)
         flow (hooks/use-memo
               #(m/eduction
                 (map checkf)
                 (dedupe)
                 (drop-while (fn [x] (identical? x init-value)))
-                editing-flow)
+                flow)
               [init-value])]
     (hooks/use-flow-state init-value flow)))
 
+(def ^:private editing-flow
+  (m/watch (:editor/editing? @state)))
+
+(defn sub-editing?
+  [container-block]
+  (sub-flow-state editing-flow
+                  (:editor/editing? @state)
+                  (fn [s] (boolean (get s container-block)))
+                  [container-block]))
+
 (defn sub-config
   "Sub equivalent to get-config which should handle all sub user-config access"
   ([] (sub-config (get-current-repo)))
@@ -1187,14 +1189,11 @@ Similar to re-frame subscriptions"
 (defn sub-block-selected?
   [block-id]
   (assert (uuid? block-id))
-  (let [checkf (fn [block-id]
-                 (some #{block-id} (get-selected-block-ids @(:selection/blocks @state))))
-        init-value (checkf block-id)]
-    (hooks/use-flow-state
-     init-value
-     (m/eduction
-      (map checkf) (dedupe) (drop-while #(= % init-value))
-      block-selected-flow))))
+  (sub-flow-state block-selected-flow
+                  (:selection/blocks @state)
+                  (fn [blocks]
+                    (some #{block-id} (get-selected-block-ids blocks)))
+                  [block-id]))
 
 (defn dom-clear-selection!
   []
@@ -2312,29 +2311,6 @@ Similar to re-frame subscriptions"
   []
   (storage/remove :user-groups))
 
-(defn sub-async-query-loading
-  [k]
-  (assert (or (string? k) (uuid? k)))
-  (let [k* (str k)]
-    (rum/react
-     (r/cached-derived-atom (:db/async-query-loading @state) [(get-current-repo) ::async-query k*]
-                            (fn [s] (contains? s k*))))))
-
-(def ^:private async-query-loading-flow
-  (->> (m/watch (:db/async-query-loading @state))
-       (c.m/throttle 100)))
-
-(defn async-query-k-flow
-  [k]
-  (let [k* (str k)]
-    (m/eduction
-     (map #(contains? % k*)) (dedupe)
-     async-query-loading-flow)))
-
-(defn clear-async-query-state!
-  []
-  (reset! (:db/async-query-loading @state) #{}))
-
 (defn set-color-accent! [color]
   (swap! state assoc :ui/radix-color color)
   (storage/set :ui/radix-color color)