file_sync.cljs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. (ns frontend.components.file-sync
  2. (:require [cljs.core.async :as async]
  3. [cljs.core.async.interop :refer [p->c]]
  4. [frontend.util.persist-var :as persist-var]
  5. [clojure.string :as string]
  6. [electron.ipc :as ipc]
  7. [frontend.components.lazy-editor :as lazy-editor]
  8. [frontend.components.onboarding.quick-tour :as quick-tour]
  9. [frontend.components.page :as page]
  10. [frontend.config :as config]
  11. [frontend.db :as db]
  12. [frontend.db.model :as db-model]
  13. [frontend.fs :as fs]
  14. [frontend.fs.sync :as fs-sync]
  15. [frontend.handler.file-sync :refer [*beta-unavailable?] :as file-sync-handler]
  16. [frontend.handler.notification :as notification]
  17. [frontend.handler.page :as page-handler]
  18. [frontend.handler.repo :as repo-handler]
  19. [frontend.handler.user :as user-handler]
  20. [frontend.handler.web.nfs :as web-nfs]
  21. [frontend.mobile.util :as mobile-util]
  22. [frontend.state :as state]
  23. [frontend.ui :as ui]
  24. [frontend.util :as util]
  25. [frontend.util.fs :as fs-util]
  26. [frontend.storage :as storage]
  27. [logseq.graph-parser.config :as gp-config]
  28. [promesa.core :as p]
  29. [reitit.frontend.easy :as rfe]
  30. [rum.core :as rum]
  31. [cljs-time.core :as t]
  32. [cljs-time.coerce :as tc]
  33. [goog.functions :refer [debounce]]))
  34. (declare maybe-onboarding-show)
  35. (declare open-icloud-graph-clone-picker)
  36. (rum/defc clone-local-icloud-graph-panel
  37. [repo graph-name close-fn]
  38. (rum/use-effect!
  39. #(some->> (state/sub :file-sync/jstour-inst)
  40. (.complete))
  41. [])
  42. (let [graph-dir (config/get-repo-dir repo)
  43. [selected-path set-selected-path] (rum/use-state "")
  44. selected-path? (and (not (string/blank? selected-path))
  45. (not (mobile-util/iCloud-container-path? selected-path)))
  46. on-confirm (fn []
  47. (when-let [dest-dir (and selected-path?
  48. ;; avoid using `util/node-path.join` to join mobile path since it replaces `file:///abc` to `file:/abc`
  49. (str (string/replace selected-path #"/+$" "") "/" graph-name))]
  50. (-> (cond
  51. (util/electron?)
  52. (ipc/ipc :copyDirectory graph-dir dest-dir)
  53. (mobile-util/native-ios?)
  54. (fs/copy! repo graph-dir dest-dir)
  55. :else
  56. nil)
  57. (.then #(do
  58. (notification/show! (str "Cloned to => " dest-dir) :success)
  59. (web-nfs/ls-dir-files-with-path! dest-dir)
  60. (repo-handler/remove-repo! {:url repo})
  61. (close-fn)))
  62. (.catch #(js/console.error %)))))]
  63. [:div.cp__file-sync-related-normal-modal
  64. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "folders")]]
  65. [:h1.text-xl.font-semibold.opacity-90.text-center.py-2
  66. "Clone your local graph away from " [:strong "☁️"] " iCloud!"]
  67. [:h2.text-center.opacity-70.text-xs.leading-5
  68. "Unfortunately, Logseq Sync and iCloud don't work perfectly together at the moment. To make sure"
  69. [:br]
  70. "You can always delete the remote graph at a later point."]
  71. [:div.folder-tip.flex.flex-col.items-center
  72. [:h3
  73. [:span (ui/icon "folder") [:label.pl-0.5 (js/decodeURIComponent graph-name)]]]
  74. [:h4.px-6 (config/get-string-repo-dir repo)]
  75. (when (not (string/blank? selected-path))
  76. [:h5.text-xs.pt-1.-mb-1.flex.items-center.leading-none
  77. (if (mobile-util/iCloud-container-path? selected-path)
  78. [:span.inline-block.pr-1.text-error.scale-75 (ui/icon "alert-circle")]
  79. [:span.inline-block.pr-1.text-success.scale-75 (ui/icon "circle-check")])
  80. selected-path])
  81. [:div.out-icloud
  82. (ui/button
  83. [:span.inline-flex.items-center.leading-none.opacity-90
  84. "Select new parent folder outside of iCloud" (ui/icon "arrow-right")]
  85. :on-click
  86. (fn []
  87. ;; TODO: support mobile
  88. (cond
  89. (util/electron?)
  90. (p/let [path (ipc/ipc "openDialog")]
  91. (set-selected-path path))
  92. (mobile-util/native-ios?)
  93. (p/let [{:keys [path _localDocumentsPath]}
  94. (p/chain
  95. (.pickFolder mobile-util/folder-picker)
  96. #(js->clj % :keywordize-keys true))]
  97. (set-selected-path path))
  98. :else
  99. nil)))]]
  100. [:p.flex.items-center.space-x-2.pt-6.flex.justify-center.sm:justify-end.-mb-2
  101. (ui/button "Cancel" :background "gray" :class "opacity-50" :on-click close-fn)
  102. (ui/button "Clone graph" :disabled (not selected-path?) :on-click on-confirm)]]))
  103. (rum/defc create-remote-graph-panel
  104. [repo graph-name close-fn]
  105. (rum/use-effect!
  106. #(some->> (state/sub :file-sync/jstour-inst)
  107. (.complete))
  108. [])
  109. (let [on-confirm
  110. (fn []
  111. (async/go
  112. (close-fn)
  113. (if (mobile-util/iCloud-container-path? repo)
  114. (open-icloud-graph-clone-picker repo)
  115. (do
  116. (state/set-state! [:ui/loading? :graph/create-remote?] true)
  117. (when-let [GraphUUID (get (async/<! (file-sync-handler/create-graph graph-name)) 2)]
  118. (async/<! (fs-sync/sync-start))
  119. (state/set-state! [:ui/loading? :graph/create-remote?] false)
  120. ;; update existing repo
  121. (state/set-repos! (map (fn [r]
  122. (if (= (:url r) repo)
  123. (assoc r
  124. :GraphUUID GraphUUID
  125. :GraphName graph-name
  126. :remote? true)
  127. r))
  128. (state/get-repos))))))))]
  129. [:div.cp__file-sync-related-normal-modal
  130. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "cloud-upload" {:size 20})]]
  131. [:h1.text-xl.font-semibold.opacity-90.text-center.py-2
  132. "Are you sure you want to create a new remote graph?"]
  133. [:h2.text-center.opacity-70.text-xs
  134. "By continuing this action you will create an encrypted cloud version of your current local graph." [:br]
  135. "You can always delete the remote graph at a later point."]
  136. [:div.folder-tip.flex.flex-col.items-center
  137. [:h3
  138. [:span (ui/icon "folder") [:label.pl-0.5 graph-name]]
  139. [:span.opacity-50.scale-75 (ui/icon "arrow-right")]
  140. [:span (ui/icon "cloud-lock")]]
  141. [:h4.px-4 (config/get-string-repo-dir repo)]]
  142. [:p.flex.items-center.space-x-2.pt-6.flex.justify-center.sm:justify-end.-mb-2
  143. (ui/button "Cancel" :background "gray" :class "opacity-50" :on-click close-fn)
  144. (ui/button "Create remote graph" :on-click on-confirm)]]))
  145. (rum/defc indicator-progress-pie
  146. [percentage]
  147. (let [*el (rum/use-ref nil)]
  148. (rum/use-effect!
  149. #(when-let [^js el (rum/deref *el)]
  150. (set! (.. el -style -backgroundImage)
  151. (util/format "conic-gradient(var(--ls-pie-fg-color) %s%, var(--ls-pie-bg-color) %s%)" percentage percentage)))
  152. [percentage])
  153. [:span.cp__file-sync-indicator-progress-pie {:ref *el}]))
  154. (rum/defc last-synced-cp < rum/reactive
  155. []
  156. (let [last-synced-at (state/sub [:file-sync/graph-state
  157. (state/get-current-file-sync-graph-uuid)
  158. :file-sync/last-synced-at])
  159. last-synced-at (if last-synced-at
  160. (util/time-ago (tc/from-long (* last-synced-at 1000)))
  161. "just now")]
  162. [:div.cl
  163. [:span.opacity-60 "Last change was"]
  164. [:span.pl-1 last-synced-at]]))
  165. (rum/defc sync-now
  166. []
  167. (ui/button "Sync now"
  168. :class "block cursor-pointer"
  169. :small? true
  170. :on-click #(async/offer! fs-sync/immediately-local->remote-chan true)
  171. :style {:color "#ffffff"}))
  172. (def *last-calculated-time (atom nil))
  173. (rum/defc ^:large-vars/cleanup-todo indicator-progress-pane
  174. [sync-state sync-progress
  175. {:keys [idle? syncing? no-active-files? online? history-files? queuing?]}]
  176. (rum/use-effect!
  177. (fn []
  178. #(reset! *last-calculated-time nil))
  179. [])
  180. (let [uploading-files (:current-local->remote-files sync-state)
  181. downloading-files (:current-remote->local-files sync-state)
  182. uploading? (seq uploading-files)
  183. downloading? (seq downloading-files)
  184. progressing? (or uploading? downloading?)
  185. full-upload-files (:full-local->remote-files sync-state)
  186. full-download-files (:full-remote->local-files sync-state)
  187. calc-progress-total #(cond
  188. uploading? (count full-upload-files)
  189. downloading? (count full-download-files)
  190. :else 0)
  191. calc-progress-finished (fn []
  192. (let [current-sync-files (set
  193. (->> (or (seq full-upload-files) (seq full-download-files))
  194. (map :path)))]
  195. (count (filter #(and (= (:percent (second %)) 100)
  196. (contains? current-sync-files (first %))) sync-progress))))
  197. calc-time-left (fn [] (let [last-calculated-at (:calculated-at @*last-calculated-time)
  198. now (tc/to-epoch (t/now))]
  199. (if (and last-calculated-at (< (- now last-calculated-at) 10))
  200. (:result @*last-calculated-time)
  201. (let [result (file-sync-handler/calculate-time-left sync-state sync-progress)]
  202. (reset! *last-calculated-time {:calculated-at now
  203. :result result})
  204. result))))
  205. p-total (if syncing? (calc-progress-total) 0)
  206. p-finished (if syncing? (calc-progress-finished) 0)
  207. tip-b&p (if (and syncing? progressing?)
  208. [[:span (util/format "%s of %s files" p-finished p-total)]
  209. [:div.progress-bar [:i {:style
  210. {:width (str (if (> p-total 0)
  211. (* (/ p-finished p-total) 100) 0) "%")}}]]]
  212. [[:span.opacity-60 "all file edits"]
  213. (last-synced-cp)])
  214. *el-ref (rum/use-ref nil)
  215. [list-active?, set-list-active?] (rum/use-state
  216. (-> (storage/get :ui/file-sync-active-file-list?)
  217. (#(if (nil? %) true %))))]
  218. (rum/use-effect!
  219. (fn []
  220. (when-let [^js outer-class-list
  221. (some-> (rum/deref *el-ref)
  222. (.closest ".menu-links-outer")
  223. (.-classList))]
  224. (->> "is-list-active"
  225. (#(if list-active?
  226. (.add outer-class-list %)
  227. (.remove outer-class-list %))))
  228. (storage/set :ui/file-sync-active-file-list? list-active?)))
  229. [list-active?])
  230. [:div.cp__file-sync-indicator-progress-pane
  231. {:ref *el-ref
  232. :class (when (and syncing? progressing?) "is-progress-active")}
  233. (let [idle-&-no-active? (and idle? no-active-files?)]
  234. [:div.a
  235. [:div.al
  236. [:strong
  237. {:class (when idle-&-no-active? "is-no-active")}
  238. (cond
  239. (not online?) (ui/icon "wifi-off")
  240. uploading? (ui/icon "arrow-up")
  241. downloading? (ui/icon "arrow-down")
  242. :else (ui/icon "thumb-up"))]
  243. [:span
  244. (cond
  245. (not online?) "Currently having connection issues..."
  246. idle-&-no-active? "Everything is synced!"
  247. syncing? "Currently syncing your graph..."
  248. :else "Waiting..."
  249. )]]
  250. [:div.ar
  251. (when queuing? (sync-now))]])
  252. [:div.b.dark:text-gray-200
  253. [:div.bl
  254. [:span.flex.items-center
  255. (if no-active-files?
  256. [:span.opacity-100.pr-1 "Successfully processed"]
  257. [:span.opacity-60.pr-1 "Processed"])]
  258. (first tip-b&p)]
  259. [:div.br
  260. [:small.opacity-50
  261. (when syncing?
  262. (calc-time-left))]]]
  263. [:div.c
  264. (second tip-b&p)
  265. (when (or history-files? (not no-active-files?))
  266. [:span.inline-flex.ml-1.active:opacity-50
  267. {:on-click #(set-list-active? (not list-active?))}
  268. (if list-active?
  269. (ui/icon "chevron-up" {:style {:font-size 24}})
  270. (ui/icon "chevron-left" {:style {:font-size 24}}))])]]))
  271. (defn- sort-files
  272. [files]
  273. (sort-by (fn [f] (or (:size f) 0)) > files))
  274. (rum/defcs ^:large-vars/cleanup-todo indicator <
  275. rum/reactive
  276. {:key-fn #(identity "file-sync-indicator")}
  277. {:will-mount (fn [state]
  278. (let [unsub-fn (file-sync-handler/setup-file-sync-event-listeners)]
  279. (assoc state ::unsub-events unsub-fn)))
  280. :will-unmount (fn [state]
  281. (apply (::unsub-events state) nil)
  282. state)}
  283. [_state]
  284. (let [_ (state/sub :auth/id-token)
  285. online? (state/sub :network/online?)
  286. enabled-progress-panel? true
  287. current-repo (state/get-current-repo)
  288. creating-remote-graph? (state/sub [:ui/loading? :graph/create-remote?])
  289. current-graph-id (state/sub-current-file-sync-graph-uuid)
  290. sync-state (state/sub-file-sync-state current-graph-id)
  291. sync-progress (state/sub [:file-sync/graph-state
  292. current-graph-id
  293. :file-sync/progress])
  294. _ (rum/react file-sync-handler/refresh-file-sync-component)
  295. synced-file-graph? (file-sync-handler/synced-file-graph? current-repo)
  296. uploading-files (sort-files (:current-local->remote-files sync-state))
  297. downloading-files (sort-files (:current-remote->local-files sync-state))
  298. queuing-files (:queued-local->remote-files sync-state)
  299. history-files (:history sync-state)
  300. status (:state sync-state)
  301. status (or (nil? status) (keyword (name status)))
  302. off? (fs-sync/sync-off? sync-state)
  303. full-syncing? (contains? #{:local->remote-full-sync :remote->local-full-sync} status)
  304. syncing? (or full-syncing? (contains? #{:local->remote :remote->local} status))
  305. idle? (contains? #{:idle} status)
  306. need-password? (and (contains? #{:need-password} status)
  307. (not (fs-sync/graph-encrypted?)))
  308. queuing? (and idle? (boolean (seq queuing-files)))
  309. no-active-files? (empty? (concat downloading-files queuing-files uploading-files))
  310. create-remote-graph-fn #(when (and current-repo (not (config/demo-graph? current-repo)))
  311. (let [graph-name
  312. (js/decodeURI (util/node-path.basename current-repo))
  313. confirm-fn
  314. (fn [close-fn]
  315. (create-remote-graph-panel current-repo graph-name close-fn))]
  316. (state/set-modal! confirm-fn {:center? true :close-btn? false})))
  317. turn-on (->
  318. (fn []
  319. (when-not (file-sync-handler/current-graph-sync-on?)
  320. (async/go
  321. (let [graphs-txid fs-sync/graphs-txid]
  322. (async/<! (p->c (persist-var/-load graphs-txid)))
  323. (cond
  324. @*beta-unavailable?
  325. (state/pub-event! [:file-sync/onboarding-tip :unavailable])
  326. ;; current graph belong to other user, do nothing
  327. (and (first @graphs-txid)
  328. (not (fs-sync/check-graph-belong-to-current-user (user-handler/user-uuid)
  329. (first @graphs-txid))))
  330. nil
  331. (and synced-file-graph?
  332. (second @graphs-txid)
  333. (fs-sync/graph-sync-off? (second @graphs-txid))
  334. (async/<! (fs-sync/<check-remote-graph-exists (second @graphs-txid))))
  335. (do
  336. (prn "sync start")
  337. (fs-sync/sync-start))
  338. ;; remote graph already has been deleted, clear repos first, then create-remote-graph
  339. synced-file-graph? ; <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-> []
  378. synced-file-graph?
  379. (concat
  380. (when-not (and no-active-files? idle?)
  381. (cond
  382. need-password?
  383. [{:title [:div.file-item
  384. (ui/icon "lock") "Password is required"]
  385. :options {:on-click fs-sync/sync-need-password!}}]
  386. ;; head of upcoming sync
  387. (not no-active-files?)
  388. [{:title [:div.file-item.is-first ""]
  389. :options {:class "is-first-placeholder"}}]))
  390. (map (fn [f] {:title [:div.file-item
  391. {:key (str "downloading-" f)}
  392. (js/decodeURIComponent f)]
  393. :key (str "downloading-" f)
  394. :icon (if enabled-progress-panel?
  395. (let [progress (get sync-progress f)
  396. percent (or (:percent progress) 0)]
  397. (if (and (number? percent)
  398. (< percent 100))
  399. (indicator-progress-pie percent)
  400. (ui/icon "circle-check")))
  401. (ui/icon "arrow-narrow-down"))
  402. }) 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. (js/decodeURIComponent 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. (js/decodeURIComponent 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. (indicator-progress-pie percent)
  423. (ui/icon "circle-check")))
  424. (ui/icon "arrow-up"))
  425. }) uploading-files)
  426. (when (seq history-files)
  427. (map-indexed (fn [i f] (:time f)
  428. (let [path (:path f)
  429. ext (string/lower-case (util/get-file-ext path))
  430. _supported? (gp-config/mldoc-support? ext)
  431. full-path (util/node-path.join (config/get-repo-dir current-repo) path)
  432. page-name (db/get-file-page full-path)]
  433. {:title [:div.files-history.cursor-pointer
  434. {:key i :class (when (= i 0) "is-first")
  435. :on-click (fn []
  436. (if page-name
  437. (rfe/push-state :page {:name page-name})
  438. (rfe/push-state :file {:path full-path})))}
  439. [:span.file-sync-item (js/decodeURIComponent (:path f))]
  440. [:div.opacity-50 (ui/humanity-time-ago (:time f) nil)]]}))
  441. (take 10 history-files)))))
  442. ;; options
  443. {:outer-header
  444. [:<>
  445. (indicator-progress-pane
  446. sync-state sync-progress
  447. {:idle? idle?
  448. :syncing? syncing?
  449. :need-password? need-password?
  450. :full-sync? full-syncing?
  451. :online? online?
  452. :queuing? queuing?
  453. :no-active-files? no-active-files?
  454. :history-files? (seq history-files)})
  455. (when (and
  456. (not enabled-progress-panel?)
  457. synced-file-graph? queuing?)
  458. [:div.head-ctls (sync-now)])]}))])))
  459. (rum/defc pick-local-graph-for-sync [graph]
  460. [:div.cp__file-sync-related-normal-modal
  461. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "cloud-download")]]
  462. [:h1.mb-5.text-2xl.text-center.font-bold (util/format "Sync graph \"%s\" to local"
  463. (:GraphName graph))]
  464. (ui/button
  465. "Open a local directory"
  466. :class "block w-full py-4 mt-4"
  467. :on-click #(do
  468. (state/close-modal!)
  469. (fs-sync/<sync-stop)
  470. (->
  471. (page-handler/ls-dir-files!
  472. (fn [{:keys [url]}]
  473. (file-sync-handler/init-remote-graph url graph)
  474. (js/setTimeout (fn [] (repo-handler/refresh-repos!)) 200))
  475. {:empty-dir?-or-pred
  476. (fn [ret]
  477. (let [empty-dir? (nil? (second ret))]
  478. (if-let [root (first ret)]
  479. ;; verify directory
  480. (-> (if empty-dir?
  481. (p/resolved nil)
  482. (if (util/electron?)
  483. (ipc/ipc :readGraphTxIdInfo root)
  484. (fs-util/read-graphs-txid-info root)))
  485. (p/then (fn [^js info]
  486. (when (and (not empty-dir?)
  487. (or (nil? info)
  488. (nil? (second info))
  489. (not= (second info) (:GraphUUID graph))))
  490. (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.")
  491. (p/resolved nil)
  492. (throw (js/Error. nil)))))))
  493. ;; cancel pick a directory
  494. (throw (js/Error. nil)))))})
  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. (rum/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 "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/original-name page-entity) page-name)]
  556. (rum/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. (rum/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 (state/get-current-repo) page-entity nil)]
  618. ;; ready loading
  619. [:div.flex.items-center.h-full.justify-center.w-full.absolute.ready-loading
  620. (ui/loading "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. ))]]))
  657. (rum/defc onboarding-unavailable-file-sync
  658. [close-fn]
  659. [:div.cp__file-sync-unavailable-logseq-sync
  660. [:span.head-bg]
  661. [:h1.text-2xl.font-bold
  662. [:span.pr-2.dark:text-white.text-gray-800 "Logseq Sync"]
  663. [:span.opacity-80 "is not yet available for you. 😔 "]]
  664. [:h2
  665. "Thanks for creating an account! To ensure that our file syncing service runs well when we release it"
  666. [:br]
  667. "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 "
  668. [:br]
  669. "charitable OpenCollective sponsors and backers. We can notify you once it becomes available for you."]
  670. [:div.pt-6.flex.justify-end.space-x-2
  671. (ui/button "Close" :on-click close-fn :background "gray" :class "opacity-60")]])
  672. (rum/defc onboarding-congrats-successful-sync
  673. [close-fn]
  674. [:div.cp__file-sync-related-normal-modal
  675. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "checkup-list" {:size 28})]]
  676. [:h1.text-xl.font-semibold.opacity-90.text-center.py-2
  677. [:span.dark:opacity-80 "Congrats on your first successful sync!"]]
  678. [:h2.text-center.dark:opacity-70.text-sm.opacity-90
  679. [:div "By using this graph with Logseq Sync you can now transition seamlessly between your different "]
  680. [:div
  681. [:span "devices. Go to the "]
  682. [:span.dark:text-white "All Graphs "]
  683. [:span "pages to manage your remote graph or switch to another local graph "]]
  684. [:div "and sync it as well."]]
  685. [:div.cloud-tip.rounded-md.mt-6.py-4
  686. [:div.items-center.opacity-90.flex.justify-center
  687. [:span.pr-2.flex (ui/icon "bell-ringing" {:class "font-semibold"})]
  688. [:strong "Logseq Sync is still in Beta and we're working on a Pro plan!"]]
  689. ;; [:ul.flex.py-6.px-4
  690. ;; [:li.it
  691. ;; [:h1.dark:text-white "10"]
  692. ;; [:h2 "Remote Graphs"]]
  693. ;; [:li.it
  694. ;; [:h1.dark:text-white "5G"]
  695. ;; [:h2 "Storage per Graph"]]
  696. ;; [:li.it
  697. ;; [:h1.dark:text-white "50G"]
  698. ;; [:h2 "Total Storage"]]]
  699. ]
  700. [:div.pt-6.flex.justify-end.space-x-2
  701. (ui/button "Done" :on-click close-fn)]])
  702. (defn open-icloud-graph-clone-picker
  703. ([] (open-icloud-graph-clone-picker (state/get-current-repo)))
  704. ([repo]
  705. (when (and repo (mobile-util/iCloud-container-path? repo))
  706. (state/set-modal!
  707. (fn [close-fn]
  708. (clone-local-icloud-graph-panel repo (util/node-path.basename repo) close-fn))
  709. {:close-btn? false :center? true}))))
  710. (defn make-onboarding-panel
  711. [type]
  712. (fn [close-fn]
  713. (case type
  714. :welcome
  715. (onboarding-welcome-logseq-sync close-fn)
  716. :unavailable
  717. (onboarding-unavailable-file-sync close-fn)
  718. :congrats
  719. (onboarding-congrats-successful-sync close-fn)
  720. [:p
  721. [:h1.text-xl.font-bold "Not handled!"]
  722. [:a.button {:on-click close-fn} "Got it!"]])))
  723. (defn maybe-onboarding-show
  724. [type]
  725. (when-not (get (state/sub :file-sync/onboarding-state) (keyword type))
  726. (try
  727. (let [current-repo (state/get-current-repo)
  728. local-repo? (= current-repo config/local-repo)
  729. login? (boolean (state/sub :auth/id-token))]
  730. (when login?
  731. (case type
  732. :welcome
  733. (when (or local-repo?
  734. (:GraphUUID (repo-handler/get-detail-graph-info current-repo)))
  735. (throw (js/Error. "current repo have been local or remote graph")))
  736. (:sync-initiate :sync-learn :sync-history)
  737. (do (quick-tour/ready
  738. (fn []
  739. (quick-tour/start-file-sync type)
  740. (state/set-state! [:file-sync/onboarding-state type] true)))
  741. (throw (js/Error. nil)))
  742. :default)
  743. (state/pub-event! [:file-sync/onboarding-tip type])
  744. (state/set-state! [:file-sync/onboarding-state (keyword type)] true)))
  745. (catch :default e
  746. (js/console.warn "[onboarding SKIP] " (name type) e)))))