Browse Source

enhance: create dir automatically when pulling down remote graphs

Tienson Qin 3 years ago
parent
commit
9652846be5

+ 2 - 2
src/main/frontend/components/repo.cljs

@@ -52,7 +52,7 @@
         :let [only-cloud? (and remote? (nil? url))]]
     [:div.flex.justify-between.mb-4.items-center {:key (or url GraphUUID)}
      (normalized-graph-label repo #(if only-cloud?
-                                     (state/pub-event! [:graph/pick-dest-to-sync repo])
+                                     (state/pub-event! [:graph/pull-down-remote-graph repo])
                                      (state/pub-event! [:graph/switch url])))
 
      [:div.controls
@@ -158,7 +158,7 @@
                                                       (if (gobj/get e "shiftKey")
                                                         (state/pub-event! [:graph/open-new-window url])
                                                         (if-not local?
-                                                          (state/pub-event! [:graph/pick-dest-to-sync graph])
+                                                          (state/pub-event! [:graph/pull-down-remote-graph graph])
                                                           (state/pub-event! [:graph/switch url]))))}})))
                     switch-repos)
         refresh-link (let [nfs-repo? (config/local-db? current-repo)]

+ 17 - 4
src/main/frontend/handler/events.cljs

@@ -179,10 +179,23 @@
       "Please wait seconds until all changes are saved for the current graph."
       :warning))))
 
-(defmethod handle :graph/pick-dest-to-sync [[_ graph]]
-  (state/set-modal!
-   (file-sync/pick-dest-to-sync-panel graph)
-   {:center? true}))
+(defmethod handle :graph/pull-down-remote-graph [[_ graph]]
+  (when-let [graph-name (:GraphName graph)]
+    (let [graph-name (util/safe-sanitize-file-name graph-name)]
+      (if (string/blank? graph-name)
+        (notification/show! "Illegal graph folder name.")
+
+        ;; Create graph directory under Logseq document folder (local)
+        (when-let [root (state/get-local-container-root-url)]
+          (-> (graph-picker/validate-graph-dirname root graph-name)
+              (p/then (fn [graph-path]
+                        (-> (fs/mkdir-if-not-exists graph-path)
+                            (p/then
+                             (fn []
+                               (nfs-handler/ls-dir-files-with-path! graph-path))))))
+              (p/catch (fn [^js e]
+                         (notification/show! (str e) :error)
+                         (js/console.error e)))))))))
 
 (defmethod handle :graph/pick-page-histories [[_ graph-uuid page-name]]
   (state/set-modal!

+ 14 - 23
src/main/frontend/mobile/graph_picker.cljs

@@ -16,10 +16,7 @@
 
 (defn validate-graph-dirname
   [root dirname]
-
-  ;; TODO: how to handle existing graph name directory?
-  ;; TODO: temporarily just load the existing folder
-  (p/resolved (util/node-path.join root dirname)))
+  (util/node-path.join root dirname))
 
 (rum/defc toggle-item
   [{:keys [on? title on-toggle]}]
@@ -56,27 +53,21 @@
                                (when-let [root (if icloud-sync-on?
                                                  (state/get-icloud-container-root-url)
                                                  (state/get-local-container-root-url))]
-                                 (-> (validate-graph-dirname root graph-name)
-                                     (p/then (fn [graph-path]
-                                               (-> (fs/mkdir-if-not-exists graph-path)
-                                                   (p/then
-                                                    (fn []
-                                                      (web-nfs/ls-dir-files-with-path!
-                                                       graph-path (merge
-                                                                   {:logseq-sync-on
-                                                                    logseq-sync-on?
-
-                                                                    :ok-handler
-                                                                    (fn []
-                                                                      (when logseq-sync-on?
-                                                                        (state/pub-event! [:sync/create-remote-graph (state/get-current-repo)])))}
-                                                                   opts))
-                                                      (notification/show! (str "Create graph: " graph-name) :success))))))
+                                 (-> (let [graph-path (validate-graph-dirname root graph-name)]
+                                       (-> (fs/mkdir-if-not-exists graph-path)
+                                           (p/then
+                                            (fn []
+                                              (web-nfs/ls-dir-files-with-path!
+                                               graph-path (merge
+                                                           {:ok-handler
+                                                            (fn []
+                                                              (when logseq-sync-on?
+                                                                (state/pub-event! [:sync/create-remote-graph (state/get-current-repo)])))}
+                                                           opts))
+                                              (notification/show! (str "Create graph: " graph-name) :success)))))
                                      (p/catch (fn [^js e]
                                                 (notification/show! (str e) :error)
-                                                (js/console.error e)))
-                                     (p/finally
-                                      #()))))))]
+                                                (js/console.error e))))))))]
 
     (rum/use-effect!
      (fn []