Browse Source

fix: store page uuids in recent/pages for db graphs

Tienson Qin 2 years ago
parent
commit
abf574a90a

+ 2 - 7
src/main/frontend/components/container.cljs

@@ -25,6 +25,7 @@
             [frontend.handler.route :as route-handler]
             [frontend.handler.user :as user-handler]
             [frontend.handler.whiteboard :as whiteboard-handler]
+            [frontend.handler.recent :as recent-handler]
             [frontend.mixins :as mixins]
             [frontend.mobile.action-bar :as action-bar]
             [frontend.mobile.footer :as footer]
@@ -166,13 +167,7 @@
 
    {:class "recent"}
 
-   (let [pages (->> (db/sub-key-value :recent/pages)
-                    (remove string/blank?)
-                    (filter string?)
-                    (map (fn [page] {:lowercase (util/safe-page-name-sanity-lc page)
-                                     :page page}))
-                    (util/distinct-by :lowercase)
-                    (map :page))]
+   (let [pages (recent-handler/get-recent-pages)]
      [:ul.text-sm
       (for [name pages]
         (when-let [entity (db/entity [:block/name (util/safe-page-name-sanity-lc name)])]

+ 2 - 4
src/main/frontend/components/search.cljs

@@ -13,6 +13,7 @@
             [frontend.db.model :as model]
             [frontend.handler.search :as search-handler]
             [frontend.handler.whiteboard :as whiteboard-handler]
+            [frontend.handler.recent :as recent-handler]
             [frontend.extensions.pdf.utils :as pdf-utils]
             [frontend.ui :as ui]
             [frontend.state :as state]
@@ -417,10 +418,7 @@
                     :on-click #(state/pub-event! [:modal/command-palette])}
                    (ui/icon "command" {:style {:font-size 20}})])])]]
    (let [recent-search (mapv (fn [q] {:type :search :data q}) (db/get-key-value :recent/search))
-         pages (->> (db/get-key-value :recent/pages)
-                    (remove nil?)
-                    (filter string?)
-                    (remove #(= (string/lower-case %) "contents"))
+         pages (->> (recent-handler/get-recent-pages)
                     (mapv (fn [page] {:type :page :data page})))
          result (concat (take 5 recent-search) pages)]
      (ui/auto-complete

+ 21 - 0
src/main/frontend/handler/db_based/recent.cljs

@@ -0,0 +1,21 @@
+(ns frontend.handler.db-based.recent
+  "Fns related to recent pages feature"
+  (:require [frontend.db :as db]
+            [logseq.graph-parser.util :as gp-util]))
+
+(defn add-page-to-recent!
+  [repo page click-from-recent?]
+  (when-let [page-uuid (if (uuid? page)
+                         page
+                         (:block/uuid (db/entity [:block/name (gp-util/page-name-sanity-lc page)])))]
+    (let [pages (or (db/get-key-value repo :recent/pages)
+                    '())]
+      (when (or (and click-from-recent? (not ((set pages) page-uuid)))
+                (not click-from-recent?))
+        (let [new-pages (take 15 (distinct (cons page-uuid pages)))]
+          (db/set-key-value repo :recent/pages new-pages))))))
+
+(defn get-recent-pages
+  []
+  (->> (db/get-key-value :recent/pages)
+       (map #(:block/original-name (db/entity [:block/uuid %])))))

+ 33 - 0
src/main/frontend/handler/file_based/recent.cljs

@@ -0,0 +1,33 @@
+(ns frontend.handler.file-based.recent
+  "Fns related to recent pages feature"
+  (:require [frontend.db :as db]
+            [frontend.util :as util]
+            [clojure.string :as string]))
+
+(defn add-page-to-recent!
+  [repo page click-from-recent?]
+  (let [pages (or (db/get-key-value repo :recent/pages)
+                  '())]
+    (when (or (and click-from-recent? (not ((set pages) page)))
+              (not click-from-recent?))
+      (let [new-pages (take 15 (distinct (cons page pages)))]
+        (db/set-key-value repo :recent/pages new-pages)))))
+
+(defn update-or-add-renamed-page [repo old-page-name new-page-name]
+  (let [pages (or (db/get-key-value repo :recent/pages)
+                  '())
+        updated-pages (replace {old-page-name new-page-name} pages)
+        updated-pages* (if (contains? (set updated-pages) new-page-name)
+                         updated-pages
+                         (cons new-page-name updated-pages))]
+    (db/set-key-value repo :recent/pages updated-pages*)))
+
+(defn get-recent-pages
+  []
+  (->> (db/sub-key-value :recent/pages)
+       (remove string/blank?)
+       (filter string?)
+       (map (fn [page] {:lowercase (util/safe-page-name-sanity-lc page)
+                        :page page}))
+       (util/distinct-by :lowercase)
+       (map :page)))

+ 1 - 1
src/main/frontend/handler/page.cljs

@@ -18,7 +18,7 @@
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.plugin :as plugin-handler]
             [frontend.handler.notification :as notification]
-            [frontend.handler.recent :as recent-handler]
+            [frontend.handler.file-based.recent :as recent-handler]
             [frontend.handler.route :as route-handler]
             [frontend.handler.ui :as ui-handler]
             [frontend.handler.web.nfs :as web-nfs]

+ 13 - 15
src/main/frontend/handler/recent.cljs

@@ -1,21 +1,19 @@
 (ns frontend.handler.recent
   "Fns related to recent pages feature"
-  (:require [frontend.db :as db]))
+  (:require [frontend.handler.db-based.recent :as db-based]
+            [frontend.handler.file-based.recent :as file-based]
+            [frontend.config :as config]
+            [frontend.state :as state]))
 
 (defn add-page-to-recent!
   [repo page click-from-recent?]
-  (let [pages (or (db/get-key-value repo :recent/pages)
-                  '())]
-    (when (or (and click-from-recent? (not ((set pages) page)))
-              (not click-from-recent?))
-      (let [new-pages (take 15 (distinct (cons page pages)))]
-        (db/set-key-value repo :recent/pages new-pages)))))
+  (if (config/db-based-graph? repo)
+    (db-based/add-page-to-recent! repo page click-from-recent?)
+    (file-based/add-page-to-recent! repo page click-from-recent?)))
 
-(defn update-or-add-renamed-page [repo old-page-name new-page-name]
-  (let [pages (or (db/get-key-value repo :recent/pages)
-                  '())
-        updated-pages (replace {old-page-name new-page-name} pages)
-        updated-pages* (if (contains? (set updated-pages) new-page-name)
-                         updated-pages
-                         (cons new-page-name updated-pages))]
-    (db/set-key-value repo :recent/pages updated-pages*)))
+(defn get-recent-pages
+  []
+  (let [repo (state/get-current-repo)]
+    (if (config/db-based-graph? repo)
+      (db-based/get-recent-pages)
+      (file-based/get-recent-pages))))

+ 2 - 3
src/main/logseq/api.cljs

@@ -13,6 +13,7 @@
             [frontend.components.plugins :as plugins]
             [frontend.config :as config]
             [frontend.handler.config :as config-handler]
+            [frontend.handler.recent :as recent-handler]
             [frontend.db :as db]
             [frontend.db.model :as db-model]
             [frontend.db.query-dsl :as query-dsl]
@@ -129,9 +130,7 @@
 
 (def ^:export get_current_graph_recent
   (fn []
-    (some->> (db/get-key-value :recent/pages)
-             (remove string/blank?)
-             (filter string?)
+    (some->> (recent-handler/get-recent-pages)
              (bean/->js))))
 
 (def ^:export get_current_graph_templates