file_sync.cljs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. (ns frontend.components.file-sync
  2. (:require [cljs-time.coerce :as tc]
  3. [cljs-time.core :as t]
  4. [cljs.core.async :as async]
  5. [cljs.core.async.interop :refer [p->c]]
  6. [clojure.string :as string]
  7. [electron.ipc :as ipc]
  8. [frontend.components.lazy-editor :as lazy-editor]
  9. [frontend.components.onboarding.quick-tour :as quick-tour]
  10. [frontend.components.page :as page]
  11. [frontend.config :as config]
  12. [frontend.db.file-based.model :as file-model]
  13. [frontend.db.model :as db-model]
  14. [frontend.fs :as fs]
  15. [frontend.fs.sync :as fs-sync]
  16. [frontend.handler.file-based.native-fs :as nfs-handler]
  17. [frontend.handler.file-sync :refer [*beta-unavailable?] :as file-sync-handler]
  18. [frontend.handler.notification :as notification]
  19. [frontend.handler.page :as page-handler]
  20. [frontend.handler.repo :as repo-handler]
  21. [frontend.handler.user :as user-handler]
  22. [frontend.mobile.util :as mobile-util]
  23. [frontend.state :as state]
  24. [frontend.storage :as storage]
  25. [frontend.ui :as ui]
  26. [frontend.util :as util]
  27. [frontend.util.fs :as fs-util]
  28. [frontend.util.persist-var :as persist-var]
  29. [goog.functions :refer [debounce]]
  30. [logseq.common.util :as common-util]
  31. [logseq.shui.hooks :as hooks]
  32. [logseq.shui.ui :as shui]
  33. [promesa.core :as p]
  34. [reitit.frontend.easy :as rfe]
  35. [rum.core :as rum]))
  36. (declare maybe-onboarding-show)
  37. (declare open-icloud-graph-clone-picker)
  38. (rum/defc clone-local-icloud-graph-panel
  39. [repo graph-name close-fn]
  40. (hooks/use-effect!
  41. #(some->> (state/sub :file-sync/jstour-inst)
  42. (.complete))
  43. [])
  44. (let [graph-dir (config/get-repo-dir repo)
  45. [selected-path set-selected-path] (rum/use-state "")
  46. selected-path? (and (not (string/blank? selected-path))
  47. (not (mobile-util/in-iCloud-container-path? selected-path)))
  48. on-confirm (fn []
  49. (when-let [dest-dir (and selected-path?
  50. ;; avoid using `util/node-path.join` to join mobile path since it replaces `file:///abc` to `file:/abc`
  51. (str (string/replace selected-path #"/+$" "") "/" graph-name))]
  52. (-> (cond
  53. (util/electron?)
  54. (ipc/ipc :copyDirectory graph-dir dest-dir)
  55. (mobile-util/native-ios?)
  56. (fs/copy! repo graph-dir dest-dir)
  57. :else
  58. nil)
  59. (.then #(do
  60. (notification/show! (str "Cloned to => " dest-dir) :success)
  61. (nfs-handler/ls-dir-files-with-path! dest-dir)
  62. (repo-handler/remove-repo! {:url repo})
  63. (close-fn)))
  64. (.catch #(js/console.error %)))))]
  65. [:div.cp__file-sync-related-normal-modal
  66. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "folders")]]
  67. [:h1.text-xl.font-semibold.opacity-90.text-center.py-2
  68. "Clone your local graph away from " [:strong "☁️"] " iCloud!"]
  69. [:h2.text-center.opacity-70.text-xs.leading-5
  70. "Unfortunately, Logseq Sync and iCloud don't work perfectly together at the moment. To make sure"
  71. [:br]
  72. "You can always delete the remote graph at a later point."]
  73. [:div.folder-tip.flex.flex-col.items-center
  74. [:h3
  75. [:span (ui/icon "folder") [:label.pl-0.5 (common-util/safe-decode-uri-component graph-name)]]]
  76. [:h4.px-6 (config/get-string-repo-dir repo)]
  77. (when (not (string/blank? selected-path))
  78. [:h5.text-xs.pt-1.-mb-1.flex.items-center.leading-none
  79. (if (mobile-util/in-iCloud-container-path? selected-path)
  80. [:span.inline-block.pr-1.text-error.scale-75 (ui/icon "alert-circle")]
  81. [:span.inline-block.pr-1.text-success.scale-75 (ui/icon "circle-check")])
  82. selected-path])
  83. [:div.out-icloud
  84. (ui/button
  85. [:span.inline-flex.items-center.leading-none.opacity-90
  86. "Select new parent folder outside of iCloud" (ui/icon "arrow-right")]
  87. :on-click
  88. (fn []
  89. ;; TODO: support mobile
  90. (cond
  91. (util/electron?)
  92. (p/let [path (ipc/ipc "openDialog")]
  93. (set-selected-path path))
  94. (mobile-util/native-ios?)
  95. (p/let [{:keys [path _localDocumentsPath]}
  96. (p/chain
  97. (.pickFolder mobile-util/folder-picker)
  98. #(js->clj % :keywordize-keys true))]
  99. (set-selected-path path))
  100. :else
  101. nil)))]]
  102. [:p.flex.items-center.space-x-2.pt-6.flex.justify-center.sm:justify-end.-mb-2
  103. (ui/button "Cancel" :background "gray" :class "opacity-50" :on-click close-fn)
  104. (ui/button "Clone graph" :disabled (not selected-path?) :on-click on-confirm)]]))
  105. (rum/defc create-remote-graph-panel
  106. [repo graph-name close-fn]
  107. (hooks/use-effect!
  108. #(some->> (state/sub :file-sync/jstour-inst)
  109. (.complete))
  110. [])
  111. (let [on-confirm
  112. (fn []
  113. (async/go
  114. (close-fn)
  115. (if (mobile-util/in-iCloud-container-path? repo)
  116. (open-icloud-graph-clone-picker repo)
  117. (do
  118. (state/set-state! [:ui/loading? :graph/create-remote?] true)
  119. (when-let [GraphUUID (get (async/<! (file-sync-handler/create-graph graph-name)) 2)]
  120. (async/<! (fs-sync/<sync-start))
  121. (state/set-state! [:ui/loading? :graph/create-remote?] false)
  122. ;; update both local && remote graphs
  123. (state/add-remote-graph! {:GraphUUID GraphUUID
  124. :GraphName graph-name})
  125. (state/set-repos! (map (fn [r]
  126. (if (= (:url r) repo)
  127. (assoc r
  128. :GraphUUID GraphUUID
  129. :GraphName graph-name
  130. :remote? true)
  131. r))
  132. (state/get-repos))))))))]
  133. [:div.cp__file-sync-related-normal-modal
  134. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "cloud-upload" {:size 20})]]
  135. [:h1.text-xl.font-semibold.opacity-90.text-center.py-2
  136. "Are you sure you want to create a new remote graph?"]
  137. [:h2.text-center.opacity-70.text-xs
  138. "By continuing this action you will create an encrypted cloud version of your current local graph." [:br]
  139. "You can always delete the remote graph at a later point."]
  140. [:div.folder-tip.flex.flex-col.items-center
  141. [:h3
  142. [:span (ui/icon "folder") [:label.pl-0.5 graph-name]]
  143. [:span.opacity-50.scale-75 (ui/icon "arrow-right")]
  144. [:span (ui/icon "cloud-lock")]]
  145. [:h4.px-4 (config/get-string-repo-dir repo)]]
  146. [:p.flex.items-center.space-x-2.pt-6.flex.justify-center.sm:justify-end.-mb-2
  147. (ui/button "Cancel" :background "gray" :class "opacity-50" :on-click close-fn)
  148. (ui/button "Create remote graph" :on-click on-confirm)]]))
  149. (rum/defc last-synced-cp < rum/reactive
  150. []
  151. (let [last-synced-at (state/sub [:file-sync/graph-state
  152. (state/get-current-file-sync-graph-uuid)
  153. :file-sync/last-synced-at])
  154. last-synced-at (if last-synced-at
  155. (util/human-time (tc/from-long (* last-synced-at 1000)))
  156. "just now")]
  157. [:div.cl
  158. [:span.opacity-60 "Last change was"]
  159. [:span.pl-1 last-synced-at]]))
  160. (rum/defc sync-now
  161. []
  162. (ui/button "Sync now"
  163. :class "block cursor-pointer"
  164. :small? true
  165. :on-click #(async/offer! fs-sync/immediately-local->remote-chan true)
  166. :style {:color "#ffffff"}))
  167. (def *last-calculated-time (atom nil))
  168. (rum/defc ^:large-vars/cleanup-todo indicator-progress-pane
  169. [sync-state sync-progress
  170. {:keys [idle? syncing? no-active-files? online? history-files? queuing?]}]
  171. (hooks/use-effect!
  172. (fn []
  173. #(reset! *last-calculated-time nil))
  174. [])
  175. (let [uploading-files (:current-local->remote-files sync-state)
  176. downloading-files (:current-remote->local-files sync-state)
  177. uploading? (seq uploading-files)
  178. downloading? (seq downloading-files)
  179. progressing? (or uploading? downloading?)
  180. full-upload-files (:full-local->remote-files sync-state)
  181. full-download-files (:full-remote->local-files sync-state)
  182. calc-progress-total #(cond
  183. uploading? (count full-upload-files)
  184. downloading? (count full-download-files)
  185. :else 0)
  186. calc-progress-finished (fn []
  187. (let [current-sync-files (set
  188. (->> (or (seq full-upload-files) (seq full-download-files))
  189. (map :path)))]
  190. (count (filter #(and (= (:percent (second %)) 100)
  191. (contains? current-sync-files (first %))) sync-progress))))
  192. calc-time-left (fn [] (let [last-calculated-at (:calculated-at @*last-calculated-time)
  193. now (tc/to-epoch (t/now))]
  194. (if (and last-calculated-at (< (- now last-calculated-at) 10))
  195. (:result @*last-calculated-time)
  196. (let [result (file-sync-handler/calculate-time-left sync-state sync-progress)]
  197. (reset! *last-calculated-time {:calculated-at now
  198. :result result})
  199. result))))
  200. p-total (if syncing? (calc-progress-total) 0)
  201. p-finished (if syncing? (calc-progress-finished) 0)
  202. tip-b&p (if (and syncing? progressing?)
  203. [[:span (util/format "%s of %s files" p-finished p-total)]
  204. [:div.progress-bar [:i {:style
  205. {:width (str (if (> p-total 0)
  206. (* (/ p-finished p-total) 100) 0) "%")}}]]]
  207. [[:span.opacity-60 "all file edits"]
  208. (last-synced-cp)])
  209. *el-ref (rum/use-ref nil)
  210. [list-active?, set-list-active?] (rum/use-state
  211. (-> (storage/get :ui/file-sync-active-file-list?)
  212. (#(if (nil? %) true %))))]
  213. (hooks/use-effect!
  214. (fn []
  215. (when-let [^js outer-class-list
  216. (some-> (rum/deref *el-ref)
  217. (.closest ".menu-links-outer")
  218. (.-classList))]
  219. (->> "is-list-active"
  220. (#(if list-active?
  221. (.add outer-class-list %)
  222. (.remove outer-class-list %))))
  223. (storage/set :ui/file-sync-active-file-list? list-active?)))
  224. [list-active?])
  225. (let [idle-&-no-active? (and idle? no-active-files?)
  226. waiting? (not (or (not online?)
  227. idle-&-no-active?
  228. syncing?))]
  229. [:div.cp__file-sync-indicator-progress-pane
  230. {:ref *el-ref
  231. :class (when (and syncing? progressing?) "is-progress-active")}
  232. [:div.a
  233. [:div.al
  234. [:strong
  235. {:class (when idle-&-no-active? "is-no-active")}
  236. (cond
  237. (not online?) (ui/icon "wifi-off")
  238. uploading? (ui/icon "arrow-up")
  239. downloading? (ui/icon "arrow-down")
  240. :else (ui/icon "thumb-up"))]
  241. [:span
  242. (cond
  243. (not online?) "Currently having connection issues..."
  244. idle-&-no-active? "Everything is synced!"
  245. syncing? "Currently syncing your graph..."
  246. :else "Waiting...")]]
  247. [:div.ar
  248. (when queuing? (sync-now))]]
  249. (when-not waiting?
  250. [:div.b.dark:text-gray-200
  251. [:div.bl
  252. [:span.flex.items-center
  253. (if no-active-files?
  254. [:span.opacity-100.pr-1 "Successfully processed"]
  255. [:span.opacity-60.pr-1 "Processed"])]
  256. (first tip-b&p)]
  257. [:div.br
  258. [:small.opacity-50
  259. (when syncing?
  260. (calc-time-left))]]])
  261. [:div.c
  262. {:class (when waiting? "pt-2")}
  263. (second tip-b&p)
  264. (when (or history-files? (not no-active-files?))
  265. [:span.inline-flex.pl-2.active:opacity-50
  266. {:on-click #(set-list-active? (not list-active?))}
  267. (if list-active?
  268. (ui/icon "chevron-up" {:style {:font-size 24}})
  269. (ui/icon "chevron-left" {:style {:font-size 24}}))])]])))
  270. (defn- sort-files
  271. [files]
  272. (sort-by (fn [f] (or (:size f) 0)) > files))
  273. (rum/defcs ^:large-vars/cleanup-todo indicator <
  274. rum/reactive
  275. {:key-fn #(identity "file-sync-indicator")}
  276. {:will-mount (fn [state]
  277. (let [unsub-fn (file-sync-handler/setup-file-sync-event-listeners)]
  278. (assoc state ::unsub-events unsub-fn)))
  279. :will-unmount (fn [state]
  280. (apply (::unsub-events state) nil)
  281. state)}
  282. [_state]
  283. (let [_ (state/sub :auth/id-token)
  284. online? (state/sub :network/online?)
  285. enabled-progress-panel? true
  286. current-repo (state/get-current-repo)
  287. creating-remote-graph? (state/sub [:ui/loading? :graph/create-remote?])
  288. current-graph-id (state/sub-current-file-sync-graph-uuid)
  289. sync-state (state/sub-file-sync-state current-graph-id)
  290. sync-progress (state/sub [:file-sync/graph-state
  291. current-graph-id
  292. :file-sync/progress])
  293. _ (rum/react file-sync-handler/refresh-file-sync-component)
  294. synced-file-graph? (file-sync-handler/synced-file-graph? current-repo)
  295. uploading-files (sort-files (:current-local->remote-files sync-state))
  296. downloading-files (sort-files (:current-remote->local-files sync-state))
  297. queuing-files (:queued-local->remote-files sync-state)
  298. history-files (:history sync-state)
  299. status (:state sync-state)
  300. status (or (nil? status) (keyword (name status)))
  301. off? (fs-sync/sync-off? sync-state)
  302. full-syncing? (contains? #{:local->remote-full-sync :remote->local-full-sync} status)
  303. syncing? (or full-syncing? (contains? #{:local->remote :remote->local} status))
  304. idle? (contains? #{:idle} status)
  305. need-password? (and (contains? #{:need-password} status)
  306. (not (fs-sync/graph-encrypted?)))
  307. queuing? (and idle? (boolean (seq queuing-files)))
  308. no-active-files? (empty? (concat downloading-files queuing-files uploading-files))
  309. create-remote-graph-fn #(when (and current-repo (not (config/demo-graph? current-repo)))
  310. (let [graph-name
  311. (js/decodeURI (util/node-path.basename current-repo))
  312. confirm-fn
  313. (fn [{:keys [close]}]
  314. (create-remote-graph-panel current-repo graph-name close))]
  315. (shui/dialog-open! confirm-fn {:center? true :close-btn? false})))
  316. turn-on (->
  317. (fn []
  318. (when-not (file-sync-handler/current-graph-sync-on?)
  319. (async/go
  320. (let [graphs-txid fs-sync/graphs-txid]
  321. (async/<! (p->c (persist-var/-load graphs-txid)))
  322. (cond
  323. @*beta-unavailable?
  324. (state/pub-event! [:file-sync/onboarding-tip :unavailable])
  325. ;; current graph belong to other user, do nothing
  326. (let [user-uuid (async/<! (user-handler/<user-uuid))
  327. user-uuid (when-not (instance? ExceptionInfo user-uuid) user-uuid)]
  328. (and (first @graphs-txid)
  329. user-uuid
  330. (not (fs-sync/check-graph-belong-to-current-user
  331. user-uuid
  332. (first @graphs-txid)))))
  333. nil
  334. (and (second @graphs-txid)
  335. (fs-sync/graph-sync-off? (second @graphs-txid))
  336. (async/<! (fs-sync/<check-remote-graph-exists (second @graphs-txid))))
  337. (fs-sync/<sync-start)
  338. ;; remote graph already has been deleted, clear repos first, then create-remote-graph
  339. (second @graphs-txid) ; <check-remote-graph-exists -> false
  340. (do (state/set-repos!
  341. (map (fn [r]
  342. (if (= (:url r) current-repo)
  343. (dissoc r :GraphUUID :GraphName :remote?)
  344. r))
  345. (state/get-repos)))
  346. (create-remote-graph-fn))
  347. (second @graphs-txid) ; sync not started yet
  348. nil
  349. :else
  350. (create-remote-graph-fn))))))
  351. (debounce 1500))]
  352. (if creating-remote-graph?
  353. (ui/loading "")
  354. [:div.cp__file-sync-indicator
  355. {:class (util/classnames
  356. [{:is-enabled-progress-pane enabled-progress-panel?
  357. :has-active-files (not no-active-files?)}
  358. (str "status-of-" (and (keyword? status) (name status)))])}
  359. (when (and (not config/publishing?)
  360. (user-handler/logged-in?))
  361. (ui/dropdown-with-links
  362. ;; trigger
  363. (fn [{:keys [toggle-fn]}]
  364. (if (not off?)
  365. [:a.button.cloud.on
  366. {:on-click toggle-fn
  367. :class (util/classnames [{:syncing syncing?
  368. :is-full full-syncing?
  369. :queuing queuing?
  370. :idle (and (not queuing?) idle?)}])}
  371. [:span.flex.items-center
  372. (ui/icon "cloud" {:size ui/icon-size})]]
  373. [:a.button.cloud.off
  374. {:on-click turn-on}
  375. (ui/icon "cloud-off" {:size ui/icon-size})]))
  376. ;; links
  377. (cond-> (vec
  378. (when-not (and no-active-files? idle?)
  379. (cond
  380. need-password?
  381. [{:title [:div.file-item.flex.items-center.leading-none.pt-3
  382. {:style {:margin-left -8}}
  383. (ui/icon "lock" {:size 20}) [:span.pl-1.font-semibold "Password is required"]]
  384. :options {:on-click fs-sync/sync-need-password!}}]
  385. ;; head of upcoming sync
  386. (not no-active-files?)
  387. [{:title [:div.file-item.is-first ""]
  388. :options {:class "is-first-placeholder"}}])))
  389. synced-file-graph?
  390. (concat
  391. (map (fn [f] {:title [:div.file-item
  392. {:key (str "downloading-" f)}
  393. f]
  394. :key (str "downloading-" f)
  395. :icon (if enabled-progress-panel?
  396. (let [progress (get sync-progress f)
  397. percent (or (:percent progress) 0)]
  398. (if (and (number? percent)
  399. (< percent 100))
  400. (ui/indicator-progress-pie percent)
  401. (ui/icon "circle-check")))
  402. (ui/icon "arrow-narrow-down"))}) downloading-files)
  403. (map (fn [e] (let [icon (case (.-type e)
  404. "add" "plus"
  405. "unlink" "minus"
  406. "edit")
  407. path (fs-sync/relative-path e)]
  408. {:title [:div.file-item
  409. {:key (str "queue-" path)}
  410. path]
  411. :key (str "queue-" path)
  412. :icon (ui/icon icon)})) (take 10 queuing-files))
  413. (map (fn [f] {:title [:div.file-item
  414. {:key (str "uploading-" f)}
  415. f]
  416. :key (str "uploading-" f)
  417. :icon (if enabled-progress-panel?
  418. (let [progress (get sync-progress f)
  419. percent (or (:percent progress) 0)]
  420. (if (and (number? percent)
  421. (< percent 100))
  422. (ui/indicator-progress-pie percent)
  423. (ui/icon "circle-check")))
  424. (ui/icon "arrow-up"))}) uploading-files)
  425. (when (seq history-files)
  426. (map-indexed (fn [i f] (:time f)
  427. (when-let [path (:path f)]
  428. (let [full-path (util/node-path.join (config/get-repo-dir current-repo) path)
  429. page-name (file-model/get-file-page full-path)]
  430. {:title [:div.files-history.cursor-pointer
  431. {:key i :class (when (= i 0) "is-first")
  432. :on-click (fn []
  433. (if page-name
  434. (rfe/push-state :page {:name page-name})
  435. (rfe/push-state :file {:path full-path})))}
  436. [:span.file-sync-item (:path f)]
  437. [:div.opacity-50 (ui/humanity-time-ago (:time f) nil)]]})))
  438. (take 10 history-files)))))
  439. ;; options
  440. {:outer-header
  441. [:<>
  442. (indicator-progress-pane
  443. sync-state sync-progress
  444. {:idle? idle?
  445. :syncing? syncing?
  446. :need-password? need-password?
  447. :full-sync? full-syncing?
  448. :online? online?
  449. :queuing? queuing?
  450. :no-active-files? no-active-files?
  451. :history-files? (seq history-files)})
  452. (when (and
  453. (not enabled-progress-panel?)
  454. synced-file-graph? queuing?)
  455. [:div.head-ctls (sync-now)])]}))])))
  456. (rum/defc pick-local-graph-for-sync [graph]
  457. [:div.cp__file-sync-related-normal-modal
  458. [:div.flex.justify-center.pb-4
  459. [:span.icon-wrap (ui/icon "cloud-download" {:size 22})]]
  460. [:h1.mb-5.text-2xl.text-center.font-bold
  461. (util/format "Sync graph \"%s\" to local" (:GraphName graph))]
  462. (ui/button
  463. "Open a local directory"
  464. :class "block w-full mt-4"
  465. :size :lg
  466. :on-click #(do
  467. (state/close-modal!)
  468. (fs-sync/<sync-stop)
  469. (->
  470. (page-handler/ls-dir-files!
  471. (fn [{:keys [url]}]
  472. (file-sync-handler/init-remote-graph url graph)
  473. (js/setTimeout (fn [] (repo-handler/refresh-repos!)) 200))
  474. {:on-open-dir
  475. (fn [result]
  476. (prn ::on-open-dir result)
  477. (let [empty-dir? (not (seq (:files result)))
  478. root (:path result)]
  479. (cond
  480. (string/blank? root)
  481. (p/rejected (js/Error. nil)) ;; cancel pick a directory
  482. empty-dir?
  483. (p/resolved nil)
  484. :else ; dir is not empty
  485. (-> (if (util/electron?)
  486. (ipc/ipc :readGraphTxIdInfo root)
  487. (fs-util/read-graphs-txid-info root))
  488. (p/then (fn [^js info]
  489. (when (or (nil? info)
  490. (nil? (second info))
  491. (not= (second info) (:GraphUUID graph)))
  492. (if (js/confirm "This directory is not empty, are you sure to sync the remote graph to it? Make sure to back up the directory first.")
  493. (p/resolved nil)
  494. (p/rejected (js/Error. nil))))))))))}) ;; cancel pick a non-empty directory
  495. (p/catch (fn [])))))
  496. [:div.text-xs.opacity-50.px-1.flex-row.flex.items-center.p-2
  497. (ui/icon "alert-circle")
  498. [:span.ml-1 " An empty directory or an existing remote graph!"]]])
  499. (defn pick-dest-to-sync-panel [graph]
  500. (fn []
  501. (pick-local-graph-for-sync graph)))
  502. (rum/defc page-history-list
  503. [graph-uuid page-entity set-list-ready? set-page]
  504. (let [[version-files set-version-files] (rum/use-state nil)
  505. [current-page set-current-page] (rum/use-state nil)
  506. [loading? set-loading?] (rum/use-state false)
  507. set-page-fn (fn [page-meta]
  508. (set-current-page page-meta)
  509. (set-page page-meta))
  510. get-version-key #(or (:VersionUUID %) (:relative-path %))]
  511. ;; fetch version files
  512. (hooks/use-effect!
  513. (fn []
  514. (when-not loading?
  515. (async/go
  516. (set-loading? true)
  517. (try
  518. (let [files (async/<! (file-sync-handler/<fetch-page-file-versions graph-uuid page-entity))]
  519. (set-version-files files)
  520. (set-page-fn (first files))
  521. (set-list-ready? true))
  522. (finally (set-loading? false)))))
  523. #())
  524. [])
  525. [:div.version-list
  526. (if loading?
  527. [:div.p-4 (ui/loading)]
  528. (for [version version-files]
  529. (let [version-uuid (get-version-key version)
  530. local? (some? (:relative-path version))]
  531. [:div.version-list-item {:key version-uuid}
  532. [:a.item-link.block.fade-link.flex.justify-between
  533. {:title version-uuid
  534. :class (util/classnames
  535. [{:active (and current-page (= version-uuid (get-version-key current-page)))}])
  536. :on-click #(set-page-fn version)}
  537. [:div.text-sm.pt-1
  538. (ui/humanity-time-ago
  539. (or (:CreateTime version)
  540. (:create-time version)) nil)]
  541. [:small.opacity-50.translate-y-1.flex.items-center.space-x-1
  542. (if local?
  543. [:<> (ui/icon "git-commit") [:span "local"]]
  544. [:<> (ui/icon "cloud") [:span "remote"]])]]])))]))
  545. (rum/defc pick-page-histories-for-sync
  546. [repo-url graph-uuid page-name page-entity]
  547. (let [[selected-page set-selected-page] (rum/use-state nil)
  548. get-version-key #(or (:VersionUUID %) (:relative-path %))
  549. file-uuid (:FileUUID selected-page)
  550. version-uuid (:VersionUUID selected-page)
  551. [version-content set-version-content] (rum/use-state nil)
  552. [list-ready? set-list-ready?] (rum/use-state false)
  553. [content-ready? set-content-ready?] (rum/use-state false)
  554. *ref-contents (rum/use-ref (atom {}))
  555. original-page-name (or (:block/title page-entity) page-name)]
  556. (hooks/use-effect!
  557. #(when selected-page
  558. (set-content-ready? false)
  559. (let [k (get-version-key selected-page)
  560. loaded-contents @(rum/deref *ref-contents)]
  561. (if (contains? loaded-contents k)
  562. (do
  563. (set-version-content (get loaded-contents k))
  564. (js/setTimeout (fn [] (set-content-ready? true)) 100))
  565. ;; without cache
  566. (let [load-file' (fn [repo-url file]
  567. (-> (fs-util/read-repo-file repo-url file)
  568. (p/then
  569. (fn [content]
  570. (set-version-content content)
  571. (set-content-ready? true)
  572. (swap! (rum/deref *ref-contents) assoc k content)))))]
  573. (if (and file-uuid version-uuid)
  574. ;; read remote content
  575. (async/go
  576. (let [downloaded-path (async/<! (file-sync-handler/download-version-file graph-uuid file-uuid version-uuid true))]
  577. (when downloaded-path
  578. (load-file' repo-url downloaded-path))))
  579. ;; read local content
  580. (when-let [relative-path (:relative-path selected-page)]
  581. (load-file' repo-url relative-path)))))))
  582. [selected-page])
  583. (hooks/use-effect!
  584. (fn []
  585. (state/update-state! :editor/hidden-editors #(conj % page-name))
  586. ;; clear effect
  587. (fn []
  588. (state/update-state! :editor/hidden-editors #(disj % page-name))))
  589. [page-name])
  590. [:div.cp__file-sync-page-histories.flex-wrap
  591. {:class (util/classnames [{:is-list-ready list-ready?}])}
  592. [:h1.absolute.top-0.left-0.text-xl.px-4.py-4.leading-4
  593. (ui/icon "history")
  594. " History for page "
  595. [:span.font-medium original-page-name]]
  596. ;; history versions
  597. [:div.cp__file-sync-page-histories-left.flex-wrap
  598. ;; sidebar lists
  599. (page-history-list graph-uuid page-entity set-list-ready? set-selected-page)
  600. ;; content detail
  601. [:article
  602. (when-let [inst-id (and selected-page (get-version-key selected-page))]
  603. (if content-ready?
  604. [:div.relative.raw-content-editor
  605. (lazy-editor/editor
  606. nil inst-id {:data-lang "markdown"}
  607. version-content {:lineWrapping true :readOnly true :lineNumbers true})
  608. [:div.absolute.top-1.right-1.opacity-50.hover:opacity-100
  609. (ui/button "Restore"
  610. :small? true
  611. :on-click #(state/pub-event! [:file-sync-graph/restore-file (state/get-current-repo) page-entity version-content]))]]
  612. [:span.flex.p-15.items-center.justify-center (ui/loading "")]))]]
  613. ;; current version
  614. [:div.cp__file-sync-page-histories-right
  615. [:h1.title.text-xl
  616. "Current version"]
  617. (page/page-blocks-cp page-entity nil)]
  618. ;; ready loading
  619. [:div.flex.items-center.h-full.justify-center.w-full.absolute.ready-loading
  620. (ui/loading)]]))
  621. (defn pick-page-histories-panel [graph-uuid page-name]
  622. (fn []
  623. (if-let [page-entity (db-model/get-page page-name)]
  624. (pick-page-histories-for-sync (state/get-current-repo) graph-uuid page-name page-entity)
  625. (ui/admonition :warning (str "The page (" page-name ") does not exist!")))))
  626. (rum/defc onboarding-welcome-logseq-sync
  627. [close-fn]
  628. (let [[loading? set-loading?] (rum/use-state false)]
  629. [:div.cp__file-sync-welcome-logseq-sync
  630. [:span.head-bg
  631. [:strong "CLOSED BETA"]]
  632. [:h1.text-2xl.font-bold.flex-col.sm:flex-row
  633. [:span.opacity-80 "Welcome to "]
  634. [:span.pl-2.dark:text-white.text-gray-800 "Logseq Sync! 👋"]]
  635. [:h2
  636. "No more cloud storage worries. With Logseq's encrypted file syncing, "
  637. [:br]
  638. "you'll always have your notes backed up and available in real-time on any device."]
  639. [:div.pt-6.flex.justify-center.space-x-2.sm:justify-end
  640. (ui/button "Later" :on-click close-fn :background "gray" :class "opacity-60")
  641. (ui/button "Start syncing"
  642. :disabled loading?
  643. :on-click (fn []
  644. (set-loading? true)
  645. (let [result (:user/info @state/state)
  646. ex-time (:ExpireTime result)]
  647. (if (and (number? ex-time)
  648. (< (* ex-time 1000) (js/Date.now)))
  649. (do
  650. (vreset! *beta-unavailable? true)
  651. (maybe-onboarding-show :unavailable))
  652. ;; Logseq sync available
  653. (maybe-onboarding-show :sync-initiate))
  654. (close-fn)
  655. (set-loading? false))))]]))
  656. (rum/defc onboarding-unavailable-file-sync
  657. [close-fn]
  658. [:div.cp__file-sync-unavailable-logseq-sync
  659. [:span.head-bg]
  660. [:h1.text-2xl.font-bold
  661. [:span.pr-2.dark:text-white.text-gray-800 "Logseq Sync"]
  662. [:span.opacity-80 "is not yet available for you. 😔 "]]
  663. [:h2
  664. "Thanks for creating an account! To ensure that our file syncing service runs well when we release it"
  665. [:br]
  666. "to our users, we need a little more time to test it. That’s why we decided to first roll it out only to our "
  667. [:br]
  668. "charitable OpenCollective sponsors and backers. We can notify you once it becomes available for you."]
  669. [:div.pt-6.flex.justify-end.space-x-2
  670. (ui/button "Close" :on-click close-fn :background "gray" :class "opacity-60")]])
  671. (rum/defc onboarding-congrats-successful-sync
  672. [close-fn]
  673. [:div.cp__file-sync-related-normal-modal
  674. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "checkup-list" {:size 28})]]
  675. [:h1.text-xl.font-semibold.opacity-90.text-center.py-2
  676. [:span.dark:opacity-80 "Congrats on your first successful sync!"]]
  677. [:h2.text-center.dark:opacity-70.text-sm.opacity-90
  678. [:div "By using this graph with Logseq Sync you can now transition seamlessly between your different "]
  679. [:div
  680. [:span "devices. Go to the "]
  681. [:span.dark:text-white "All Graphs "]
  682. [:span "pages to manage your remote graph or switch to another local graph "]]
  683. [:div "and sync it as well."]]
  684. [:div.cloud-tip.rounded-md.mt-6.py-4
  685. [:div.items-center.opacity-90.flex.justify-center
  686. [:span.pr-2.flex (ui/icon "bell-ringing" {:class "font-semibold"})]
  687. [:strong "Logseq Sync is still in Beta and we're working on a Pro plan!"]]]
  688. ;; [:ul.flex.py-6.px-4
  689. ;; [:li.it
  690. ;; [:h1.dark:text-white "10"]
  691. ;; [:h2 "Remote Graphs"]]
  692. ;; [:li.it
  693. ;; [:h1.dark:text-white "5G"]
  694. ;; [:h2 "Storage per Graph"]]
  695. ;; [:li.it
  696. ;; [:h1.dark:text-white "50G"]
  697. ;; [:h2 "Total Storage"]]]
  698. [:div.pt-6.flex.justify-end.space-x-2
  699. (ui/button "Done" :on-click close-fn)]])
  700. (defn open-icloud-graph-clone-picker
  701. ([] (open-icloud-graph-clone-picker (state/get-current-repo)))
  702. ([repo]
  703. (when (and repo (mobile-util/in-iCloud-container-path? repo))
  704. (shui/dialog-open!
  705. (fn [close-fn]
  706. (clone-local-icloud-graph-panel repo (util/node-path.basename repo) close-fn))
  707. {:close-btn? false}))))
  708. (defn make-onboarding-panel
  709. [type]
  710. (fn [{:keys [close]}]
  711. (case type
  712. :welcome
  713. (onboarding-welcome-logseq-sync close)
  714. :unavailable
  715. (onboarding-unavailable-file-sync close)
  716. :congrats
  717. (onboarding-congrats-successful-sync close)
  718. [:p
  719. [:h1.text-xl.font-bold "Not handled!"]
  720. [:a.button {:on-click close} "Got it!"]])))
  721. (defn maybe-onboarding-show
  722. [type]
  723. (when-not (get (state/sub :file-sync/onboarding-state) (keyword type))
  724. (try
  725. (let [current-repo (state/get-current-repo)
  726. demo-repo? (= current-repo config/demo-repo)
  727. login? (boolean (state/sub :auth/id-token))]
  728. (when login?
  729. (case type
  730. :welcome
  731. (when (or demo-repo?
  732. (:GraphUUID (repo-handler/get-detail-graph-info current-repo)))
  733. (throw (js/Error. "current repo have been local or remote graph")))
  734. (:sync-initiate :sync-learn :sync-history)
  735. (do (quick-tour/ready
  736. (fn []
  737. (quick-tour/start-file-sync type)
  738. (state/set-state! [:file-sync/onboarding-state type] true)))
  739. (throw (js/Error. nil)))
  740. :default)
  741. (state/pub-event! [:file-sync/onboarding-tip type])
  742. (state/set-state! [:file-sync/onboarding-state (keyword type)] true)))
  743. (catch :default e
  744. (js/console.warn "[onboarding SKIP] " (name type) e)))))