Browse Source

Recents store page db ids instead of uuids or page names

Tienson Qin 1 year ago
parent
commit
96c9605b82

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

@@ -1703,6 +1703,7 @@
       (editor-handler/highlight-block! uuid)))
 
   (editor-handler/block->data-transfer! uuid event false)
+
   (.setData (gobj/get event "dataTransfer")
             "block-dom-id"
             block-id)

+ 14 - 16
src/main/frontend/components/container.cljs

@@ -56,7 +56,8 @@
             [logseq.common.path :as path]
             [react-draggable]
             [reitit.frontend.easy :as rfe]
-            [rum.core :as rum]))
+            [rum.core :as rum]
+            [logseq.db :as ldb]))
 
 (rum/defc nav-content-item < rum/reactive
   [name {:keys [class count]} child]
@@ -73,21 +74,18 @@
       (when child [:div.bd child])]]))
 
 (rum/defc page-name
-  [name icon recent?]
-  (let [original-name (db-model/get-page-original-name name)
-        whiteboard-page? (db-model/whiteboard-page? name)
-        untitled? (db-model/untitled-page? name)
-        name (util/safe-page-name-sanity-lc name)
+  [page icon recent?]
+  (let [original-name (:block/original-name page)
+        whiteboard-page? (db-model/whiteboard-page? page)
+        untitled? (db-model/untitled-page? original-name)
+        name (:block/name page)
         file-rpath (when (util/electron?) (page-util/get-page-file-rpath name))
         source-page (db-model/get-alias-source-page (state/get-current-repo) name)
         ctx-icon #(shui/tabler-icon %1 {:class "scale-90 pr-1 opacity-80"})
-        open-in-sidebar #(when-let [page-entity (and (not whiteboard-page?)
-                                                     (if (empty? source-page)
-                                                       (db/entity [:block/name name]) source-page))]
-                           (state/sidebar-add-block!
-                            (state/get-current-repo)
-                            (:db/id page-entity)
-                            :page))
+        open-in-sidebar #(state/sidebar-add-block!
+                         (state/get-current-repo)
+                         (:db/id page)
+                         :page)
         x-menu-content (fn []
                          (let [x-menu-item shui/dropdown-menu-item
                                x-menu-shortcut shui/dropdown-menu-shortcut]
@@ -175,7 +173,7 @@
                                 icon (icon/get-page-icon e {})]
                             {:id (str (:db/id e))
                              :value name
-                             :content [:li.favorite-item (page-name name icon false)]}))
+                             :content [:li.favorite-item (page-name e icon false)]}))
                         favorite-entities)]
          (dnd-component/items favorites
                               {:on-drag-end (fn [favorites]
@@ -196,14 +194,14 @@
 
      [:ul.text-sm
       (for [name pages]
-        (when-let [entity (db/entity [:block/name (util/safe-page-name-sanity-lc name)])]
+        (when-let [entity (db/entity (ldb/get-first-page-by-name (db/get-db) name))]
           [:li.recent-item.select-none
            {:key name
             :title name
             :draggable true
             :on-drag-start (fn [event] (editor-handler/block->data-transfer! name event true))
             :data-ref name}
-           (page-name name (icon/get-page-icon entity {}) true)]))])))
+           (page-name entity (icon/get-page-icon entity {}) true)]))])))
 
 (rum/defcs flashcards < db-mixins/query rum/reactive
   {:did-mount (fn [state]

+ 11 - 10
src/main/frontend/db/utils.cljs

@@ -19,20 +19,21 @@
     blocks))
 
 (defn entity
-  "This function will return nil if passed `id-or-lookup-ref` is an integer and
+  "This function will return nil if passed `eid` is an integer and
   the entity doesn't exist in db.
   `repo-or-db`: a repo string or a db,
-  `id-or-lookup-ref`: same as d/entity."
-  ([id-or-lookup-ref]
-   (entity (state/get-current-repo) id-or-lookup-ref))
-  ([repo-or-db id-or-lookup-ref]
-   (when-let [db (if (string? repo-or-db)
+  `eid`: same as d/entity."
+  ([eid]
+   (entity (state/get-current-repo) eid))
+  ([repo-or-db eid]
+   (when eid
+     (when-let [db (if (string? repo-or-db)
                    ;; repo
-                   (let [repo (or repo-or-db (state/get-current-repo))]
-                     (conn/get-db repo))
+                     (let [repo (or repo-or-db (state/get-current-repo))]
+                       (conn/get-db repo))
                    ;; db
-                   repo-or-db)]
-     (d/entity db id-or-lookup-ref))))
+                     repo-or-db)]
+       (d/entity db eid)))))
 
 (defn update-block-content
   "Replace `[[internal-id]]` with `[[page name]]`"

+ 2 - 3
src/main/frontend/handler/common/page.cljs

@@ -17,7 +17,6 @@
             [goog.object :as gobj]
             [promesa.core :as p]
             [frontend.handler.block :as block-handler]
-            [frontend.handler.file-based.recent :as file-recent-handler]
             [logseq.db :as ldb]
             [frontend.db.conn :as conn]
             [datascript.core :as d]
@@ -46,6 +45,7 @@
          config (state/get-config repo)
          [_ page-name] (worker-page/create! repo conn config title options)]
      (when redirect?
+       ;; FIXME: use uuid instead
        (route-handler/redirect-to-page! page-name))
      (when-let [first-block (first (:block/_left (db/entity [:block/name page-name])))]
        (block-handler/edit-block! first-block :max nil))
@@ -63,6 +63,7 @@
            [p page-name] (worker-page/create! repo conn config title options)
            _result p]
      (when redirect?
+       ;; FIXME: use uuid instead
        (route-handler/redirect-to-page! page-name))
      (let [page (db/entity [:block/name page-name])]
        (when-let [first-block (first (:block/_left page))]
@@ -223,8 +224,6 @@
         (config-handler/set-config! :default-home (assoc home :page new-name))))
 
     (when-not db-based?
-      (file-recent-handler/update-or-add-renamed-page repo old-page-name new-page-name)
-
       (when (and old-path new-path)
         (rename-file! old-path new-path)))
 

+ 11 - 13
src/main/frontend/handler/db_based/recent.cljs

@@ -6,25 +6,23 @@
             [logseq.db :as ldb]))
 
 (defn add-page-to-recent!
-  [page click-from-recent?]
+  [db-id click-from-recent?]
   (when-not (:db/restoring? @state/state)
-    (when-let [page-uuid (if (uuid? page)
-                           nil
-                           (ldb/get-page-uuid (db/get-db) page))]
-      (when-let [page (db/entity [:block/uuid page-uuid])]
-        (when-not (ldb/hidden-page? page)
-          (let [pages (state/get-recent-pages)]
-            (when (or (and click-from-recent? (not ((set pages) page-uuid)))
-                      (not click-from-recent?))
-              (let [new-pages (vec (take 15 (distinct (cons page-uuid pages))))]
-                (state/set-recent-pages! new-pages)))))))))
+    (when-let [page (db/entity db-id)]
+      (when-not (ldb/hidden-page? page)
+        (let [pages (state/get-recent-pages)]
+          (when (or (and click-from-recent? (not ((set pages) db-id)))
+                    (not click-from-recent?))
+            (let [new-pages (vec (take 15 (distinct (cons db-id pages))))]
+              (state/set-recent-pages! new-pages))))))))
 
 (defn get-recent-pages
   []
   (->> (state/get-recent-pages)
        (distinct)
        (map (fn [id]
-              (let [e (db/entity [:block/uuid id])]
+              (let [e (db/entity id)]
                 (:block/original-name e))))
        (remove string/blank?)
-       (remove ldb/hidden-page?)))
+       (remove ldb/hidden-page?)
+       (take 20)))

+ 2 - 3
src/main/frontend/handler/editor.cljs

@@ -1205,9 +1205,8 @@
              (let [page-id (some-> (db/entity [:block/uuid block-id])
                                    :block/page
                                    :db/id)]
-
-               (when-let [page-name (:block/name (db/entity page-id))]
-                 (route-handler/redirect-to-page! page-name)
+               (when-let [page (db/entity page-id)]
+                 (route-handler/redirect-to-page! (:block/uuid page))
                  (util/schedule #(edit-block! {:block/uuid block-id} :max nil)))))))))
     (js/window.history.back)))
 

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

@@ -1,33 +0,0 @@
-(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)))

+ 5 - 17
src/main/frontend/handler/recent.cljs

@@ -1,24 +1,12 @@
 (ns frontend.handler.recent
   "Fns related to recent pages feature"
-  (:require [frontend.handler.db-based.recent :as db-based]
-            [frontend.handler.file-based.recent :as file-recent-handler]
-            [frontend.config :as config]
-            [frontend.state :as state]
-            [frontend.db.model :as model]))
+  (:require [frontend.handler.db-based.recent :as db-based]))
 
 (defn add-page-to-recent!
-  [repo page-name-or-block-uuid click-from-recent?]
-  (let [page-name (if (uuid? page-name-or-block-uuid)
-                    (when-let [block (model/get-block-by-uuid page-name-or-block-uuid)]
-                      (get-in block [:block/page :block/original-name]))
-                    page-name-or-block-uuid)]
-    (if (config/db-based-graph? repo)
-    (db-based/add-page-to-recent! page-name click-from-recent?)
-    (file-recent-handler/add-page-to-recent! repo page-name click-from-recent?))))
+  [db-id click-from-recent?]
+  (when db-id
+    (db-based/add-page-to-recent! db-id click-from-recent?)))
 
 (defn get-recent-pages
   []
-  (let [repo (state/get-current-repo)]
-    (if (config/db-based-graph? repo)
-      (db-based/get-recent-pages)
-      (file-recent-handler/get-recent-pages))))
+  (db-based/get-recent-pages))

+ 9 - 4
src/main/frontend/handler/route.cljs

@@ -13,7 +13,9 @@
             [frontend.extensions.pdf.utils :as pdf-utils]
             [logseq.graph-parser.text :as text]
             [reitit.frontend.easy :as rfe]
-            [frontend.context.i18n :refer [t]]))
+            [frontend.context.i18n :refer [t]]
+            [clojure.string :as string]
+            [logseq.db :as ldb]))
 
 (defn redirect!
   "If `push` is truthy, previous page will be left in history."
@@ -74,11 +76,14 @@
    (redirect-to-page! page-name {}))
   ([page-name {:keys [anchor push click-from-recent? block-id new-whiteboard?]
                :or {click-from-recent? false}}]
-   (when (or (uuid? page-name) (seq page-name))
+   (when (or (uuid? page-name)
+             (and (string? page-name) (not (string/blank? page-name))))
      ;; Always skip onboarding when loading an existing whiteboard
      (when-not new-whiteboard? (state/set-onboarding-whiteboard! true))
-     (recent-handler/add-page-to-recent! (state/get-current-repo) page-name
-                                         click-from-recent?)
+     (when-let [db-id (if (uuid? page-name)
+                        (:db/id (db/entity [:block/uuid page-name]))
+                        (:db/id (db/entity (ldb/get-first-page-by-name (db/get-db) page-name))))]
+       (recent-handler/add-page-to-recent! db-id click-from-recent?))
      (if (and (= name (state/get-current-whiteboard)) block-id)
        (state/focus-whiteboard-shape block-id)
        (let [m (cond->