events.cljs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. (ns frontend.handler.events
  2. (:refer-clojure :exclude [run!])
  3. (:require [frontend.state :as state]
  4. [clojure.core.async :as async]
  5. [frontend.spec :as spec]
  6. [frontend.ui :as ui]
  7. [frontend.util :as util :refer-macros [profile]]
  8. [frontend.config :as config]
  9. [frontend.handler.notification :as notification]
  10. [frontend.components.encryption :as encryption]
  11. [frontend.fs.nfs :as nfs]
  12. [frontend.handler.migrate :as migrate]))
  13. ;; TODO: should we move all events here?
  14. (defn show-install-error!
  15. [repo-url title]
  16. (spec/validate :repos/url repo-url)
  17. (notification/show!
  18. [:p.content
  19. title
  20. " "
  21. [:span.mr-2
  22. (util/format
  23. "Please make sure that you've installed the logseq app for the repo %s on GitHub. "
  24. repo-url)
  25. (ui/button
  26. "Install Logseq on GitHub"
  27. :href (str "https://github.com/apps/" config/github-app-name "/installations/new"))]]
  28. :error
  29. false))
  30. (defmulti handle first)
  31. (defmethod handle :repo/install-error [[_ repo-url title]]
  32. (show-install-error! repo-url title))
  33. (defmethod handle :modal/encryption-setup-dialog [[_ repo-url close-fn]]
  34. (state/set-modal!
  35. (encryption/encryption-setup-dialog repo-url close-fn)))
  36. (defmethod handle :modal/encryption-input-secret-dialog [[_ repo-url db-encrypted-secret close-fn]]
  37. (state/set-modal!
  38. (encryption/encryption-input-secret-dialog
  39. repo-url
  40. db-encrypted-secret
  41. close-fn)))
  42. (defmethod handle :graph/added [[_ repo]]
  43. (js/setTimeout
  44. (fn []
  45. (when (not (:markdown/version (state/get-config)))
  46. (migrate/show-convert-notification! repo)))
  47. 5000))
  48. (defmethod handle :graph/migrated [[_ repo]]
  49. (js/setTimeout
  50. (fn []
  51. (when (not (:markdown/version (state/get-config)))
  52. (migrate/show-migrated-notification! repo)))
  53. 5000))
  54. (defn get-local-repo
  55. []
  56. (when-let [repo (state/get-current-repo)]
  57. (when (config/local-db? repo)
  58. repo)))
  59. (defn ask-permission
  60. [repo]
  61. (when-not (util/electron?)
  62. (fn [close-fn]
  63. [:div
  64. [:p
  65. "Grant native filesystem permission for directory: "
  66. [:b (config/get-local-dir repo)]]
  67. (ui/button
  68. "Grant"
  69. :class "ui__modal-enter"
  70. :on-click (fn []
  71. (nfs/check-directory-permission! repo)
  72. (close-fn)))])))
  73. (defmethod handle :modal/nfs-ask-permission []
  74. (when-let [repo (get-local-repo)]
  75. (state/set-modal! (ask-permission repo))))
  76. (defn run!
  77. []
  78. (let [chan (state/get-events-chan)]
  79. (async/go-loop []
  80. (let [payload (async/<! chan)]
  81. (handle payload))
  82. (recur))
  83. chan))