Browse Source

fix(project): hide the menu when there is not any project

defclass 5 years ago
parent
commit
c9d25bcb95

+ 83 - 68
src/main/frontend/components/header.cljs

@@ -13,8 +13,8 @@
             [frontend.handler.export :as export]
             [frontend.components.svg :as svg]
             [frontend.components.repo :as repo]
-            [frontend.components.page :as page]
             [frontend.components.search :as search]
+            [frontend.handler.project :as project-handler]
             [frontend.handler.web.nfs :as nfs]))
 
 (rum/defc logo < rum/reactive
@@ -43,73 +43,79 @@
 
 (rum/defc dropdown-menu < rum/reactive
   [{:keys [me current-repo t default-home]}]
-  (ui/dropdown-with-links
-   (fn [{:keys [toggle-fn]}]
-     [:button.max-w-xs.flex.items-center.text-sm.rounded-full.focus:outline-none.focus:shadow-outline.h-7.w-7.ml-2
-      {:on-click toggle-fn}
-      (if-let [avatar (:avatar me)]
-        [:img.h-7.w-7.rounded-full
-         {:src avatar}]
-        [:div.h-7.w-7.rounded-full.bg-base-2.opacity-70.hover:opacity-100 {:style {:padding 1.5}}
-         [:a svg/user]])])
-   (let [logged? (:name me)]
-     (->>
-      [(when current-repo
-         {:title (t :graph)
-          :options {:href (rfe/href :graph)}
-          :icon svg/graph-sm})
-       (when (or logged? (and (nfs/supported?) current-repo))
-         {:title (t :all-graphs)
-          :options {:href (rfe/href :repos)}
-          :icon svg/repos-sm})
-       (when current-repo
-         {:title (t :all-pages)
-          :options {:href (rfe/href :all-pages)}
-          :icon svg/pages-sm})
-       (when current-repo
-         {:title (t :all-files)
-          :options {:href (rfe/href :all-files)}
-          :icon svg/folder-sm})
-       (when (and default-home current-repo)
-         {:title (t :all-journals)
-          :options {:href (rfe/href :all-journals)}
-          :icon svg/calendar-sm})
-       (when current-repo
-         {:title (t :my-publishing)
-          :options {:href (rfe/href :my-publishing)}})
-       {:title (t :excalidraw-title)
-        :options {:href (rfe/href :draw)}
-        :icon (svg/excalidraw-logo)}
-       {:title (t :settings)
-        :options {:href (rfe/href :settings)}
-        :icon svg/settings-sm}
-       (when-let [project (and current-repo (state/get-current-project))]
-         (let [link (str config/website "/" project)]
-           {:title (str (t :go-to) "/" project)
-            :options {:href link
-                      :target "_blank"}
-            :icon svg/external-link}))
-       (when (and logged? current-repo)
-         {:title (t :export)
-          :options {:on-click (fn []
-                                (export/export-repo-as-html! current-repo))}
-          :icon nil})
-       (when current-repo
-         {:title (t :import)
-          :options {:href (rfe/href :import)}
-          :icon svg/import-sm})
-       {:title [:div.flex-row.flex.justify-between.items-center
-                [:span (t :join-community)]]
-        :options {:href "https://discord.gg/KpN4eHY"
-                  :title (t :discord-title)
-                  :target "_blank"}
-        :icon svg/discord}
-       (when logged?
-         {:title (t :sign-out)
-          :options {:on-click user-handler/sign-out!}
-          :icon svg/logout-sm})]
-      (remove nil?)))
-   {}))
+  (let [projects (state/sub [:me :projects])]
+    (ui/dropdown-with-links
+     (fn [{:keys [toggle-fn]}]
+       [:button.max-w-xs.flex.items-center.text-sm.rounded-full.focus:outline-none.focus:shadow-outline.h-7.w-7.ml-2
+        {:on-click toggle-fn}
+        (if-let [avatar (:avatar me)]
+          [:img.h-7.w-7.rounded-full
+           {:src avatar}]
+          [:div.h-7.w-7.rounded-full.bg-base-2.opacity-70.hover:opacity-100 {:style {:padding 1.5}}
+           [:a svg/user]])])
+     (let [logged? (:name me)]
+       (->>
+         [(when current-repo
+            {:title (t :graph)
+             :options {:href (rfe/href :graph)}
+             :icon svg/graph-sm})
+          (when (or logged? (and (nfs/supported?) current-repo))
+            {:title (t :all-graphs)
+             :options {:href (rfe/href :repos)}
+             :icon svg/repos-sm})
+          (when current-repo
+            {:title (t :all-pages)
+             :options {:href (rfe/href :all-pages)}
+             :icon svg/pages-sm})
+          (when current-repo
+            {:title (t :all-files)
+             :options {:href (rfe/href :all-files)}
+             :icon svg/folder-sm})
+          (when (and default-home current-repo)
+            {:title (t :all-journals)
+             :options {:href (rfe/href :all-journals)}
+             :icon svg/calendar-sm})
+
+          (when (project-handler/get-current-project current-repo projects)
+            {:title (t :my-publishing)
+             :options {:href (rfe/href :my-publishing)}})
+
+          (when current-repo
+            {:title (t :my-publishing)
+             :options {:href (rfe/href :my-publishing)}})
+          {:title (t :excalidraw-title)
+           :options {:href (rfe/href :draw)}
+           :icon (svg/excalidraw-logo)}
+          {:title (t :settings)
+           :options {:href (rfe/href :settings)}
+           :icon svg/settings-sm}
+          (when-let [project (and current-repo (state/get-current-project))]
+            (let [link (str config/website "/" project)]
+              {:title (str (t :go-to) "/" project)
+               :options {:href link
+                         :target "_blank"}
+               :icon svg/external-link}))
+          (when (and logged? current-repo)
+            {:title (t :export)
+             :options {:on-click (fn []
+                                   (export/export-repo-as-html! current-repo))}
+             :icon nil})
+          (when current-repo
+            {:title (t :import)
+             :options {:href (rfe/href :import)}
+             :icon svg/import-sm})
+          {:title [:div.flex-row.flex.justify-between.items-center
+                   [:span (t :join-community)]]
+           :options {:href "https://discord.gg/KpN4eHY"
+                     :title (t :discord-title)
+                     :target "_blank"}
+           :icon svg/discord}
+          (when logged?
+            {:title (t :sign-out)
+             :options {:on-click user-handler/sign-out!}
+             :icon svg/logout-sm})]
+         (remove nil?)))
+     {})))
 
 (rum/defc right-menu-button < rum/reactive
   []
@@ -118,6 +124,7 @@
    (svg/menu)])
 
 (rum/defc header
+  < rum/reactive
   [{:keys [open-fn current-repo white? logged? page? route-match me default-home new-block-mode]}]
   (let [local-repo? (= current-repo config/local-repo)
         repos (->> (state/sub [:me :repos])
@@ -149,6 +156,14 @@
        [:div.repos.hidden.md:block
         (repo/repos-dropdown true)]
 
+       (let [projects (state/sub [:me :projects])]
+         (when-let [project (project-handler/get-current-project current-repo projects)]
+           [:a.opacity-70.hover:opacity-100.ml-4
+            {:title (str (t :go-to) "/" project)
+             :href (str config/website "/" project)
+             :target "_blank"}
+            svg/external-link]))
+
        (when (and (nfs/supported?) (empty? repos))
          (ui/tooltip
           "Warning: this is an experimental feature, please only use it for testing purpose."

+ 11 - 18
src/main/frontend/components/publishing.cljs

@@ -13,7 +13,8 @@
             [frontend.handler.notification :as notification]
             [clojure.string :as string]
             [frontend.ui :as ui]
-            [frontend.components.svg :as svg]))
+            [frontend.components.svg :as svg]
+            [frontend.handler.project :as project-handler]))
 
 (defn get-published-pages
   []
@@ -69,25 +70,17 @@
             (log/error :project/http-delete-failed error)
             (reject error)))))))
 
-(defn get-current-project
-  [current-repo projects]
-  (let [project (some (fn [p]
-                        (when (= (:repo p) current-repo)
-                          p))
-                  projects)
-        project-name (:name project)]
-    (when-not (string/blank? project-name) project-name)))
-
 (defn project
   [editor-state current-project pages]
   (if (= :display @editor-state)
-    [:div.cp__publishing-pj
-     [:span.cp__publishing-pj-name current-project]
-     [:span.cp__publishing-edit
-      {:on-click
-             (fn [_]
-               (reset! editor-state :editor))}
-      "edit"]]
+    (when current-project
+      [:div.cp__publishing-pj
+       [:span.cp__publishing-pj-name current-project]
+       [:span.cp__publishing-edit
+        {:on-click
+         (fn [_]
+           (reset! editor-state :editor))}
+        "edit"]])
     [:div.flex.cp__publishing_pj_edit
      [:input#cp__publishing-project-input
       {:placeholder current-project
@@ -146,7 +139,7 @@
         projects (state/sub [:me :projects])
         pages (get-published-pages)
         editor-state (get state ::project-state)
-        current-project (get-current-project current-repo projects)]
+        current-project (project-handler/get-current-project current-repo projects)]
     (rum/with-context [[t] i18n/*tongue-context*]
       [:div.flex-1
        [:h1.title (t :my-publishing)]

+ 9 - 0
src/main/frontend/handler/project.cljs

@@ -5,6 +5,15 @@
             [frontend.config :as config]
             [frontend.handler.notification :as notification]))
 
+(defn get-current-project
+  [current-repo projects]
+  (let [project (some (fn [p]
+                        (when (= (:repo p) current-repo)
+                          p))
+                  projects)
+        project-name (:name project)]
+    (when-not (string/blank? project-name) project-name)))
+
 ;; project exists and current user owns it
 ;; if project not exists, the server will create it
 (defn project-exists?