handler.cljs 9.9 KB

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