fix: preserve the original :block/page ref when missing page details

This commit is contained in:
Tienson Qin
2026-02-25 07:40:29 +08:00
parent 8b5407a91d
commit 25d0a44681
4 changed files with 44 additions and 9 deletions

View File

@@ -20,6 +20,19 @@
(when (seq queries)
(boolean (some #(= % title) (map :title queries))))))
(defn- grouped-by-page-result?
[result group-by-page?]
(let [first-group (first result)
first-page (first first-group)
first-block (first (second first-group))]
(boolean
(and group-by-page?
(seq result)
(coll? first-group)
(or (:block/name first-page)
(:db/id first-page))
(:block/uuid first-block)))))
(rum/defcs custom-query-inner < rum/static
[state {:keys [dsl-query?] :as config} {:keys [query breadcrumb-show?]}
{:keys [query-error-atom
@@ -30,12 +43,7 @@
(let [{:keys [->hiccup]} config
*query-error query-error-atom
only-blocks? (:block/uuid (first result))
blocks-grouped-by-page? (and group-by-page?
(seq result)
(coll? (first result))
(:block/name (ffirst result))
(:block/uuid (first (second (first result))))
true)]
blocks-grouped-by-page? (grouped-by-page-result? result group-by-page?)]
(if @*query-error
(do
(log/error :exception @*query-error)

View File

@@ -98,11 +98,18 @@ independent of format as format specific heading characters are stripped"
(remove nil?))
pages (when (seq pages-ids)
(db-utils/pull-many '[:db/id :block/name :block/title :block/journal-day] pages-ids))
pages-map (reduce (fn [acc p] (assoc acc (:db/id p) p)) {} pages)
pages-map (reduce (fn [acc p]
(if (map? p)
(assoc acc (:db/id p) p)
acc))
{}
pages)
blocks (map
(fn [block]
(assoc block :block/page
(get pages-map (:db/id (:block/page block)))))
(assoc block
:block/page
(or (get pages-map (:db/id (:block/page block)))
(:block/page block))))
blocks)]
blocks))

View File

@@ -0,0 +1,10 @@
(ns frontend.components.query-test
(:require [cljs.test :refer [deftest is]]
[frontend.components.query :as query]))
(deftest grouped-by-page-result-detection-supports-partial-page-refs
(let [result [[{:db/id 42}
[{:block/uuid (random-uuid)}]]]]
(is (true? (#'frontend.components.query/grouped-by-page-result? result true))
"Grouped query results with page refs that only include :db/id should still be recognized")
(is (false? (#'frontend.components.query/grouped-by-page-result? result false)))))

View File

@@ -117,3 +117,13 @@
(is (= ["child 1" "child 2" "child 3"]
(map :block/title
(model/get-block-immediate-children test-db (:block/uuid parent)))))))
(deftest with-pages-preserves-page-ref-when-ui-db-is-partial
(let [page-ref {:db/id 1}
block {:db/id 2
:block/uuid (random-uuid)
:block/page page-ref}]
(with-redefs [frontend.db.utils/pull-many (fn [& _] nil)]
(is (= page-ref
(:block/page (first (model/with-pages [block]))))
"When page entity details are unavailable locally, keep the original page ref instead of replacing it with nil"))))