Просмотр исходного кода

fix: :default-queries for db graphs

closes LOG-3042. Moved :default-queries to default-config as this allows
for any future fix to automatically update for a user. There's also a
breaking change with this config key from :block/journal? so moving this
to default-config makes the migration easier e.g. just tell users to
remove the config key
Gabriel Horner 1 год назад
Родитель
Сommit
489e7f3422

+ 4 - 34
deps/common/resources/templates/config.edn

@@ -203,40 +203,10 @@
  {:sort-by-priority
   (fn [result] (sort-by (fn [h] (get h :block/priority "Z")) result))}
 
- ;; The following queries will be displayed at the bottom of today's journal page.
- ;; The "NOW" query returns tasks with "NOW" or "DOING" status.
- ;; The "NEXT" query returns tasks with "NOW", "LATER", or "TODO" status.
- :default-queries
- {:journals
-  [{:title "🔨 NOW"
-    :query [:find (pull ?h [*])
-            :in $ ?start ?today
-            :where
-            [?h :block/marker ?marker]
-            [(contains? #{"NOW" "DOING"} ?marker)]
-            [?h :block/page ?p]
-            [?p :block/journal-day ?d]
-            [(>= ?d ?start)]
-            [(<= ?d ?today)]]
-    :inputs [:14d :today]
-    :result-transform (fn [result]
-                        (sort-by (fn [h]
-                                   (get h :block/priority "Z")) result))
-    :group-by-page? false
-    :collapsed? false}
-   {:title "📅 NEXT"
-    :query [:find (pull ?h [*])
-            :in $ ?start ?next
-            :where
-            [?h :block/marker ?marker]
-            [(contains? #{"NOW" "LATER" "TODO"} ?marker)]
-            [?h :block/page ?p]
-            [?p :block/journal-day ?d]
-            [(> ?d ?start)]
-            [(< ?d ?next)]]
-    :inputs [:today :7d-after]
-    :group-by-page? false
-    :collapsed? false}]}
+;; Queries will be displayed at the bottom of today's journal page.
+;; Example usage:
+;; :default-queries
+;; {:journals []}
 
  ;; Add custom commands to the command palette
  ;; Example usage:

+ 6 - 0
deps/db/src/logseq/db/frontend/rules.cljc

@@ -237,6 +237,12 @@
       [?v :block/content ?status]
       [(contains? ?statuses ?status)]]}))
 
+(def rules-dependencies
+  "For db graphs, a map of rule names and the rules they depend on. If this map
+  becomes long or brittle, we could do scan rules for their deps with something
+  like find-rules-in-where"
+  {:task #{:property}})
+
 (defn extract-rules
   "Given a rules map and the rule names to extract, returns a vector of rules to
   be passed to datascript.core/q. Can handle rules with multiple or single clauses"

+ 4 - 1
src/main/frontend/db/query_custom.cljs

@@ -28,7 +28,10 @@
   [{:keys [query] :as query-m} {:keys [db-graph?]}]
   (let [{:keys [where in]} (datalog-util/query-vec->map query)
         query-dsl-rules (if db-graph? rules/db-query-dsl-rules rules/query-dsl-rules)
-        rules-found (datalog-util/find-rules-in-where where (-> query-dsl-rules keys set))]
+        rules-found* (datalog-util/find-rules-in-where where (-> query-dsl-rules keys set))
+        rules-found (cond-> rules-found*
+                      db-graph?
+                      (concat (mapcat rules/rules-dependencies rules-found*)))]
     (if (seq rules-found)
       (if (and (= '% (last in)) (vector? (last (:inputs query-m))))
         ;; Add to existing :inputs rules

+ 72 - 5
src/main/frontend/state.cljs

@@ -19,7 +19,8 @@
             [medley.core :as medley]
             [promesa.core :as p]
             [rum.core :as rum]
-            [frontend.rum :as r]))
+            [frontend.rum :as r]
+            [logseq.db.sqlite.util :as sqlite-util]))
 
 (defonce *profile-state
   (atom {}))
@@ -341,8 +342,8 @@
 ;;  (re-)fetches get-current-repo needlessly
 ;; TODO: Add consistent validation. Only a few config options validate at get time
 
-(def default-config
-  "Default config for a repo-specific, user config"
+(def common-default-config
+  "Common default config for a user's repo config"
   {:feature/enable-search-remove-accents? true
    :default-arweave-gateway "https://arweave.net"
    :ui/auto-expand-block-refs? true
@@ -353,6 +354,72 @@
    ;; Then the default value is applied
    :file/name-format :legacy})
 
+(def file-default-config
+  "Default repo config for file graphs"
+  (merge common-default-config
+         ;; The "NOW" query returns tasks with "NOW" or "DOING" status.
+         ;; The "NEXT" query returns tasks with "NOW", "LATER", or "TODO" status.
+         {:default-queries
+          {:journals
+           [{:title "🔨 NOW"
+             :query '[:find (pull ?h [*])
+                      :in $ ?start ?today
+                      :where
+                      (task ?h #{"NOW" "DOING"})
+                      [?h :block/page ?p]
+                      [?p :block/journal-day ?d]
+                      [(>= ?d ?start)]
+                      [(<= ?d ?today)]]
+             :inputs [:14d :today]
+             :result-transform '(fn [result]
+                                 (sort-by (fn [h]
+                                            (get h :block/priority "Z")) result))
+             :group-by-page? false
+             :collapsed? false}
+            {:title "📅 NEXT"
+             :query '[:find (pull ?h [*])
+                      :in $ ?start ?next
+                      :where
+                      (task ?h #{"NOW" "LATER" "TODO"})
+                      [?h :block/page ?p]
+                      [?p :block/journal-day ?d]
+                      [(> ?d ?start)]
+                      [(< ?d ?next)]]
+             :inputs [:today :7d-after]
+             :group-by-page? false
+             :collapsed? false}]}}))
+
+(def db-default-config
+  "Default repo config for DB graphs"
+  (merge common-default-config
+         ;; The "NOW" query returns tasks with "Doing" status for recent past days
+         ;; The "NEXT" query returns tasks with "Todo" status for upcoming future days
+         {:default-queries
+          {:journals
+           [{:title "🔨 NOW"
+             :query '[:find (pull ?b [*])
+                      :in $ ?start ?today
+                      :where
+                      (task ?b #{"Doing"})
+                      [?b :block/page ?p]
+                      [?p :block/journal-day ?d]
+                      [(>= ?d ?start)]
+                      [(<= ?d ?today)]]
+             :inputs [:14d :today]
+             :collapsed? false}
+            {:title "📅 NEXT"
+             :query '[:find (pull ?b [*])
+                      :in $ ?start ?next
+                      :where
+                      (task ?b #{"Todo"})
+                      [?b :block/page ?p]
+                      [?p :block/journal-day ?d]
+                      [(> ?d ?start)]
+                      [(< ?d ?next)]]
+             :inputs [:today :7d-after]
+             :group-by-page? false
+             :collapsed? false}]}}))
+
 ;; State that most user config is dependent on
 (declare get-current-repo sub set-state!)
 
@@ -387,7 +454,7 @@ should be done through this fn in order to get global config and config defaults
    (get-config (get-current-repo)))
   ([repo-url]
    (merge-configs
-    default-config
+    (if (sqlite-util/db-based-graph? repo-url) db-default-config file-default-config)
     (get-global-config)
     (get-graph-config repo-url))))
 
@@ -627,7 +694,7 @@ Similar to re-frame subscriptions"
   ([] (sub-config (get-current-repo)))
   ([repo]
    (let [config (sub :config)]
-     (merge-configs default-config
+     (merge-configs (if (sqlite-util/db-based-graph? repo) db-default-config file-default-config)
                     (get config ::global-config)
                     (get config repo)))))