fix: remote graphs state when create/delete a remote graph

This commit is contained in:
Tienson Qin
2022-11-08 00:48:38 +08:00
parent 5a66bf8fb9
commit 20e1e77362
7 changed files with 56 additions and 45 deletions

View File

@@ -135,7 +135,9 @@
(when-let [GraphUUID (get (async/<! (file-sync-handler/create-graph graph-name)) 2)]
(async/<! (fs-sync/<sync-start))
(state/set-state! [:ui/loading? :graph/create-remote?] false)
;; update existing repo
;; update both local && remote graphs
(state/add-remote-graph! {:GraphUUID GraphUUID
:GraphName graph-name})
(state/set-repos! (map (fn [r]
(if (= (:url r) repo)
(assoc r

View File

@@ -58,39 +58,37 @@
(state/pub-event! [:graph/switch url])))
[:div.controls
(let [loading? (state/sub [:ui/loading? :remove/remote-graph GraphUUID])]
[:div.flex.flex-row.items-center
(when loading? [:div.ml-2 (ui/loading "")])
(ui/tippy {:html [:div.text-sm.max-w-xs
(if only-cloud?
"Deletes this remote graph. Note this can't be recovered."
"Removes Logseq's access to the local file path of your graph. It won't remove your local files.")]
:class "tippy-hover"
:interactive true}
[:a.text-gray-400.ml-4.font-medium.text-sm.whitespace-nowrap
{:on-click (fn []
(if only-cloud?
(let [confirm-fn
(fn []
(ui/make-confirm-modal
{:title [:div
{:style {:max-width 700}}
(str "Are you sure to permanently delete the graph \"" GraphName "\" from our server?")]
:sub-title [:div.small.mt-1
"Notice that we can't recover this graph after being deleted. Make sure you have backups before deleting it."]
:on-confirm (fn [_ {:keys [close-fn]}]
(close-fn)
(state/set-state! [:ui/loading? :remove/remote-graph GraphUUID] true)
(go (<! (file-sync/<delete-graph GraphUUID))
(file-sync/load-session-graphs)
(state/set-state! [:ui/loading? :remove/remote-graph GraphUUID] false)))}))]
(state/set-modal! (confirm-fn)))
(let [current-repo (state/get-current-repo)]
(repo-handler/remove-repo! repo)
(state/pub-event! [:graph/unlinked repo current-repo])
(when only-cloud?
(file-sync/load-session-graphs)))))}
(if only-cloud? "Remove" "Unlink")])])]]))
[:div.flex.flex-row.items-center
(ui/tippy {:html [:div.text-sm.max-w-xs
(if only-cloud?
"Deletes this remote graph. Note this can't be recovered."
"Removes Logseq's access to the local file path of your graph. It won't remove your local files.")]
:class "tippy-hover"
:interactive true}
[:a.text-gray-400.ml-4.font-medium.text-sm.whitespace-nowrap
{:on-click (fn []
(if only-cloud?
(let [confirm-fn
(fn []
(ui/make-confirm-modal
{:title [:div
{:style {:max-width 700}}
(str "Are you sure to permanently delete the graph \"" GraphName "\" from our server?")]
:sub-title [:div.small.mt-1
"Notice that we can't recover this graph after being deleted. Make sure you have backups before deleting it."]
:on-confirm (fn [_ {:keys [close-fn]}]
(close-fn)
(state/set-state! [:file-sync/remote-graphs :loading] true)
(go (<! (file-sync/<delete-graph GraphUUID))
(state/delete-repo! repo)
(state/delete-remote-graph! repo)
(state/set-state! [:file-sync/remote-graphs :loading] false)))}))]
(state/set-modal! (confirm-fn)))
(let [current-repo (state/get-current-repo)]
(repo-handler/remove-repo! repo)
(state/pub-event! [:graph/unlinked repo current-repo]))))}
(if only-cloud? "Remove" "Unlink")])]]]))
(rum/defc repos < rum/reactive
[]

View File

@@ -194,7 +194,6 @@
{:pre [(int? latest-txid) (>= latest-txid 0)]}
(-> (p/let [_ (persist-var/-reset-value! graphs-txid [user-uuid graph-uuid latest-txid] repo)
_ (persist-var/persist-save graphs-txid)]
(state/pub-event! [:graph/refresh])
(when (state/developer-mode?) (assert-local-txid<=remote-txid)))
p->c))

View File

@@ -113,16 +113,12 @@
(route-handler/redirect-to-home!)))
(when-let [dir-name (config/get-repo-dir repo)]
(fs/watch-dir! dir-name))
(repo-handler/refresh-repos!)
(file-sync-restart!))
(defmethod handle :graph/unlinked [repo current-repo]
(when (= (:url repo) current-repo)
(file-sync-restart!)))
(defmethod handle :graph/refresh [_]
(repo-handler/refresh-repos!))
;; FIXME: awful multi-arty function.
;; Should use a `-impl` function instead of the awful `skip-ios-check?` param with nested callback.
(defn- graph-switch
@@ -141,7 +137,6 @@
(fs/watch-dir! dir-name))
(srs/update-cards-due-count!)
(state/pub-event! [:graph/ready graph])
(repo-handler/refresh-repos!)
(file-sync-restart!)))))
;; Parameters for the `persist-db` function, to show the notification messages

View File

@@ -49,7 +49,8 @@
(string? r))
(let [tx-info [0 r (user/user-uuid) (state/get-current-repo)]]
(<! (apply sync/<update-graphs-txid! tx-info))
(swap! refresh-file-sync-component not) tx-info)
(swap! refresh-file-sync-component not)
tx-info)
(do
(state/set-state! [:ui/loading? :graph/create-remote?] false)
(cond

View File

@@ -498,7 +498,7 @@
[local-repos remote-repos]
(when-let [repos' (seq (concat (map #(if-let [sync-meta (seq (:sync-meta %))]
(assoc % :GraphUUID (second sync-meta)) %)
local-repos)
local-repos)
(some->> remote-repos
(map #(assoc % :remote? true)))))]
(let [repos' (group-by :GraphUUID repos')
@@ -515,7 +515,7 @@
[url]
(when-let [graphs (seq (and url (combine-local-&-remote-graphs
(state/get-repos)
(state/get-remote-repos))))]
(state/get-remote-graphs))))]
(first (filter #(when-let [url' (:url %)]
(= url url')) graphs))))
@@ -524,7 +524,7 @@
(p/let [repos (get-repos)
repos' (combine-local-&-remote-graphs
repos
(state/get-remote-repos))]
(state/get-remote-graphs))]
(state/set-repos! repos')
repos'))

View File

@@ -711,7 +711,7 @@ Similar to re-frame subscriptions"
(when-not (mobile-util/native-platform?)
"local")))
(defn get-remote-repos
(defn get-remote-graphs
[]
(get-in @state [:file-sync/remote-graphs :graphs]))
@@ -720,6 +720,22 @@ Similar to re-frame subscriptions"
(when-let [graphs (seq (get-in @state [:file-sync/remote-graphs :graphs]))]
(some #(when (= (:GraphUUID %) (str uuid)) %) graphs)))
(defn delete-remote-graph!
[repo]
(swap! state update-in [:file-sync/remote-graphs :graphs]
(fn [repos]
(remove #(and
(:GraphUUID repo)
(:GraphUUID %)
(= (:GraphUUID repo) (:GraphUUID %))) repos))))
(defn add-remote-graph!
[repo]
(swap! state update-in [:file-sync/remote-graphs :graphs]
(fn [repos]
(->> (conj repos repo)
(distinct)))))
(defn get-repos
[]
(get-in @state [:me :repos]))