Last active
July 22, 2022 16:21
-
-
Save tombarys/ad48787cdf7cf9792952c0b25f10cfd9 to your computer and use it in GitHub Desktop.
Looking to the past query generator for Roam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; TWO OPTIONS of using the looking-back query generator: | |
;; 1) WITH PARAMETERS: | |
;; {{roam/render ((this–cljs-block-id)) time_window time_ago}} where | |
;; "time_window" is number of days that will be seen (default: 7) | |
;; "time_ago" is number of days you are looking | |
;; Example: | |
;; {{roam/render ((gsWdfssdf)) 10 720}} | |
;; | |
;; 2) WITHOUT PARAMETERS: | |
;; Sliders will be shown to adjust both parameter. | |
;; | |
;; Example: | |
;; {{roam/render ((gsWdfssdf))}} | |
;; | |
(ns my-custom-roam-030722-V2 | |
(:require | |
[roam.util :refer [parse]] | |
[reagent.core :as r])) | |
(def ms-in-day "Millisenconds in a day of 86400 seconds." (* 86400 1000)) | |
(defn days-from-today-title [n] (js/window.roamAlphaAPI.util.dateToPageTitle (new js/Date (+ (.now js/Date) (* n ms-in-day))))) | |
(defn calc-data [{:keys [a t from-date to-date] :as data}] | |
(assoc data | |
:to-date (days-from-today-title (- (- a (js/Math.floor (/ t 2))))) | |
:from-date (days-from-today-title (- (+ a (/ t 2)))))) | |
(def db-state | |
(r/atom | |
{:time-data (calc-data {:a 365 :t 7}) | |
:events {:event/clicked false}})) | |
(defn parse-query [] | |
(let [{:keys [from-date to-date]} (:time-data @db-state)] | |
(parse (str "{{[[query]]: {between: [[" | |
from-date | |
"]] [[" | |
to-date | |
"]]}}}")))) | |
(defn slider [param value min max] | |
[:input {:type "range" :value value :min min :max max | |
:style {:width "50%"} | |
:on-change (fn [e] | |
(let [new-value (js/parseInt (.. e -target -value))] | |
(swap! db-state | |
(fn [data] | |
(assoc-in data [:time-data] | |
(-> data | |
(:time-data) | |
(assoc param new-value) | |
(calc-data)))))))}]) | |
(defn buttn [] | |
[:input {:type "button" | |
:value (if-not (:event/clicked (:events @db-state)) | |
(str "Show query") | |
(str "Hide query")) | |
:on-click | |
#(swap! db-state update-in [:events :event/clicked] not)}]) | |
(defn main [_ & [a' t']] | |
(when (and a' t') ; if both user-params are set, use them | |
(swap! db-state | |
(fn [data] | |
(assoc-in data [:time-data] | |
(-> data | |
(:time-data) | |
(assoc :a a' :t t') | |
(calc-data)))))) | |
(let [{:keys [a t from-date to-date]} (:time-data @db-state)] | |
[:span | |
(if-not (and a' t') | |
[:span | |
t "-day window:" [slider :t t 1 30] | |
a " days back: " [slider :a a 60 1000]] | |
[:span "Query " t "-day window " a " days back ⤵️ "]) | |
(buttn) | |
(when (:event/clicked (:events @db-state)) (parse-query))])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looking.20back.20to.20old.20Daily.20Pages.20in.20Roam.mp4