handler.cljs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. (ns frontend.handler
  2. "Main ns that handles application startup. Closest ns that we have to a
  3. system. Contains a couple of small system components"
  4. (:require [cljs.reader :refer [read-string]]
  5. [clojure.string :as string]
  6. [electron.ipc :as ipc]
  7. [electron.listener :as el]
  8. [frontend.components.block :as block]
  9. [frontend.components.editor :as editor]
  10. [frontend.components.page :as page]
  11. [frontend.components.reference :as reference]
  12. [frontend.components.whiteboard :as whiteboard]
  13. [frontend.config :as config]
  14. [frontend.context.i18n :as i18n :refer [t]]
  15. [frontend.db :as db]
  16. [frontend.db.conn :as conn]
  17. [frontend.db.persist :as db-persist]
  18. [frontend.db.react :as react]
  19. [frontend.error :as error]
  20. [frontend.extensions.srs :as srs]
  21. [frontend.handler.command-palette :as command-palette]
  22. [frontend.handler.events :as events]
  23. [frontend.handler.file :as file-handler]
  24. [frontend.handler.notification :as notification]
  25. [frontend.handler.page :as page-handler]
  26. [frontend.handler.repo :as repo-handler]
  27. [frontend.handler.ui :as ui-handler]
  28. [frontend.handler.user :as user-handler]
  29. [frontend.handler.repo-config :as repo-config-handler]
  30. [frontend.handler.global-config :as global-config-handler]
  31. [frontend.handler.metadata :as metadata-handler]
  32. [frontend.idb :as idb]
  33. [frontend.mobile.util :as mobile-util]
  34. [frontend.modules.instrumentation.core :as instrument]
  35. [frontend.modules.outliner.datascript :as outliner-db]
  36. [frontend.modules.outliner.file :as file]
  37. [frontend.modules.shortcut.core :as shortcut]
  38. [frontend.state :as state]
  39. [frontend.ui :as ui]
  40. [frontend.util :as util]
  41. [frontend.util.persist-var :as persist-var]
  42. [goog.object :as gobj]
  43. [lambdaisland.glogi :as log]
  44. [promesa.core :as p]))
  45. (defn set-global-error-notification!
  46. []
  47. (set! js/window.onerror
  48. (fn [message, _source, _lineno, _colno, error]
  49. (when-not (error/ignored? message)
  50. (log/error :exception error)
  51. ;; (notification/show!
  52. ;; (str "message=" message "\nsource=" source "\nlineno=" lineno "\ncolno=" colno "\nerror=" error)
  53. ;; :error
  54. ;; ;; Don't auto-hide
  55. ;; false)
  56. ))))
  57. (defn- watch-for-date!
  58. []
  59. (let [f (fn []
  60. #_:clj-kondo/ignore
  61. (let [repo (state/get-current-repo)]
  62. (when (and (not (state/nfs-refreshing?))
  63. (not (contains? (:file/unlinked-dirs @state/state)
  64. (config/get-repo-dir repo))))
  65. ;; Don't create the journal file until user writes something
  66. (page-handler/create-today-journal!))))]
  67. (f)
  68. (js/setInterval f 5000)))
  69. (defn- instrument!
  70. []
  71. (let [total (srs/get-srs-cards-total)]
  72. (state/set-state! :srs/cards-due-count total)
  73. (state/pub-event! [:instrument {:type :flashcards/count
  74. :payload {:total (or total 0)}}])
  75. (state/pub-event! [:instrument {:type :blocks/count
  76. :payload {:total (db/blocks-count)}}])))
  77. (defn restore-and-setup!
  78. [repos]
  79. (when-let [repo (or (state/get-current-repo) (:url (first repos)))]
  80. (-> (db/restore! repo)
  81. (p/then
  82. (fn []
  83. ;; try to load custom css only for current repo
  84. (ui-handler/add-style-if-exists!)
  85. (->
  86. (p/do! (repo-config-handler/start {:repo repo})
  87. (when (config/global-config-enabled?)
  88. (global-config-handler/start {:repo repo})))
  89. (p/finally
  90. (fn []
  91. ;; install after config is restored
  92. (shortcut/unlisten-all)
  93. (shortcut/refresh!)
  94. (cond
  95. (and (not (seq (db/get-files config/local-repo)))
  96. ;; Not native local directory
  97. (not (some config/local-db? (map :url repos)))
  98. (not (mobile-util/native-platform?)))
  99. ;; will execute `(state/set-db-restoring! false)` inside
  100. (repo-handler/setup-local-repo-if-not-exists!)
  101. :else
  102. (state/set-db-restoring! false)))))))
  103. (p/then
  104. (fn []
  105. (js/console.log "db restored, setting up repo hooks")
  106. (state/pub-event! [:modal/nfs-ask-permission])
  107. (page-handler/init-commands!)
  108. (watch-for-date!)
  109. (file-handler/watch-for-current-graph-dir!)
  110. (state/pub-event! [:graph/restored (state/get-current-repo)])))
  111. (p/catch (fn [error]
  112. (log/error :exception error))))))
  113. (defn- handle-connection-change
  114. [e]
  115. (let [online? (= (gobj/get e "type") "online")]
  116. (state/set-online! online?)))
  117. (defn set-network-watcher!
  118. []
  119. (js/window.addEventListener "online" handle-connection-change)
  120. (js/window.addEventListener "offline" handle-connection-change))
  121. (defn enable-datalog-console
  122. "Enables datalog console in browser provided by https://github.com/homebaseio/datalog-console"
  123. []
  124. (js/document.documentElement.setAttribute "__datalog-console-remote-installed__" true)
  125. (.addEventListener js/window "message"
  126. (fn [event]
  127. (let [db (conn/get-db)]
  128. (when-let [devtool-message (gobj/getValueByKeys event "data" ":datalog-console.client/devtool-message")]
  129. (let [msg-type (:type (read-string devtool-message))]
  130. (case msg-type
  131. :datalog-console.client/request-whole-database-as-string
  132. (.postMessage js/window #js {":datalog-console.remote/remote-message" (pr-str db)} "*")
  133. nil)))))))
  134. (defn clear-cache!
  135. []
  136. (notification/show! "Clearing..." :warning false)
  137. (p/let [_ (when (util/electron?)
  138. (ipc/ipc "clearCache"))
  139. _ (idb/clear-local-storage-and-idb!)]
  140. (js/setTimeout
  141. (fn [] (if (util/electron?)
  142. (ipc/ipc :reloadWindowPage)
  143. (js/window.location.reload)))
  144. 2000)))
  145. ;; FIXME: Another get-repos implementation at src\main\frontend\handler\repo.cljs
  146. (defn- get-repos
  147. []
  148. (p/let [nfs-dbs (db-persist/get-all-graphs)]
  149. ;; TODO: Better IndexDB migration handling
  150. (cond
  151. (and (mobile-util/native-platform?)
  152. (some #(or (string/includes? % " ")
  153. (string/includes? % "logseq_local_/")) nfs-dbs))
  154. (do (notification/show! ["DB version is not compatible, please clear cache then re-add your graph back."
  155. (ui/button
  156. (t :settings-page/clear-cache)
  157. :class "ui__modal-enter"
  158. :class "text-sm p-1"
  159. :on-click clear-cache!)] :error false)
  160. {:url config/local-repo
  161. :example? true})
  162. (seq nfs-dbs)
  163. (map (fn [db] {:url db :nfs? true}) nfs-dbs)
  164. :else
  165. [{:url config/local-repo
  166. :example? true}])))
  167. (defn- register-components-fns!
  168. []
  169. (state/set-page-blocks-cp! page/page-blocks-cp)
  170. (state/set-component! :block/linked-references reference/block-linked-references)
  171. (state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
  172. (state/set-component! :block/single-block block/single-block-cp)
  173. (state/set-component! :editor/box editor/box)
  174. (command-palette/register-global-shortcut-commands))
  175. (reset! db/*db-listener outliner-db/after-transact-pipelines)
  176. (defn start!
  177. [render]
  178. (set-global-error-notification!)
  179. (register-components-fns!)
  180. (state/set-db-restoring! true)
  181. (render)
  182. (i18n/start)
  183. (instrument/init)
  184. (state/set-online! js/navigator.onLine)
  185. (set-network-watcher!)
  186. (util/indexeddb-check?
  187. (fn [_error]
  188. (notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
  189. (state/set-indexedb-support! false)))
  190. (react/run-custom-queries-when-idle!)
  191. (events/run!)
  192. (-> (p/let [repos (get-repos)]
  193. (state/set-repos! repos)
  194. (restore-and-setup! repos))
  195. (p/catch (fn [e]
  196. (js/console.error "Error while restoring repos: " e)))
  197. (p/finally (fn []
  198. (state/set-db-restoring! false))))
  199. (when (mobile-util/native-platform?)
  200. (mobile-util/hide-splash))
  201. (db/run-batch-txs!)
  202. (file/<ratelimit-file-writes!)
  203. (when config/dev?
  204. (enable-datalog-console))
  205. (when (util/electron?)
  206. (el/listen!))
  207. (persist-var/load-vars)
  208. (user-handler/restore-tokens-from-localstorage)
  209. (user-handler/refresh-tokens-loop)
  210. (metadata-handler/run-set-page-metadata-job!)
  211. (js/setTimeout instrument! (* 60 1000)))
  212. (defn stop! []
  213. (prn "stop!"))
  214. (defn quit-and-install-new-version!
  215. []
  216. (p/let [_ (el/persist-dbs!)
  217. _ (ipc/invoke "set-quit-dirty-state" false)]
  218. (ipc/ipc :quitAndInstall)))