Quellcode durchsuchen

fix: add loading status

Tienson Qin vor 4 Jahren
Ursprung
Commit
17bb4be3ad

+ 7 - 13
src/main/frontend/components/sidebar.cljs

@@ -174,22 +174,22 @@
         current-repo (state/sub :git/current-repo)
         latest-journals (db/get-latest-journals (state/get-current-repo) journals-length)
         preferred-format (state/sub [:me :preferred_format])
-        logged? (:name me)
-        token (state/sub :encrypt/token)
-        ;; TODO: remove this
-        daily-migrating? (state/sub [:daily/migrating?])]
+        logged? (:name me)]
     (rum/with-context [[t] i18n/*tongue-context*]
       [:div.max-w-7xl.mx-auto
        (cond
-         daily-migrating?
-         (ui/loading "Migrating to daily notes")
-
          (and default-home
               (= :home (state/get-current-route))
               (not (state/route-has-p?)))
          (route-handler/redirect! {:to :page
                                    :path-params {:name (:page default-home)}})
 
+         importing-to-db?
+         (ui/loading (t :parsing-files))
+
+         loading-files?
+         (ui/loading (t :loading-files))
+
          (and (not logged?) (seq latest-journals))
          (journal/journals latest-journals)
 
@@ -206,12 +206,6 @@
          (seq latest-journals)
          (journal/journals latest-journals)
 
-         importing-to-db?
-         (ui/loading (t :parsing-files))
-
-         loading-files?
-         (ui/loading (t :loading-files))
-
          (and logged? (empty? (:repos me)))
          (widgets/add-repo)
 

+ 22 - 22
src/main/frontend/fs.cljs

@@ -122,13 +122,13 @@
           sub-dir (->> (butlast parts)
                        (remove string/blank?)
                        (string/join "/"))
-          handle-path (str "handle/"
-                           (subs dir 1)
-                           (if sub-dir
-                             (str "/" sub-dir)))
-          handle-path (if (= "/" (last handle-path))
-                        (subs handle-path 0 (dec (count handle-path)))
-                        handle-path)
+          sub-dir-handle-path (str "handle/"
+                                   (subs dir 1)
+                                   (if sub-dir
+                                     (str "/" sub-dir)))
+          handle-path (if (= "/" (last sub-dir-handle-path))
+                        (subs sub-dir-handle-path 0 (dec (count sub-dir-handle-path)))
+                        sub-dir-handle-path)
           basename-handle-path (str handle-path "/" basename)
           file-handle-cache (get-nfs-file-handle basename-handle-path)]
       (p/let [file-handle (or file-handle-cache (idb/get-item basename-handle-path))]
@@ -174,23 +174,23 @@
 (defn stat
   [dir path]
   (let [append-path (if path
-               (str "/" (string/replace-first path "/" ""))
-               "")]
+                      (str "/" (string/replace-first path "/" ""))
+                      "")]
     (cond
-     (local-db? dir)
-     (if-let [file (get-nfs-file-handle (str "handle/"
-                                             (string/replace-first dir "/" "")
-                                             append-path))]
-       (p/let [file (.getFile file)]
-         (let [get-attr #(gobj/get file %)]
-           {:file/last-modified-at (get-attr "lastModified")
-            :file/size (get-attr "size")
-            :file/type (get-attr "type")}))
-       (p/rejected "File not exists"))
-
-     :else
+      (local-db? dir)
+      (if-let [file (get-nfs-file-handle (str "handle/"
+                                              (string/replace-first dir "/" "")
+                                              append-path))]
+        (p/let [file (.getFile file)]
+          (let [get-attr #(gobj/get file %)]
+            {:file/last-modified-at (get-attr "lastModified")
+             :file/size (get-attr "size")
+             :file/type (get-attr "type")}))
+        (p/rejected "File not exists"))
+
+      :else
      ;; FIXME: same format
-     (js/window.pfs.stat (str dir append-path)))))
+      (js/window.pfs.stat (str dir append-path)))))
 
 (defn mkdir-if-not-exists
   [dir]

+ 1 - 3
src/main/frontend/handler.cljs

@@ -84,7 +84,6 @@
 
                               :else
                               (state/set-db-restoring! false))
-
                             (if (schema-changed?)
                               (do
                                 (notification/show!
@@ -113,7 +112,7 @@
                                (fn []
                                  (js/console.error "Failed to request GitHub app tokens."))))
 
-                            (watch-for-date!)))
+                            (nfs/trigger-check! watch-for-date!)))
                          (p/catch (fn [error]
                                     (log/error :db/restore-failed error))))))]
     ;; clear this interval
@@ -185,7 +184,6 @@
        (notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
        (state/set-indexedb-support! false)))
 
-    ;; (nfs/trigger-check!)
     (p/let [nfs-dbs (idb/get-nfs-dbs)
             nfs-dbs (map (fn [db]
                            {:url db :nfs? true}) nfs-dbs)]

+ 1 - 1
src/main/frontend/handler/file.cljs

@@ -82,7 +82,7 @@
 (defn load-files
   [repo-url]
   (state/set-cloning! false)
-  (state/set-state! :repo/loading-files? true)
+  (state/set-loading-files! true)
   (p/let [files (git/list-files repo-url)
           files (bean/->clj files)
           config-content (load-file repo-url (str config/app-name "/" config/config-file))

+ 45 - 41
src/main/frontend/handler/project.cljs

@@ -16,38 +16,41 @@
   ([ok-handler]
    (create-project! (state/get-current-project) ok-handler))
   ([project ok-handler]
-   (let [config (state/get-config)
-         data {:name project
-               :repo (state/get-current-repo)
-               :settings (or (get config :project)
-                             {:name project})}]
-     (util/post (str config/api "projects")
-                data
-                (fn [result]
-                  (when-not (:message result) ; exists
-                    (swap! state/state
-                           update-in [:me :projects]
-                           (fn [projects]
-                             (util/distinct-by :name (conj projects result))))
-                    (ok-handler project)))
-                (fn [error]
-                  (js/console.dir error)
-                  (notification/show! (util/format "Project \"%s\" already taken, please change to another name." project) :error))))))
+   (when (state/logged?)
+     (let [config (state/get-config)
+           data {:name project
+                 :repo (state/get-current-repo)
+                 :settings (or (get config :project)
+                               {:name project})}]
+       (util/post (str config/api "projects")
+                  data
+                  (fn [result]
+                    (when-not (:message result) ; exists
+                      (swap! state/state
+                             update-in [:me :projects]
+                             (fn [projects]
+                               (util/distinct-by :name (conj projects result))))
+                      (ok-handler project)))
+                  (fn [error]
+                    (js/console.dir error)
+                    (notification/show! (util/format "Project \"%s\" already taken, please change to another name." project) :error)))))))
 
 (defn exists-or-create!
   [ok-handler modal-content]
-  (if-let [project (state/get-current-project)]
-    (if (project-exists? project)
-      (ok-handler project)
-      (create-project! ok-handler))
-    (state/set-modal! modal-content)))
+  (when (state/logged?)
+    (if-let [project (state/get-current-project)]
+      (if (project-exists? project)
+        (ok-handler project)
+        (create-project! ok-handler))
+      (state/set-modal! modal-content))))
 
 (defn add-project!
   [project]
-  (create-project! project
-                   (fn []
-                     (notification/show! (util/format "Project \"%s\" was created successfully." project) :success)
-                     (state/close-modal!))))
+  (when (state/logged?)
+    (create-project! project
+                     (fn []
+                       (notification/show! (util/format "Project \"%s\" was created successfully." project) :success)
+                       (state/close-modal!)))))
 
 (defn sync-project-settings!
   ([]
@@ -55,18 +58,19 @@
      (let [settings (:project (state/get-config))]
        (sync-project-settings! project-name settings))))
   ([project-name settings]
-   (when-let [repo (state/get-current-repo)]
-     (if (project-exists? project-name)
-       (util/post (str config/api "projects/" project-name)
-                  {:name project-name
-                   :settings settings
-                   :repo repo}
-                  (fn [response]
-                    (notification/show! "Project settings changed successfully!" :success))
-                  (fn [error]
-                    (println "Project settings updated failed, reason: ")
-                    (js/console.dir error)))
-       (when (and settings
-                  (not (string/blank? (:name settings)))
-                  (>= (count (string/trim (:name settings))) 2))
-         (add-project! (:name settings)))))))
+   (when (state/logged?)
+     (when-let [repo (state/get-current-repo)]
+       (if (project-exists? project-name)
+         (util/post (str config/api "projects/" project-name)
+                    {:name project-name
+                     :settings settings
+                     :repo repo}
+                    (fn [response]
+                      (notification/show! "Project settings changed successfully!" :success))
+                    (fn [error]
+                      (println "Project settings updated failed, reason: ")
+                      (js/console.dir error)))
+         (when (and settings
+                    (not (string/blank? (:name settings)))
+                    (>= (count (string/trim (:name settings))) 2))
+           (add-project! (:name settings))))))))

+ 4 - 4
src/main/frontend/handler/repo.cljs

@@ -148,8 +148,8 @@
 
 (defn- parse-files-and-load-to-db!
   [repo-url files {:keys [first-clone? delete-files delete-blocks re-render? re-render-opts] :as opts}]
-  (state/set-state! :repo/loading-files? false)
-  (state/set-state! :repo/importing-to-db? true)
+  (state/set-loading-files! false)
+  (state/set-importing-to-db! true)
   (let [file-paths (map :file/path files)
         parsed-files (filter
                       (fn [file]
@@ -168,7 +168,7 @@
     (when first-clone? (create-default-files! repo-url))
     (when re-render?
       (ui-handler/re-render-root! re-render-opts))
-    (state/set-state! :repo/importing-to-db? false)))
+    (state/set-importing-to-db! false)))
 
 (defn load-repo-to-db!
   [repo-url {:keys [first-clone? diffs nfs-files]}]
@@ -192,7 +192,7 @@
                   (js/console.dir error)
                   ;; Empty repo
                   (create-default-files! repo-url)
-                  (state/set-state! :repo/loading-files? false))))
+                  (state/set-loading-files! false))))
 
       :else
       (when (seq diffs)

+ 8 - 15
src/main/frontend/handler/web/nfs.cljs

@@ -71,6 +71,7 @@
      (p/let [result (utils/openDirectory #js {:recursive true}
                                          (fn [path handle]
                                            (swap! path-handles assoc path handle)))
+             _ (state/set-loading-files! true)
              root-handle (nth result 0)
              dir-name (gobj/get root-handle "name")
              repo (str config/local-db-prefix dir-name)
@@ -86,6 +87,7 @@
                          (p/let [content (.text (:file/file file))]
                            (assoc file :file/content content))) markup-files))
            (p/then (fn [result]
+                     _ (state/set-loading-files! false)
                      (let [files (map #(dissoc % :file/file) result)]
                        (repo-handler/start-repo-db-if-not-exists! repo {:db-type :local-native-fs})
                        (repo-handler/load-repo-to-db! repo
@@ -118,7 +120,7 @@
       (utils/verifyPermission handle true))))
 
 (defn ask-permission
-  [repo]
+  [repo cb]
   (fn [close-fn]
     [:div
      [:p.text-gray-700
@@ -127,12 +129,13 @@
      (ui/button
       "Grant"
       :on-click (fn []
-                  (check-directory-permission! repo)
+                  (p/let [_ (check-directory-permission! repo)]
+                    (cb))
                   (close-fn)))]))
 
-(defn trigger-check! []
+(defn trigger-check! [cb]
   (when-let [repo (get-local-repo)]
-    (state/set-modal! (ask-permission repo))))
+    (state/set-modal! (ask-permission repo cb))))
 
 (defn- compute-diffs
   [old-files new-files]
@@ -223,17 +226,7 @@
 
 (defn- refresh!
   [repo]
-  (when repo
-    (reload-dir! repo)
-    ;; check permission is too slow
-    ;; (->
-    ;;  (p/let [verified? (check-directory-permission! repo)]
-    ;;    (prn {:verified? verified?})
-    ;;    (when verified?
-    ;;      (reload-dir! repo)))
-    ;;  (p/catch (fn [error]
-    ;;             (log/error :nfs/refresh-error error))))
-))
+  (when repo (reload-dir! repo)))
 
 (defn supported?
   []

+ 10 - 5
src/main/frontend/state.cljs

@@ -16,7 +16,6 @@
   (atom
    {:route-match nil
     :today nil
-    :daily/migrating? nil
     :db/batch-txs (async/chan 100)
     :notification/show? false
     :notification/content nil
@@ -25,6 +24,7 @@
     :repo/importing-to-db? nil
     :repo/sync-status {}
     :repo/changed-files nil
+    :nfs/loading-files? nil
     ;; TODO: how to detect the network reliably?
     :network/online? true
     :indexeddb/support? true
@@ -822,10 +822,6 @@
   [value]
   (set-state! :ui/left-sidebar-open? value))
 
-(defn set-daily-migrating!
-  [value]
-  (set-state! :daily/migrating? value))
-
 (defn set-developer-mode!
   [value]
   (set-state! :ui/developer-mode? value)
@@ -896,7 +892,16 @@
   [value]
   (set-state! :graph/syncing? value))
 
+(defn set-loading-files!
+  [value]
+  (set-state! :repo/loading-files? value))
+
+(defn set-importing-to-db!
+  [value]
+  (set-state! :repo/importing-to-db? value))
+
 ;; TODO: Move those to the uni `state`
+
 (defonce editor-op (atom nil))
 (defn set-editor-op!
   [value]

+ 16 - 15
src/main/frontend/utils.js

@@ -111,21 +111,6 @@ export var getFiles = async function (dirHandle, recursive, cb, path = dirHandle
   return [(await Promise.all(dirs)), (await Promise.all(files))];
 };
 
-export var openDirectory = async function (options = {}, cb) {
-  options.recursive = options.recursive || false;
-  const handle = await window.showDirectoryPicker({ mode: 'readwrite' });
-  return [handle, getFiles(handle, options.recursive, cb)];
-};
-
-export var writeFile = async function (fileHandle, contents) {
-  // Create a FileSystemWritableFileStream to write to.
-  const writable = await fileHandle.createWritable();
-  // Write the contents of the file to the stream.
-  await writable.write(contents);
-  // Close the file and write the contents to disk.
-  await writable.close();
-};
-
 export var verifyPermission = async function (handle, readWrite) {
   const options = {};
   if (readWrite) {
@@ -143,6 +128,22 @@ export var verifyPermission = async function (handle, readWrite) {
   return false;
 }
 
+export var openDirectory = async function (options = {}, cb) {
+  options.recursive = options.recursive || false;
+  const handle = await window.showDirectoryPicker({ mode: 'readwrite' });
+  const _ask = await verifyPermission(handle, true);
+  return [handle, getFiles(handle, options.recursive, cb)];
+};
+
+export var writeFile = async function (fileHandle, contents) {
+  // Create a FileSystemWritableFileStream to write to.
+  const writable = await fileHandle.createWritable();
+  // Write the contents of the file to the stream.
+  await writable.write(contents);
+  // Close the file and write the contents to disk.
+  await writable.close();
+};
+
 export var nfsSupported = function () {
   if ('chooseFileSystemEntries' in self) {
     return 'chooseFileSystemEntries';